Live data from Hacker News

GNU GCC does not round floating-point divisions to the nearest value

lemire.me

1–10 of 87 posts

Re: GNU GCC does not round floating-point divisions to the nearest value

#3
Learning C recently, I ran into confusion around FLT_EVAL_METHOD as my dirty (not recommended) trick for getting machine epsilon didn't work (7.0/3.0 - 4.0/3.0 - 1.0). With strict IEEE 754 floating points it works fine (Python, Julia, Go all work as expected). But gcc, with c11 flags at least, performed the computation using the 80 bit upsampling mentioned in this post. Caused some head-scratching initially.

Edit: Also, curiously, things worked as expected for me using -std=gnu11. This was a fairly old version of GCC though, things might have changed.

Re: GNU GCC does not round floating-point divisions to the nearest value

#5
post #2

This seems like a property of the Intel 8087 math coprocessor and its distant descendants found in x86 chips, not anything to do with gcc?

My understanding is that in this case it's GCC's optimizer evaluating the expression at compile time in a way that gives a different result to if it's done at runtime.

EDIT: GCC gets it wrong with -O0 (when it's evaluated at runtime) and right with -O2 (when it evaluates it at compile time)

Clang appears to get it right when evaluated at compile time or runtime.

Clang uses the divsd instruction; GCC uses the fdiv instruction – so this really is SSE vs x87 FPU.

Re: GNU GCC does not round floating-point divisions to the nearest value

#6
post #4
post #2

This seems like a property of the Intel 8087 math coprocessor and its distant descendants found in x86 chips, not anything to do with gcc?

But then why does python and JavaScript on the same machines get it right?

Unsure: compiled to use SSE instead of x87 for fpmath?

Re: GNU GCC does not round floating-point divisions to the nearest value

#7
post #4
post #2

This seems like a property of the Intel 8087 math coprocessor and its distant descendants found in x86 chips, not anything to do with gcc?

But then why does python and JavaScript on the same machines get it right?

Because it's now getting exceedingly rare to find a program that was not compiled with at least SSE instructions enabled. So the x87 FPU is simply not used then.
Post reply on HN