"nan".to_f?

Hi,

···

In message "Re: "nan".to_f ?" on Sat, 16 Oct 2004 05:54:36 +0900, furufuru@ccsr.u-tokyo.ac.jp (Ryo Furue) writes:

  x = 0.0/0.0 #=> NaN
  x.to_s #=> "NaN"

*then*

  y = "NaN".to_f # y *should* be NaN.

I agree that it's good to be so. The point is how I can implement
that in portable way. I'm waiting someone to enlighten me the way to
generate NaN portably, or that I can assume IEEE 754 for all the
platforms. Maybe Gotoken is saying the latter in [ruby-talk:116791],
but I'm not sure yet.

              matz.

That should have been:

       class String
            def to_f
                  Float(self) rescue 0.0/0.0
                  end
            end

...I really shouldn't post before caffeination, esp. after being up half
the night chasing my son's imaginary tigers out of the house.

-- Markus

···

On Wed, 2004-10-13 at 09:20, Markus wrote:

On Wed, 2004-10-13 at 05:38, Yukihiro Matsumoto wrote:
> Hi,
>
> In message "Re: "nan".to_f ?" > > on Wed, 13 Oct 2004 21:18:38 +0900, Markus <markus@reality.com> writes:
>
> >> It's not valid string representation of a float.
> >
> > Then why does:
> >
> >x = 0.0/0.0
> >x.class #Float
> >x.to_s #NaN
> >
> > If "NaN" is not the valid string representation of a Float, why
> >does calling Float#to_s return it?
>
> Although we can tell whether a float value is NaN by using isnan(),
> but as far as I know there's no portably way to generate NaN. I think
> it's not guaranteed that 0.0/0.0 generate NaN.
>
> If I'm wrong, feel free to correct me.

     No, so far as I know you are correct (at as far as math operations
go). There was another sub-thread on this topic regarding the
assumption that 0.0/0.0 could be PosInf, NegInf, Inf, or NaN.

     I believe there are defined binary representation of IEEE NaNs, so
one could be constructed. (See, for example

http://www.psc.edu/general/software/packages/ieee/ieee.html

), which I suspect is how the libs do it.

     If we could count on 0.0/0.0 to always produce NaN, I believe the
original poster's goal could be met by writing something like:

        class String
            def to_i
                 Integer(self) rescue 0.0/0.0
                 end
            end
        
...although this may not be as general as he needed.

-- Markus

Hi,

···

In message "Re: "nan".to_f ?" on Wed, 13 Oct 2004 23:33:53 +0900, GOTO Kentaro <gotoken@notwork.org> writes:

def aNaN
   s, e, m = rand(2), 2047, rand(2**52-1)+1
   [sprintf("%1b%011b%052b", s,e,m)].pack("B*").unpack("G").first
end

I believe this will generate NaN on environments where
pack/unpack works and NaN exists.

If the platform uses IEEE floating number, right?

              matz.

Hi,

In message "Re: "nan".to_f ?"

···

on Thu, 14 Oct 2004 02:39:43 +0900, Yukihiro Matsumoto wrote:

> def aNaN
> s, e, m = rand(2), 2047, rand(2**52-1)+1
> [sprintf("%1b%011b%052b", s,e,m)].pack("B*").unpack("G").first
> end
>
>I believe this will generate NaN on environments where
>pack/unpack works and NaN exists.

If the platform uses IEEE floating number, right?

Agreed! And if there exists NaN on a non IEEE platform,
that NaN must be another concept because NaN is defined in
IEEE 754.

Gotoken

My mistake is clear. I was assuming Float == IEEE.

     I still see the original poster's desire to be able to serialize
floats as text without gross changes, but do not at this point see a
clear and portable way of implementing it. At best (using the code I
posted yesterday morning) you could maintain the distinction between
error-values and numbers, but this might well silently morph (say) Inf
to NaN, etc.

        -- Markus

···

On Thu, 2004-10-14 at 09:44, GOTO Kentaro wrote:

Hi,

In message "Re: "nan".to_f ?"
    on Thu, 14 Oct 2004 02:39:43 +0900, Yukihiro Matsumoto wrote:
> > def aNaN
> > s, e, m = rand(2), 2047, rand(2**52-1)+1
> > [sprintf("%1b%011b%052b", s,e,m)].pack("B*").unpack("G").first
> > end
> >
> >I believe this will generate NaN on environments where
> >pack/unpack works and NaN exists.
>
> If the platform uses IEEE floating number, right?

Agreed! And if there exists NaN on a non IEEE platform,
that NaN must be another concept because NaN is defined in
IEEE 754.

Gotoken

In message Re: "nan".to_f ?
on Fri, 15 Oct 2004 03:05:16 +0900, Markus:

     My mistake is clear. I was assuming Float == IEEE.

Mistake? I don't think you mistook. or I also mistake.
When we are talking about Ruby, NaN means IEEE 754's NaN, isn't it? :slight_smile:

     I still see the original poster's desire to be able to serialize
floats as text without gross changes, but do not at this point see a
clear and portable way of implementing it. At best (using the code I
posted yesterday morning) you could maintain the distinction between
error-values and numbers, but this might well silently morph (say) Inf
to NaN, etc.

NaN was designed to represent abnormal result of an arithmetic operation.
So, IMHO, String#to_f is not arithmetic and should not returns NaN.
By the way, we sometimes want to define a method return NaN value.
In such situations I believe pack/unpack will help.

Gotoken

Hi,

···

In message "Re: "nan".to_f ?" on Sat, 16 Oct 2004 03:39:51 +0900, GOTO Kentaro <gotoken@notwork.org> writes:

     My mistake is clear. I was assuming Float == IEEE.

Mistake? I don't think you mistook. or I also mistake.
When we are talking about Ruby, NaN means IEEE 754's NaN, isn't it? :slight_smile:

C99 defines isnan() and isinf(). I'm not sure C99 assumes IEEE 754.

              matz.

Hi,

In message Re: "nan".to_f ?

>When we are talking about Ruby, NaN means IEEE 754's NaN, isn't it? :slight_smile:

C99 defines isnan() and isinf(). I'm not sure C99 assumes IEEE 754.

That is very IEEE 754 support.

ISO/IEC 9899:1999 Annex F says:

  F.1 Introduction

  This annex specifies C language support for the IEC 60559
  floating-point standard. The IEC 60559 floating-point standard is
  specifically Binary floating-point arithmetic for microprocessor
  systems, second edition (IEC 60559:1989), previously designated IEC
  559:1989 and as IEEE Standard for Binary Floating-Point Arithmetic
  (ANSI/IEEE 754-1985). ...

Gotoken

···

on Sat, 16 Oct 2004 03:44:14 +0900, Yukihiro Matsumoto wrote:

Hi,

···

In message "Re: "nan".to_f ?" on Sat, 16 Oct 2004 04:02:31 +0900, GOTO Kentaro <gotoken@notwork.org> writes:

That is very IEEE 754 support.

ISO/IEC 9899:1999 Annex F says:

<snip>

Does this mean that I can assume IEEE 754 on every platform?
If so, I'm glad that life is THAT simple.

              matz.