14

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.

dan04
  • 87,747
  • 23
  • 163
  • 198
c0ntrol
  • 908
  • 2
  • 9
  • 14

5 Answers5

15

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

Community
  • 1
  • 1
Hector Correa
  • 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
8

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.

Float C# reference

Double C# reference

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

Mike
  • 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
6

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.

Bas
  • 26,772
  • 8
  • 53
  • 86
1

remember that the behavior of floating points can vary depending on the processor you are using.

Here is an question on this forum that deals with the subject

If you really want to dig into the subject, here is a good source on how to examine the behavior of floating-point

Community
  • 1
  • 1
Diego
  • 34,802
  • 21
  • 91
  • 134
0

Depending in what you you can use either decimal type, or store it as is, but round before displaying the answer

Uriil
  • 11,948
  • 11
  • 47
  • 68