Easy Arithmetic question
-
double a = 212.45 ; double b = 6795.9 ; double c = a + b ; The result of c is 7008.3499999999995. I cannot understand why the result isn't 7008.35. Anyone can explain that to me :confused: ? Thx a lot
-
double a = 212.45 ; double b = 6795.9 ; double c = a + b ; The result of c is 7008.3499999999995. I cannot understand why the result isn't 7008.35. Anyone can explain that to me :confused: ? Thx a lot
Internal rounding error
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
-
Internal rounding error
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
What does that mean exactly ? Do you mean that .NET double type does rounding error ? No exception at all are thrown from framework. I resolved it using decimal type but I'd like to understand why this rounding error happens with Double :sigh:. Thx N0rthernlights
-
double a = 212.45 ; double b = 6795.9 ; double c = a + b ; The result of c is 7008.3499999999995. I cannot understand why the result isn't 7008.35. Anyone can explain that to me :confused: ? Thx a lot
The binary representation of fractional numbers is an approximation. Just as the fraction 1/3 must be approximated when represented in a decimal notation. I would pose a question, though. Since the two numbers you are adding have only 5 and 6 digits of significance, why are you expecting more than that when adding the two together. If you round the result to only be 6 significant digits, you will have the correct answer. The double type can only have 17 digits and once you begin operating on them, the accuracy usually falls down to only about 13 or 14 that are correct. Chris Meech We're more like a hobbiest in a Home Depot drooling at all the shiny power tools, rather than a craftsman that makes the chair to an exacting level of comfort by measuring the customer's butt. Marc Clifton VB is like a toolbox, in the hands of a craftsman, you can end up with some amazing stuff, but without the skills to use it right you end up with Homer Simpson's attempt at building a barbeque or his attempt at a Spice rack. Michael P. Butler
-
double a = 212.45 ; double b = 6795.9 ; double c = a + b ; The result of c is 7008.3499999999995. I cannot understand why the result isn't 7008.35. Anyone can explain that to me :confused: ? Thx a lot
-
double a = 212.45 ; double b = 6795.9 ; double c = a + b ; The result of c is 7008.3499999999995. I cannot understand why the result isn't 7008.35. Anyone can explain that to me :confused: ? Thx a lot
As far as i remember the double is accurate for 12 digits after the comma. And as Chris Meech mentionned, the internal represntation of fraction is not the same the one for decimals. Also if you Round your Number to 12 digits of precision (you have 13), you would get exactly 7008.350000000000
-
As far as i remember the double is accurate for 12 digits after the comma. And as Chris Meech mentionned, the internal represntation of fraction is not the same the one for decimals. Also if you Round your Number to 12 digits of precision (you have 13), you would get exactly 7008.350000000000
Thx all for your answers :-O. I now understand that internal fraction representation has some special behaviour. I'll use Decimal type because it's much easier to work with it to calculate money values.