Floating point underflow

Floating point underflow

Post by Gerhard Wes » Wed, 14 Aug 2002 00:17:35



Hi NG,

Is it implementation dependent whether an exception is raised for
floating point errors?  Specifically, I'd like to round the result of a
division to zero if it is less than the minimal positive double value,
i.e. 1e-320/10 == 0.

On Intel it appears to work this way, but on Alphas the program dumps
core with a Floating Exception.

What does the standard mandate? (Don't have a copy handy at the moment.)

Regards,
-Gerhard
--
| voice: +43 (0)676 6253725  ***  web: http://www.cosy.sbg.ac.at/~gwesp
|
| Passts auf, seid's vuasichdig, und lossds eich nix gfoin!
|  -- Dr. Kurt Ostbahn


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Floating point underflow

Post by Charles Vill » Wed, 14 Aug 2002 12:45:34


Quote:> Is it implementation dependent whether an exception is raised for
> floating point errors?  Specifically, I'd like to round the result of a
> division to zero if it is less than the minimal positive double value,
> i.e. 1e-320/10 == 0.

> On Intel it appears to work this way, but on Alphas the program dumps
> core with a Floating Exception.

> What does the standard mandate? (Don't have a copy handy at the moment.)

The standard says it is undefined.  Sec 5.0.5

<quote>
If during the evaluation of an expression, the result is not mathematically
defined or not in the range of rep-resentable values for its type, the
behavior is undefined, unless such an expression is a constant expression
(5.19), in which case the program is ill-formed. [Note: most existing
implementations of C++ ignore integer overflows. Treatment of division by
zero, forming a remainder using a zero divisor, and all floating point
exceptions vary among machines, and is usually adjustable by a library
function. ]
</quote>

Charles Villa


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Floating point underflow

Post by Rob » Thu, 15 Aug 2002 00:55:24



 > Hi NG,
 >
 > Is it implementation dependent whether an exception is raised for
 > floating point errors?  Specifically, I'd like to round the result of a
 > division to zero if it is less than the minimal positive double value,
 > i.e. 1e-320/10 == 0.
 >
 > On Intel it appears to work this way, but on Alphas the program dumps
 > core with a Floating Exception.
 >
 > What does the standard mandate? (Don't have a copy handy at the moment.)
 >

In this case (what happens with events like underflow of floating point)
the C++ standard does not mandate anything.   IIRC, the evaluation of
any expression is formally undefined if the result is not mathematically
defined or not in the range of values that can be represented by its type.
A floating point underflow is an example of a result not being in a range
of values that can be represented by its type so .....


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Floating point underflow

Post by Bill Wad » Sat, 17 Aug 2002 14:04:07



Quote:> > Is it implementation dependent whether an exception is raised for
> > floating point errors?  Specifically, I'd like to round the result of a
> > division to zero if it is less than the minimal positive double value,
> > i.e. 1e-320/10 == 0.

> > On Intel it appears to work this way, but on Alphas the program dumps
> > core with a Floating Exception.

> > What does the standard mandate? (Don't have a copy handy at the moment.)
> The standard says it is undefined.  Sec 5.0.5

Is the range from max-double to min-double, or is it just the precisely
representable values?  If the latter, then 1./3. is undefined according to
the standard.  If the former, then underflow is not undefined.


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Floating point underflow

Post by Rob » Sat, 17 Aug 2002 22:10:31



 >

 > > > Is it implementation dependent whether an exception is raised for
 > > > floating point errors?  Specifically, I'd like to round the result of
a
 > > > division to zero if it is less than the minimal positive double value,
 > > > i.e. 1e-320/10 == 0.
 > > >
 > > > On Intel it appears to work this way, but on Alphas the program dumps
 > > > core with a Floating Exception.
 > > >
 > > > What does the standard mandate? (Don't have a copy handy at the
moment.)
 > > The standard says it is undefined.  Sec 5.0.5
 >
 > Is the range from max-double to min-double, or is it just the precisely
 > representable values?  If the latter, then 1./3. is undefined according to
 > the standard.  If the former, then underflow is not undefined.
 >

You're reaching with these statements.   The result of operations is
undefined
if not mathematically defined or not IN THE RANGE of representable values.

1./3.  is within the range that can be represented in most floating point
variables.
That does not mean it can (or is required to) be represented exactly.


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Floating point underflow

Post by Bill Wad » Mon, 19 Aug 2002 20:31:57



 > You're reaching with these statements.   The result of operations is
 > undefined
 > if not mathematically defined or not IN THE RANGE of representable values.

If "THE RANGE" is everything between DBL_MIN and DBL_MAX, then underflow is
not be UB.


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Floating point underflow

Post by Gerhard Wes » Tue, 20 Aug 2002 19:43:16


{This thread is drifting off topic, please be careful with followups.
-mod/fwg}

 > if not mathematically defined or not IN THE RANGE of representable values.

Please define ``THE RANGE''.

As a mathematician, I'd be very inclined to think of it simply as the set of
representable values.  Which means that 1./3. would not be in ``THE
RANGE''.

One could perhaps also define ``THE RANGE'' as

   conv P union 0 union conv N,

where P and N are the representable positive and negative values, resp,
and conv is the convex hull.

Then, an underflow would not be in ``THE RANGE'', but 1./3. would.

I venture to claim that this proposed definition is somewhat counter-
intuitive for mathematicians, but natural for computer numericists.

Kind regards,
-Gerhard


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Floating point underflow

Post by James Kan » Wed, 21 Aug 2002 03:29:41




>  > You're reaching with these statements.  The result of operations is
>  > undefined if not mathematically defined or not IN THE RANGE of
>  > representable values.
> If "THE RANGE" is everything between DBL_MIN and DBL_MAX, then
> underflow is not be UB.

If "THE RANGE" is everything between DBL_MIN and DBL_MAX, then negative
results or zeor are not defined.  The only possible interpretation of
"the range" is -DBL_MAX ... DBL_MAX.

--

Conseils en informatique oriente objet/
                    Beratung in objektorientierter Datenverarbeitung


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Floating point underflow

Post by Gerhard Wes » Wed, 21 Aug 2002 20:45:42


 > If "THE RANGE" is everything between DBL_MIN and DBL_MAX, then negative
 > results or zeor are not defined.  The only possible interpretation of
 > "the range" is -DBL_MAX ... DBL_MAX.

I take it you mean every real number between -DBL_MAX and DBL_MAX.

In this case, no underflow can occur and my implementation would be in
error.

To the moderators:  IMHO, this is still very much on-topic, being about
the correct interpretation of this certain phrase in the standard!

Regards,
-Gerhard
--
| voice: +43 (0)676 6253725  ***  web: http://www.cosy.sbg.ac.at/~gwesp
|
| Passts auf, seid's vuasichdig, und lossds eich nix gfoin!
|  -- Dr. Kurt Ostbahn


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Floating point underflow

Post by Bill Wad » Wed, 21 Aug 2002 20:50:31



 > One could perhaps also define ``THE RANGE'' as
 >
 >    conv P union 0 union conv N,
 >
 > where P and N are the representable positive and negative values, resp,
 > and conv is the convex hull.

Why not just the convex hull of representable numbers?


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Floating point underflow

Post by James Kan » Thu, 22 Aug 2002 22:06:40




>  > If "THE RANGE" is everything between DBL_MIN and DBL_MAX, then
>  > negative results or zeor are not defined.  The only possible
>  > interpretation of "the range" is -DBL_MAX ... DBL_MAX.
> I take it you mean every real number between -DBL_MAX and DBL_MAX.

Correct.

Quote:> In this case, no underflow can occur and my implementation would be in
> error.

Correct:-).

My statement was intentionally provocative, because I think that the
wording in the standard is somewhat unclear.  I think that the *intent*
was to let the implementation do what it likes in the case of
underflow.  But I don't think that the standard says this clearly, and
the most na?ve interpretation of "range" (the one I gave above) would
suggest that an implementation must treat underflow as 0 (since that is
the nearest representation available).

Quote:> To the moderators: IMHO, this is still very much on-topic, being about
> the correct interpretation of this certain phrase in the standard!

Of course it is on topic.  Independantly of the standard, general
discussions about behavior that is possible, or that you might encounter
somewhere, is on topic.  About the only way this would slip off topic is
if you start talking about an implementation specific way to modify this
behavior, and even then, if you word it correctly... "Most
implementations will have some way of ... For example, under Windows,
you can ..."

--

Conseils en informatique oriente objet/
                    Beratung in objektorientierter Datenverarbeitung


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

1. Q: how to detect a overflow (or underflow) floating point calculation?

Hi,

I want to detect an overflow (or underfolow) floating point
calculation.  Do you know how?

I figure out a way below but it just partly works (only for
SW_ZERODIVIDE events). In the code below, d2 is apparently overflow.
But ui gets a value of zero instead of _SW_OVERFLOW (0x00000004) when
I debug the code.  If I change d1 to 0., then ui is _SW_ZERODIVIDE
(0x00000008) as desired.  Do you know why? Thanks for any advise.
Tony

_clearfp();
double d1 = 100.;
double d2 = DBL_MAX * d1;
unsigned int ui = _statusfp();
if (ui > 1)
{
  // handle the error
  .....

2. Steps for Windows Authentication?

3. Debug problem when step debug float point instruction that raise an float point exception

4. asynchronous sprites : sprite server?

5. Returing a float pointer that points to an array of floats

6. OE blocking attachments

7. Windows, Floating Point, and the IBM Value Point

8. Plus98 Compressed Folders problem

9. Fixed-point or floating point for RTS

10. int/float/double overflow / underflow

11. Floating point coords in GDI+?

12. Hooks and MMX and floating point?