Re: Floating point performance issue

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 23/12/11 21:25, Vincent Lefevre wrote:
On 2011-12-21 08:51:39 +0100, David Brown wrote:
But I expect that you know the details a lot better than me.  If the
compiler can guarantee consistent and expected results in cases like yours
involving simple assignments, then it would make sense to change the
"-Wfloat-equal" not to trigger in such situations.  After all, the point of
the warning is to help users avoid code that might not do what it seems to
do - if it /does/ do the expected thing, then there is no need of a warning.

Floating point is tricky. You can't ask the compiler to detect every
potential problems. That's not possible. Otherwise you would have too
many false positives (-Wfloat-equal being one of the causes).

For instance, according to Usenet posts in C and Perl groups, users
often regard something like

   double x = 0.1;

as exact. So, would you want the compiler to issue a warning every
time a constant that cannot be represented exactly is used? I'd say
no, even though the consequences can be disastrous[*]. Users should
learn how FP works instead of relying on the compiler to detect
their mistakes.


I agree that users need to understand floating point, its benefits and its restrictions. And compilers most certainly can't warn about everything. But when it /can/ warn you and help you, that's a good thing.

It is particularly useful when dealing with other people's code, of course. The "-Wfloat-equal" flag would almost never trigger a warning on my own code - I would not write code that triggers it. But it may catch a mistake I accidentally made, and it may help when handling other people's code.

[*] This example isn't much different from

   http://www.ima.umn.edu/~arnold/disasters/patriot.html

(where all calculations could have been done exactly, if the code
were better designed), which lead to 28 people killed.


This is an example where floating point should not have been used - no compiler can warn about such inappropriate use of tools and lack of understanding about the job in hand.

In defence of the programmer, however, it should be noted that the Patriots were never designed for intercepting Scuds, and there were a lot of reasons why they did a poor job of it (with issues in the software, electronics and mechanics).




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux