A Moore wrote:
Using floats, floor() returns wrong result, as shown in this program:

I assume you are doing this on an x86-linux machine, in which case this is the classic x86 excess-precision problem, as detailed in PR 323 in our bugzilla bug database.

The old x87 FP stack uses 80-bit precision to compute the divide result, and gets a number slightly less than 15, which only becomes 15 when rounded to float. If you pass the unrounded number to the floor() function, then you get 14 as the result from floor. This is very hard to fix in gcc without destroying FP performance. There is unfortunately no good solution, and it may never be fixed. There are various workarounds, such as using -ffloat-store.

x86_64-linux does not have this problem, as it avoids use of the old x87 FP stack, and instead uses SSE instructions for FP arithmetic which do not have this excess precision problem.
--
Jim Wilson, GNU Tools Support, http://www.specifix.com

Reply via email to