"nan".to_f ?

G

GOTO Kentaro

Hi,

In message "Re: "nan".to_f ?"
| 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
 
M

Markus

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



Hi,

In message "Re: "nan".to_f ?"
| 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
 
G

GOTO Kentaro

In message Re: "nan".to_f ?
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.


Gotoken
 
Y

Yukihiro Matsumoto

Hi,

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

|> 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? :)

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

matz.
 
G

GOTO Kentaro

Hi,

In message Re: "nan".to_f ?
|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
 
R

Ryo Furue

Yukihiro Matsumoto said:
Hi,

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

|I would say that 'NaN' is a the string representation of a float, so
|logically it should be parsed to such, that's why I'm wondering why
|this behaviour was *removed*.

It's not valid string representation of a float.
[...]

Maybe I'm too late to jump in to this old thread
(three-days old is old enough for people to forget about
a thread :). Anyway, . . .

I think that symmetry is important, or if not important, it's nice
to have. So, *if*

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

*then*

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

In other words, I'd expect (x.to_s).to_f to be an identity
operation (apart from potential truncation associated with to_s)
for a Float x.

I'm not saying that 0.0/0.0 *should* evaluate to NaN. That depends
on the platform. What I'm saying is that whatever x.to_s evaluates
to, (x.to_s).to_f "should" recover the (more-or-less) original value.
By "should", I mean "I wish".

Same goes for "Inf" and "-Inf".

Cheers,
Ryo
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: "nan".to_f ?"
on Sat, 16 Oct 2004 05:54:36 +0900, (e-mail address removed)-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.
 
Y

Yukihiro Matsumoto

Hi,

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

|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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top