Human-readable form of NaN



  • As many of us know, Java is one of those wtfs that just keeps on giving.  I've been coding primarily in Java for many years now and there are always new wtfs to be found.  Here's one that I ran into the other day.

    I was trying to print out the value of a double after it was converted to a string in our log to do some debugging.  The code was using a DecimalFormat to format the number.  I ran the code a couple times and for some reason the log kept showing me an "invalid" unicode character instead of the number, � (\uFFFD). I ended up also printing out the double using Double.toString(), and sure enough, it prints a nice little ASCII "NaN".  Oh, I see.  It's not some unicode problem on my computer, but NaN.

    So apparently while toString() prints something useful out, DecimalFormat tries to be cute and print unicode characters for infinity and NaN.  Only, there doesn't appear to be a unicode character for NaN, so they used the Replacement Character, which according to Wikipedia is used for unprintable characters.

    Seriously?  How is that at all helpful to a user?

    A secondary wtf is that there are unicode characters for just about every dumb thing you can imagine... all sorts of smileys, icons, you name it... but no little NaN icon! (That I could find.)




  • @NatmanZ8 said:

    I was trying to print out the value of a double after it was converted to a string in our log to do some debugging.  The code was using a DecimalFormat to format the number.  I ran the code a couple times and for some reason the log kept showing me an "invalid" unicode character instead of the number, � (\uFFFD). I ended up also printing out the double using Double.toString(), and sure enough, it prints a nice little ASCII "NaN".  Oh, I see.  It's not some unicode problem on my computer, but NaN.

    So apparently while toString() prints something useful out, DecimalFormat tries to be cute and print unicode characters for infinity and NaN.  Only, there doesn't appear to be a unicode character for NaN, so they used the Replacement Character, which according to Wikipedia is used for unprintable characters.

    Seriously?  How is that at all helpful to a user?

    Not terribly I guess, but if you'd prefer something other than the default, why not just set it using getDecimalFormatSymbols().setNaN()?




  • trwtf is creating NaN



  • @_leonardo_ said:

    trwtf is creating NaN

    I wonder if Indians ever get hungry when working with floating point numbers.



  • @morbiuswilters said:

    @_leonardo_ said:

    trwtf is creating NaN

    I wonder if Indians ever get hungry when working with floating point numbers.

    I wonder if programmers in New Zealand laugh themselves into a fit when they have to deal with Directed Acyclic Graphs.

    (And I still wonder sometimes if Rich Little and Little Richard have to get together periodically to exchange misdelivered fan mail.)

     



  • @da Doctah said:

    I wonder if programmers in New Zealand laugh themselves into a fit when they have to deal with Directed Acyclic Graphs.
    I don't get this, but I often wonder if Xzibit puts Directed Acyclic Weighted Graphs in his Directed Acyclic Weighted Graphs.



  • @Zecc said:

    @da Doctah said:

    I wonder if programmers in New Zealand laugh themselves into a fit when they have to deal with Directed Acyclic Graphs.
    I don't get this, but I often wonder if Xzibit puts Directed Acyclic Weighted Graphs in his Directed Acyclic Weighted Graphs.

    I have no idea what the fuck you're talking about, but I suddenly really miss Flight of the Conchords.



  •  @Zecc said:

    @da Doctah said:

    I wonder if programmers in New Zealand laugh themselves into a fit when they have to deal with Directed Acyclic Graphs.
    I don't get this, but I often wonder if Xzibit puts Directed Acyclic Weighted Graphs in his Directed Acyclic Weighted Graphs.

     

    DAGs in IT:


    DAGs in NZ:




  • @NatmanZ8 said:

    � (\uFFFD) [...] the Replacement Character, which according to Wikipedia is used for unprintable characters.

    U+FFFD is not used for unprintable characters, but for invalid characters. That is when the string is not unicode at all. Merely unprintable characters get simply displayed as empty boxes.

    Since Java java.lang.String is unicode, it is pretty difficult for it to produce invalid character. So either somebody has set that character explicitly (WTF) or somebody explicitly set something outside of the unicode range (0x0–0xd7ff ∪ 0xe000–0xfffd ∪ 0x10000–0x10ffff — yes, that is TRWTF) (even bigger WTF I guess). Or your locale is badly broken, but converting to legacy charset and back produces plain old ascii ?s, not s.



  • @Bulb said:

    So either somebody has set that character explicitly (WTF) [...]
    That's exactly what it is. I just checked it, because I couldn't believe it, but apparently the standard Java locale (I tried it with Locale.ENGLISH, for reference) in fact does explicitly set the character representing NaN to U+FFFD.



  • @Anonymouse said:

    @Bulb said:

    So either somebody has set that character explicitly (WTF) [...]
    That's exactly what it is. I just checked it, because I couldn't believe it, but apparently the standard Java locale (I tried it with Locale.ENGLISH, for reference) in fact does explicitly set the character representing NaN to U+FFFD.

    As documented: http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html, in the "Special Values" section.  See also http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormatSymbols.html.



  • ♿ (Parody)

    @Anonymouse said:

    @Bulb said:

    So either somebody has set that character explicitly (WTF) [...]
    That's exactly what it is. I just checked it, because I couldn't believe it, but apparently the standard Java locale (I tried it with Locale.ENGLISH, for reference) in fact does explicitly set the character representing NaN to U+FFFD.

    I don't get the big deal. Principle of least surprise and all that. Not a Number prints as Not a Character.



  • @boomzilla said:

    @Anonymouse said:

    @Bulb said:

    So either somebody has set that character explicitly (WTF) [...]
    That's exactly what it is. I just checked it, because I couldn't believe it, but apparently the standard Java locale (I tried it with Locale.ENGLISH, for reference) in fact does explicitly set the character representing NaN to U+FFFD.

    I don't get the big deal. Principle of least surprise and all that. Not a Number prints as Not a Character.

    You have a NaC for stating the obvious.



  • @boomzilla said:

    Not a Number prints as Not a Character.
     

    O_O



  • @dhromed said:

    @boomzilla said:

    Not a Number prints as Not a Character.
     

    O_O

    Oh no, your bicycle flipped over again!


  • ♿ (Parody)

    @morbiuswilters said:

    You have a NaCl for stating the obvious.

    Mmmmm.....salt....



  • @morbiuswilters said:

    @dhromed said:

    @boomzilla said:

    Not a Number prints as Not a Character.
     

    O_O

    Oh no, your bicycle flipped over again!

     

     

      _
    O O

     fixed!

    ( ^ ∪^)/

     



  • @dhromed said:

    \( ^ ∪^)/

    Well, you fixed one thing and broke another. Now your hovercraft full of eels is upside down.

    /( ᵥ ᐢᵥ)\

    FTFY.



  • @Faxmachinen said:

    @dhromed said:

    \( ^ ∪^)/

    Well, you fixed one thing and broke another. Now your hovercraft full of eels is upside down.

    /( ᵥ ᐢᵥ)\

    FTFY.

     

    You're doing so much drugs I can't even.

     



  • @dhromed said:

    @Faxmachinen said:

    @dhromed said:

    \( ^ ∪^)/

    Well, you fixed one thing and broke another. Now your hovercraft full of eels is upside down.

    /( ᵥ ᐢᵥ)\

    FTFY.

     

    You're doing so much drugs I can't even.

    He accidentally the entire bag of drugs.


Log in to reply