avr-gcc-list
[Top][All Lists]
Advanced

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

Re: [avr-gcc-list] [Bug] float: 0/0 =2


From: Bjarne Laursen
Subject: Re: [avr-gcc-list] [Bug] float: 0/0 =2
Date: Wed, 24 Sep 2003 10:01:52 +0200

At 16:08 23-09-2003, you wrote:
From: Bjarne Laursen <address@hidden>

I discovered another bug in the float libary:

    {
        volatile float a;
        volatile float b;
        volatile float c;
        a=0;
        b=0;
        c= b/a; // divide returns 0x40000000 (2.0)
    }

That's not technically a bug.  At least, not in avr-gcc.

According to n869 6.5.5p5, if the right-hand operand of either the "/" or "%" operators has the value zero, the behavior is undefined. Undefined behavior may include behavior you expect (perhaps +Inf or NaN), but it may include behavior you do not expect (such as 2.0 or nasal demons).

So I guess, to make safe portable c-code, one will have to check all operands before doing any calculations, so that NaN and Inf can be avoided.

If the float is received from an external source (network) it will be neassesary to check that it is a valid number [hopefully !(isnan(x)|isinf(x)) can do this], then the range should be checked to avoid overflows and divide by zero.

-Bjarne Laursen, RoseTechnology


reply via email to

[Prev in Thread] Current Thread [Next in Thread]