in reply to Numeric Comparisons Randomly Giving the Wrong Result

Read the replies to When 1000 is less than 1000.

The short version is that floating point values can not precisely represent decimal fractions like 0.1. The result is that small errors are present whenever you perform arithmetic in computers using floating point representations of decimal fractions. Those errors are often not shown because they are small, but lead to the sort of strange results you have seen.

If you are dealing with monetary values you can avoid the problem by performing all calculations in, for example, cents then convert to dollars and cents for display. Even then, if you perform mark up, discount, interest, tax or other calculations you will have to take care to round results at an appropriate point and always compare integer cents.

True laziness is hard work
  • Comment on Re: Numeric Comparisons Randomly Giving the Wrong Result