On 2008-09-04 02:00:59 -0700, Brian Dessent wrote: > eool wrote: > > Basically, the reason given in the FAQ is that the result of > > cos(x) (intermediate, higher precision value) will be stored in a > > register and thus truncated. Then the result of cos(y) is directly > > compared with the truncated value located in a register, resulting > > in this strange (?) behavior. There could be other reasons in practice, e.g. the cos() function wasn't the same one. For instance, this can happen when one of them is evaluated at compilation time because the argument value was known. > > Now, my question is, why isn't gcc > > able to tell that it is about to compare two floating point > > numbers, one of which has been truncated, and then similarly Because it can't. You can have more complex expressions... > You can get this with -ffloat-store. But it has been told that it doesn't always have effects inside expressions. You also need to store every intermediate value to variables. > And in general floating point units do not work this way. It's only > the x87 that has this quirk of performing calculations with extended > precision. This is more a problem with operating systems (and API's) that enable this extended precision. With some codes, there could still be a problem with the extended exponent range, though. > And even with this oddball implementation, the behavior is > still optional: the precision is configurable. It's just that the > default is 80 bits on many operating systems because there are legacy > math routines that depend on the excess precision for numerical > stability. Only for math routines *specifically* written for the x87 configured in extended precision, i.e. non-portable across the processors and the operating systems, and not following the IEEE-754 standard (I mean here that the code won't work on all IEEE-754 implementations as extended precision, though allowed by the IEEE-754 standard, is just optional). So, this is a completely stupid argument for having extended precision by default. -- Vincent Lefèvre <vincent@xxxxxxxxxx> - Web: <http://www.vinc17.org/> 100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/> Work: CR INRIA - computer arithmetic / Arenaire project (LIP, ENS-Lyon)