473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

.1 + .2 = 0.30000000000000004

In the below code, mOnePlusTwo evaluates to 0.30000000000000004 (although it
displays as ".3"). Consequently, comparing that value to mThree (.3) results
in false.

What's going on? Is there something else I should do (other than switch to
using decimal types)?

double mOne = .1;
double mTwo = .2;
double mThree = .3;
double mOnePlusTwo = mOne + mTwo;

bool mEqual = false;
if (mOnePlusTwo == mThree)
{
mEqual = true;
}

Console.WriteLine("mOne = " + mOne);
Console.WriteLine("mTwo = " + mTwo);
Console.WriteLine("mThree = " + mThree);
Console.WriteLine("mOnePlusTwo = " + mOnePlusTwo);
Console.WriteLine("mThree == mOnePlusTwo? = " + mEqual);
Nov 17 '05 #1
10 3021
Craig wrote:
In the below code, mOnePlusTwo evaluates to 0.30000000000000004
(although it displays as ".3"). Consequently, comparing that value
to mThree (.3) results in false.

What's going on? Is there something else I should do (other than
switch to using decimal types)?


Switch to decimal and read this article:

What Every Computer Scientist Should Know About Floating-Point Arithmetic
http://docs.sun.com/source/806-3568/ncg_goldberg.html

--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my
telephone."
- Bjarne Stroustrup (originator of C++) [quoted at the 2003
International Conference on Intelligent User Interfaces]
Nov 17 '05 #2
Double or float number always has a precision up to certain digits. In
other word, there is a rounding error. You should never compare to
doubles for equal. To compare doubles:
double epsilon = .0000000001; // a precision you like up to double
can have.
if (Math.Abs(mOnePlusTwo - mThree) < epsilon)
true part
else
false part

Nov 17 '05 #3
This is entirely correct.

In base 10, the rational number 1 divided by the rational number 3 yeilds a
repeating decimal value 0.333333333333333. It is not accurately
represented.
In base 2, the same number (1/3) is not a repeating radix value. It is
accurately represented.

In base 2, the rational number 1 divided by the rational number 10 (1/10)
yeilds a repeating radix value. It is not accurately represented.

Since 0.1 cannot be accurately represented in binary, we don't try. We use
decimal notation which is a representation of the digits, not the number.

If you are making statistical calcuations, use double. For currency or
values where multiples of 1/10 are important, use decimal.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Craig" <Cr***@discussions.microsoft.com> wrote in message
news:19**********************************@microsof t.com...
In the below code, mOnePlusTwo evaluates to 0.30000000000000004 (although
it
displays as ".3"). Consequently, comparing that value to mThree (.3)
results
in false.

What's going on? Is there something else I should do (other than switch
to
using decimal types)?

double mOne = .1;
double mTwo = .2;
double mThree = .3;
double mOnePlusTwo = mOne + mTwo;

bool mEqual = false;
if (mOnePlusTwo == mThree)
{
mEqual = true;
}

Console.WriteLine("mOne = " + mOne);
Console.WriteLine("mTwo = " + mTwo);
Console.WriteLine("mThree = " + mThree);
Console.WriteLine("mOnePlusTwo = " + mOnePlusTwo);
Console.WriteLine("mThree == mOnePlusTwo? = " + mEqual);

Nov 17 '05 #4
Nick Malik [Microsoft] wrote:
In base 2, the same number (1/3) is not a repeating radix
value. It is accurately represented.


1 / 11 = 0.0101010101010101010101....

Till
Nov 17 '05 #5
Nick Malik [Microsoft] <ni*******@hotmail.nospam.com> wrote:
In base 2, the same number (1/3) is not a repeating radix value. It is
accurately represented.


No it isn't - *anything* which can't be accurately represented as a
non-recurring decimal can't be accurately represented as a binary
number either. (Conversely, everything which can be represented exactly
as binary can be represented exactly in decimal.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

"Craig" <Cr***@discussions.microsoft.com> wrote in message
news:19**********************************@microsof t.com...
In the below code, mOnePlusTwo evaluates to 0.30000000000000004 (although it displays as ".3"). Consequently, comparing that value to mThree (.3) results in false.


You need to round your numbers
no alternative
several ways to do this
you need to be careful
compare the two writes.

using System;

namespace trial1
{

class Class1
{

[STAThread]
static void Main(string[] args)
{
double dbl1 = .333333333333333;
double dbl2 = .4444444444444444;

double dbl3 = round(dbl1)+round(dbl2);

//Console.WriteLine(round(dbl1 + dbl2).ToString());

Console.WriteLine(round(dbl3).ToString());
}

static double round(double dbl)
{
//return Math.Round(dbl,2);
//return Convert.ToDouble(dbl.ToString("########.##"));
//return Convert.ToDouble(dbl.ToString("N"));
//return Convert.ToDouble( String.Format("{0:N}",dbl));
return Convert.ToDouble(String.Format("{0:F}",dbl));
}

}
}


Nov 17 '05 #7
if you use the decimal datatype, you do not need to round the numbers

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Zach" <00@00.00> wrote in message
news:f2***************************@freeler.nl...

"Craig" <Cr***@discussions.microsoft.com> wrote in message
news:19**********************************@microsof t.com...
In the below code, mOnePlusTwo evaluates to 0.30000000000000004 (although

it
displays as ".3"). Consequently, comparing that value to mThree (.3)

results
in false.


You need to round your numbers
no alternative
several ways to do this
you need to be careful
compare the two writes.

using System;

namespace trial1
{

class Class1
{

[STAThread]
static void Main(string[] args)
{
double dbl1 = .333333333333333;
double dbl2 = .4444444444444444;

double dbl3 = round(dbl1)+round(dbl2);

//Console.WriteLine(round(dbl1 + dbl2).ToString());

Console.WriteLine(round(dbl3).ToString());
}

static double round(double dbl)
{
//return Math.Round(dbl,2);
//return Convert.ToDouble(dbl.ToString("########.##"));
//return Convert.ToDouble(dbl.ToString("N"));
//return Convert.ToDouble( String.Format("{0:N}",dbl));
return Convert.ToDouble(String.Format("{0:F}",dbl));
}

}
}


Nov 17 '05 #8
Nick Malik [Microsoft] <ni*******@hotmail.nospam.com> wrote:
if you use the decimal datatype, you do not need to round the numbers


Unless you do anything which potentially leads to a result which can't
be accurately represented in decimal - like dividing 1 by 3, for
example.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #9
Hi !
Jon Skeet wrote:
[...snip...]
Unless you do anything which potentially leads to a result which can't
be accurately represented in decimal - like dividing 1 by 3, for
example.

[...snip...]

Do you know any .net maths library implementing a Fraction class to be able
to perform accurate fraction calculation ?
Nov 17 '05 #10
Michael Voss <mi********************@lvrREMOVE.CAPSde> wrote:
Jon Skeet wrote:
[...snip...]
Unless you do anything which potentially leads to a result which can't
be accurately represented in decimal - like dividing 1 by 3, for
example.

[...snip...]

Do you know any .net maths library implementing a Fraction class to be able
to perform accurate fraction calculation ?


I don't, I'm afraid - I'm pretty sure there's one out there, but I
can't remember the name.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Uma Abhyankar | last post by:
Hello, We are facing precision issues with addition, multiplication or division of "double" Issue1: ##### The output of 0.1 + 0.2 is not 0.3 It is 0.30000000000000004 How do we tackle this??
5
by: ajordan | last post by:
hello! i think i have a bug when i use parseFoat function: i have the following operation : parseFloat("10.664062") + parseFloat("10.664062") +parseFloat("10.664062"); the result of...
10
by: Andrew Thompson | last post by:
I only dipped back into the Javascript group again recently, after writing this document for regular reference in Java groups.. <http://www.physci.org/codes/sscce.jsp> In a nutshell, it calls...
17
by: Adam | last post by:
Hi! I've modified a javascript order form calculation script that tallies up the subtotal, shipping/handling, and total automatically. I did some modifications and it still worked fine, even...
4
by: tomamil | last post by:
i know this example is stupid and useless, but that's not the answer to my question. here it goes: status = 0.0 for i in range(10): status = status + 0.1 if status == 0.1: print status
7
by: Kürşat | last post by:
Hi, I have a strange problem about a simple sum operation on a double variable. There is a loop in my application like this : double _max = 0.4; double _step = 0.1; for (double d = 0.0; i...
6
Dormilich
by: Dormilich | last post by:
Hi, I’ve got a mathematical script, which is currently running fine*, although it bothers me that JS does not always increment correctly. sometimes it happens that the incremented value is...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.