Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  nodelist  faq  login

Maybe it's time to break that. -- Larry Wall in <>

programming / alt.lang.asm / Re: 0.0 / 0.0 = -NAN ?

o Re: 0.0 / 0.0 = -NAN ?Alexei A. Frounze

Subject: Re: 0.0 / 0.0 = -NAN ?
From: Alexei A. Frounze
Newsgroups: alt.lang.asm
Date: Sat, 30 Apr 2022 23:25 UTC
References: 1
X-Received: by 2002:a05:6214:2349:b0:444:2a7b:cd5c with SMTP id hu9-20020a056214234900b004442a7bcd5cmr4671010qvb.77.1651361137794;
Sat, 30 Apr 2022 16:25:37 -0700 (PDT)
X-Received: by 2002:a05:6808:1924:b0:325:e838:6129 with SMTP id
bf36-20020a056808192400b00325e8386129mr645534oib.299.1651361137557; Sat, 30
Apr 2022 16:25:37 -0700 (PDT)
Newsgroups: alt.lang.asm
Date: Sat, 30 Apr 2022 16:25:37 -0700 (PDT)
In-Reply-To: <>
Injection-Info:; posting-host=; posting-account=r_UqYQoAAADroB0Qe_EzH25J5Oyuce4G
References: <>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <>
Subject: Re: 0.0 / 0.0 = -NAN ?
From: (Alexei A. Frounze)
Injection-Date: Sat, 30 Apr 2022 23:25:37 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 40
View all headers
On Saturday, April 30, 2022 at 8:31:04 AM UTC-7, Skybuck Flying wrote:
When exception masks are all enabled to stop the processor from throwing floating point exceptions the following calculation produces a somewhat strange result:

0.0 / 0.0 = -nan

Totally expected. 0 / 0 is undefined. This should've been covered in middle school.

(At least in Delphi).

For now I will assume this is the case in C/C++ as well and with that I mean on x86/x64 which should and seems to be following IEEE 754 floating-point format.

I am a little bit surprised by this and I want/need to know more. Where is this defined that 0.0 / 0.0 should be -NAN ?!?

Problem is with the code, example:

T := 0;
D := 0.0 / 0.0;
P := T * D;

This screws up P. instead of P being zero, P is now also -NAN ?!?

What you're seeing is on purpose and by design.
Suppose you had sqrt(-1e-30) in place of 0/0, that is, your
computations had some rounding errors and the square root
got a negative argument instead of zero or a small positive argument.
If we're staying away from complex values, square root of
negative argument is undefined as well.

But the CPU doesn't know that it may, for example, ignore this error
in the context of your formula and simply substitute 0 for sqrt(-1e-30).
So, instead it returns a NAN for sqrt(-1e-30) and poisons with it
all further computations by propagating the NAN all the way through
all operations to the final result.

This way the programmer may find out that their result had an
anomaly during computation and something needs to be done with it.

You can also think of it in terms of "how do I hide a boolean or
an if statement in a pure math formula/expression". This is how.


rocksolid light 0.7.2