Floating-point arithmetic is designed to approximate real-number arithmetic. A binary-based floating-point format represents a finite number as some integer scaled by a power of two (with limits on the ranges of the integer and the power).
log(243) and log(3) have irrational values and so cannot be represented as an integer scaled by a power of two. The closest value to log(243) that is representable in the format commonly used for double
(IEEE-754 binary64) is 5.49306144334054824440727315959520637989044189453125, which is 6,184,637,367,337,933•2−50. The closest value to log(3) that is representable is 1.0986122886681097821082175869378261268138885498046875, which is 4,947,709,893,870,347•2−52.
When these two numbers are divided in the double
format, the result is 4.99999999999999911182158029987476766109466552734375. Obviously, that is not equal to its ceiling, 5, so your test routine returns false.
The rounding errors that occur in floating-point arithmetic vary, so using log10
instead of log
may happen to get rounding errors in the log10
and the division that cancel, resulting in a computed quotient of 5. Also, the library functions like log
are implemented with varying quality—they might not always return the closest representable value.
Generally, you should avoid using floating-point arithmetic for tests such as this. Redesign your code to use integer arithmetic.