I am beginner in C# and I am working with floating point numbers. I need to do subtraction between these two numbers but it does not work. I know it is caused by floating point number, but how can I fix it please and if you be so good can you explain me why is it happening? Thanks in advance.
-
What you want and what is your problem, please elaborate youe question..? – Vishal Suthar Mar 27 '12 at 13:30
-
3Post your code please. Are all the variables floats? – C0D3 Mar 27 '12 at 13:30
-
4Can you post code? The calculation in your topic looks very strange. Normal floating point rounding doesn't cause such behavior. – CodesInChaos Mar 27 '12 at 13:30
-
2Some code demonstrating the problem would help. – Joachim Isaksson Mar 27 '12 at 13:30
-
1Post the full code, even with rounding errors and floating point stuffs you can't get that result from that inputs. – Adriano Repetti Mar 27 '12 at 13:30
-
1what exactly are you trying to do, can you post your code snippet? – Jason Mar 27 '12 at 13:30
-
8Are you sure the answer is 0.000000000000001 and not 0.345000000000001? The latter I can explain! – Rob Levine Mar 27 '12 at 13:33
-
1Title has been edited. Rob was correct. – GazTheDestroyer Mar 27 '12 at 13:41
-
1For your new question, the simple answer is using `decimal` instead of float. – CodesInChaos Mar 27 '12 at 13:46
-
possible duplicate of [Why does (int)(33.46639 * 1000000) return 33466389?](http://stackoverflow.com/questions/2422027/why-does-int33-46639-1000000-return-33466389) – dan04 Mar 27 '12 at 18:46
5 Answers
Consider using decimal instead of float:
// Instead of this...
float a = 12.345F;
float b = 12;
float c = a - b;
// Use this:
decimal d = 12.345M;
decimal e = 12;
decimal f = d - e;
Jon Skeet gives a good explanation of the differences between both types in this answer: https://stackoverflow.com/a/618596/446681

- 1
- 1

- 26,290
- 8
- 57
- 73
-
+1: if you need any kind of numeric fidelity, `decimal` is the way to go. – code4life Mar 27 '12 at 14:00
-
Decimal is what I was looking for, thanks a lot. I wanna thank also to people who explained why it was happening. – c0ntrol Mar 27 '12 at 15:11
-
Depends on what the numbers are for. You should always use `decimal` for money. But *not* for physics simulations. – dan04 Mar 27 '12 at 18:48
-
Even if the exact same problem will not happen with `decimal`, it is still true that if you subtract the most significant part (such as the integral part) of a number from that number, the precision of the result is limited. For example let `decimal g = 1000000000000000000m / 7m; decimal h = 142857142857142857m; decimal i = g - h;`, one could ask: Why is my result `i` only precise up to 11 digits; I thought a `decimal` had a precision of 28 to 29 digits? So precision is still lost with `decimal`. – Jeppe Stig Nielsen Apr 29 '13 at 18:33
This is not a c# problem, this is a computer science problem. If you want to truly understand what is going on, read What Every Computer Scientist Should Know About Floating-Point Arithmetic. If you just care about why you're having the problem, it's because Float and Double are only precise to 7 and 15 digits respectively on this platform, and you need to apply rounding logic to achieve the result you are looking for.
Squeezing infinitely many real numbers into a finite number of bits requires an approximate representation. Although there are infinitely many integers, in most programs the result of integer computations can be stored in 32 bits. In contrast, given any fixed number of bits, most calculations with real numbers will produce quantities that cannot be exactly represented using that many bits. Therefore the result of a floating-point calculation must often be rounded in order to fit back into its finite representation. This rounding error is the characteristic feature of floating-point computation. Goldberg 1991

- 1,924
- 14
- 16
-
Good to find someone, who says that this is an obvious problem, and moreover this is definitely a bug (issue) in programming, which hasnt been solved. even my old calculator works well, and it's 2019 and we are still having headaches in PC. – T.Todua Feb 21 '19 at 11:21
How exactly are you calculating?
float a = 12.35F;
float b = 12.0F;
float ans = a - b; //0.350000381
double x = 12.35;
double y = 12.0;
double ans2 = x - y; //0.34999999999999964
decimal n = 12.35m;
decimal m = 12.0m;
decimal ans3 = n - m; //0.35
For me these calculations give the correct results.

- 26,772
- 8
- 53
- 86
Depending in what you you can use either decimal type, or store it as is, but round before displaying the answer

- 11,948
- 11
- 47
- 68