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.
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
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.
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.
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?
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.
>When we are talking about Ruby, NaN means IEEE 754's NaN, isn't it?
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: