summation
-
float a = (float)93.3; float b = (float)2.3; float c = a + b; // at this moment c = 95.60001; Why? I meet this strange behavior not only in C#, but also in Javascript, Actionscript and PHP.
What every computer scientist should know about floating point arithmatic[^]
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall
-
float a = (float)93.3; float b = (float)2.3; float c = a + b; // at this moment c = 95.60001; Why? I meet this strange behavior not only in C#, but also in Javascript, Actionscript and PHP.
in addition to Dan's link, did you know you can declare a float as a literal by suffixing with an 'f' and save the expense of casting from a double (or int).
float a = 93.3f;
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
float a = (float)93.3; float b = (float)2.3; float c = a + b; // at this moment c = 95.60001; Why? I meet this strange behavior not only in C#, but also in Javascript, Actionscript and PHP.
I try to make it short: If you compute float numbers, the computer uses "MachineNumbers" which are to a certain level adequate. So the computer has internal numbers which are taken when given a float to compute. e.g the Machinenumbers are 0.1, 0.2, 0.3, 0.4, 0.5, 1, 1.2, 1.5, 2, 2.4, 2.5 now you pass the number 1.2 -> computer takes 1.2 because it exists then you take 1.3 -> computer takes again 1.2 because it is the nearest "MachineNumber" you summ them and allthough there would be a "MachineNumber" 2.5 the computer computes 1.2 + 1.2 and you get the nearest "MachineNumber" which is 2.4 That's been very simplified, but I hope that helps
-
float a = (float)93.3; float b = (float)2.3; float c = a + b; // at this moment c = 95.60001; Why? I meet this strange behavior not only in C#, but also in Javascript, Actionscript and PHP.
If you want to do floating point math, I've found that using the decimal type is more precise. Try it this way:
decimal a = 93.3M;
decimal b = 2.3M;
decimal c = a + b;You should end up with precisely 95.6. If you need to, you can follow that up by casting the result to a float or double as need be.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
If you want to do floating point math, I've found that using the decimal type is more precise. Try it this way:
decimal a = 93.3M;
decimal b = 2.3M;
decimal c = a + b;You should end up with precisely 95.6. If you need to, you can follow that up by casting the result to a float or double as need be.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001The caveat to using the decimal type is that unlike floats it's not implemented in hardware directly and is significantly slower to use as result. This isn't a problem if you're doing light computations, but if you're doing larger amounts of calculation working within the limits of native floating points may be needed.
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall
-
The caveat to using the decimal type is that unlike floats it's not implemented in hardware directly and is significantly slower to use as result. This isn't a problem if you're doing light computations, but if you're doing larger amounts of calculation working within the limits of native floating points may be needed.
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall
To further and agree with your statement, decimal should only be used for financial or monetary values in .NET. At least that is my rule of thumb.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego. -
I try to make it short: If you compute float numbers, the computer uses "MachineNumbers" which are to a certain level adequate. So the computer has internal numbers which are taken when given a float to compute. e.g the Machinenumbers are 0.1, 0.2, 0.3, 0.4, 0.5, 1, 1.2, 1.5, 2, 2.4, 2.5 now you pass the number 1.2 -> computer takes 1.2 because it exists then you take 1.3 -> computer takes again 1.2 because it is the nearest "MachineNumber" you summ them and allthough there would be a "MachineNumber" 2.5 the computer computes 1.2 + 1.2 and you get the nearest "MachineNumber" which is 2.4 That's been very simplified, but I hope that helps
-
To further and agree with your statement, decimal should only be used for financial or monetary values in .NET. At least that is my rule of thumb.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.To really jump in on the good vibes agreeance bandwagon, if the 96.1000001 is annoying you, then you really should be using an appropriate format string when you display it to the user.
Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
Alpha release: Entanglar: Transparant multiplayer framework for .Net games. -
float a = (float)93.3; float b = (float)2.3; float c = a + b; // at this moment c = 95.60001; Why? I meet this strange behavior not only in C#, but also in Javascript, Actionscript and PHP.
Additionally to what the others already said, it's not safe to compare two float values like this:
if(float1 == float2)
Instead, for floats you should rather use something like this:
if((float1 - float2) < delta)
where
delta
can be a small number like 0.001. regards