2

It's Unity Engine(v2018.1) C#, .Net 3.5 equivalent version.
I got a strange floating point number operation.
The code goes like this.

        float   fa = 4.2f;
        float   fb = 10.0f;
        float   f1 = fa * fb;
        int     i1 = (int)(fa * fb);
        int     i2 = (int)f1;
        int     i3 = (int)42.0f;

        Debug.Log(f1);
        Debug.Log(i1);
        Debug.Log(i2);
        Debug.Log(i3);

        // Log Result
        //
        //  f1 : 42
        //  i1 : 41
        //  i2 : 42
        //  i3 : 42
        //

i1 is the problem.
Integer type casting gives 41 not 42.

Anybody can give hint about this issue? FP precision or type casting problem?

Jinbom Heo
  • 7,248
  • 14
  • 52
  • 59

1 Answers1

2

4.2 cannot be exactly represented in binary.

It's binary equivalent of 1/3 (0.3333333333...) which cannot be exactly represented in decimal.

You can check this by trying to convert 4.2 into binary here

(Make sure you check single precision) In the result if you see inexact checked, it means it cannot be precisely represented in binary.

4.2 becomes 100.00110011001100110011 which in decimal is 4.19999980926513671875

Casting to int just removes decimal part. So 41.9999980926513671875 when casted to int becomes 41.

You should not really be casting to an int especially with floating point values to avoid unexpected results like this. Just do a rounding instead.

v1p3r
  • 677
  • 7
  • 13
  • Great, thanks v1p3r. I understood why i1 is 41. if you don't mind I want to ask one more question. Why i2 is 42, not 41. if it's the same with i1's case, I think it should be 41 too. – Jinbom Heo Jun 19 '18 at 08:44
  • I got the answer here. https://stackoverflow.com/questions/50966599/floating-point-number-rounded-automatically – Jinbom Heo Jun 22 '18 at 01:10