www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4726] New: writeln(0.0 / 0.0) prints -nan

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4726

           Summary: writeln(0.0 / 0.0) prints -nan
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2010-08-25 19:22:39 PDT ---
This program prints (dmd 2.048):
-nan
But I expect:
nan


import std.stdio: writeln;
void main() {
    writeln(0.0 / 0.0);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 25 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4726


David Simcha <dsimcha yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |dsimcha yahoo.com
         Resolution|                            |INVALID


--- Comment #1 from David Simcha <dsimcha yahoo.com> 2010-08-25 20:35:37 PDT ---
This is the correct behavior.  For whatever reason x86 CPUs create a NaN with
the sign bit set to 1 when they get a 0.0 / 0.0.  writeln() just displays the
sign bit of the NaN because it gives the programmer more information about how
the NaN was triggered.  The following code demonstrates that the sign bit is
set to 1.

import std.stdio;

void main() {
    double myNan = 0.0 / 0.0;
    ulong asInt = *(cast(ulong*) &myNan); 
    writeln(asInt & (1UL << 63));  // Prints some huge number.
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 25 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4726



--- Comment #2 from bearophile_hugs eml.cc 2010-08-26 00:50:46 PDT ---
OK. Thank you for your answer. I will not reopen this bug because it's a minor
thing, but I don't like it because:

From a purely ideal point of view, a NaN isn't a number, so it can't be
positive or negative, it's "undefined", that is not negative.

In 0.0/0.0 both values are positive, so if you extend the semantics of division
between two positive real numbers, the result can't be negative.

And because no other language I know of (including D printf) seems to print a
"negative nan" in that situation:

-------------------

In D (2.048) if you run this program:

import std.stdio;
void main() {
  printf("%f\n", 0.0 / 0.0);
}


It prints "nan".

-------------------

This D1 program (dmd 1.026):

import std.stdio;
void main() {
  writefln("%f", 0.0 / 0.0);
}


Prints "nan".

-------------------

In C if you run this program:

#include "stdio.h"
int main() {
  printf("%f\n", 0.0 / 0.0);
  return 0;
}

It prints "nan".

-------------------

In Scala language, this program:

import java.io.{BufferedReader, InputStreamReader}

object Main {
  def main(args: Array[String]) {
      System.out.println(0.0 / 0.0);
  }
}

Prints "NaN".

-------------------

In Haskell (that is a quite mathematical-oriented language), this program:

main = do
   putStr  (show (0.0 / 0.0))


Prints "NaN".

-------------------

In F#, this program:

open System
do
    System.Console.Write(0.0 / 0.0)

Prints "NaN".

-------------------

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 26 2010