kphmd's blog

By kphmd, 13 years ago, In English

I guess the case "int(100*0.94) == 93" may be like this process:


         0.939999....
       _______________________
100 )94
        900
        ____
         400
         300
         _____
         1000
           900
           _____
          1000
            900
            _____
            1000
            ... ...
  • Vote: I like it
  • +2
  • Vote: I do not like it

13 years ago, # |
  Vote: I like it 0 Vote: I do not like it
I'm afraid that's for any compliant platform.

I mean, obviously the infinite binary fractions are truncated at some point. (Yet I cannot explicitly state how their last digit is obtained, sometimes it must be rounded down).

If ater that multiplication does not bring rounding up and happy disentegration of error, the numeric error is after us to haunt our dreams. Especially when we round it down to integer (and C/C++ casts with rounding down). In this case we obtained double with value <94. which shall be (and is) rounded to 93.

Your basic idea, gratefully explained to us with decimal example is right.