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 <199710311718.JAA19082@wall.org>


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

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

1
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)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: alt.lang.asm
Date: Sat, 30 Apr 2022 16:25:37 -0700 (PDT)
In-Reply-To: <cca0e7f8-c99d-4f83-9a98-edb26f1509fcn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=98.47.8.185; posting-account=r_UqYQoAAADroB0Qe_EzH25J5Oyuce4G
NNTP-Posting-Host: 98.47.8.185
References: <cca0e7f8-c99d-4f83-9a98-edb26f1509fcn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <999a748e-3fe6-4436-bf27-f59018bc3621n@googlegroups.com>
Subject: Re: 0.0 / 0.0 = -NAN ?
From: alexfrun...@gmail.com (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.

Alex


1
rocksolid light 0.7.2
clearneti2ptor