[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

"nan".to_f ?

Thomas Fini Hansen

10/12/2004 9:15:00 AM

I ran into this difference:

---
irb(main):001:0> VERSION
"1.6.7"
irb(main):002:0> "nan".to_f
NaN
---
irb(main):001:0> VERSION
=> "1.8.2"
irb(main):002:0> "nan".to_f
=> 0.0
---

(1.8.2 is really 1.8.1+1.8.2pre2-1woody1 in Debian)

I wouldn't have been surprised if it was the other way round. Can
anyone explain why it is so? Seemed like a nice feature to me.

--
Thomas
beast@system-tnt.dk


28 Answers

Robo

10/12/2004 9:33:00 AM

0

Thomas Fini Hansen wrote:
> I ran into this difference:
>
> ---
> irb(main):001:0> VERSION
> "1.6.7"
> irb(main):002:0> "nan".to_f
> NaN
> ---
> irb(main):001:0> VERSION
> => "1.8.2"
> irb(main):002:0> "nan".to_f
> => 0.0
> ---
>
> (1.8.2 is really 1.8.1+1.8.2pre2-1woody1 in Debian)
>
> I wouldn't have been surprised if it was the other way round. Can
> anyone explain why it is so? Seemed like a nice feature to me.
>

Why would Ruby turn "nan" to "NaN" when you told it to convert the
string to a float? I don't see the connection.

Robo

gabriele renzi

10/12/2004 9:35:00 AM

0

Robo ha scritto:


> Why would Ruby turn "nan" to "NaN" when you told it to convert the
> string to a float? I don't see the connection.
>

NaN stands for Not a Number and is a 'builtin' value in floating point
math system (I mean, at the cpu level)

Stephan Kämper

10/12/2004 9:35:00 AM

0

Hi Thomas, hi Rubyists

Thomas Fini Hansen wrote:

> ---
> irb(main):001:0> VERSION
> "1.6.7"
> irb(main):002:0> "nan".to_f
> NaN
> ---
> irb(main):001:0> VERSION
> => "1.8.2"
> irb(main):002:0> "nan".to_f
> => 0.0
> ---
>
> (1.8.2 is really 1.8.1+1.8.2pre2-1woody1 in Debian)
>
> I wouldn't have been surprised if it was the other way round. Can
> anyone explain why it is so? Seemed like a nice feature to me.
>

>ri String#to_f
------------------------------------------------------------ String#to_f
str.to_f => float
------------------------------------------------------------------------
Returns the result of interpreting leading characters in _str_ as a
floating point number. Extraneous characters past the end of a
valid number are ignored. If there is not a valid number at the
start of _str_, +0.0+ is returned. This method never raises an
exception.

"123.45e1".to_f #=> 1234.5

"45.67 degrees".to_f #=> 45.67

"thx1138".to_f #=> 0.0

I don't use String#to_f a lot, because I'd prefer Float( a_string ).
Float() is stricter and raises an ArgumentError if the whole String
can't be interpreted as a Float, while String#to_s tries to create a
Float from the first characters and stops when/if there are any chars
that wouldn't yield a Float.

Happy rubying

Stephan

Markus

10/12/2004 4:15:00 PM

0


Yes, but his point is that "NaN" _is_ the string representation of
valid floating point value, defined and handled by the system, with
standardized semantics, etc.
"NaN".to_f should return NaN. How else are you going to reliably
get NaN if you need it?
IMO, this is a bug.

-- Markus


On Tue, 2004-10-12 at 02:39, Stephan Kämper wrote:
> Hi Thomas, hi Rubyists
>
> Thomas Fini Hansen wrote:
>
> > ---
> > irb(main):001:0> VERSION
> > "1.6.7"
> > irb(main):002:0> "nan".to_f
> > NaN
> > ---
> > irb(main):001:0> VERSION
> > => "1.8.2"
> > irb(main):002:0> "nan".to_f
> > => 0.0
> > ---
> >
> > (1.8.2 is really 1.8.1+1.8.2pre2-1woody1 in Debian)
> >
> > I wouldn't have been surprised if it was the other way round. Can
> > anyone explain why it is so? Seemed like a nice feature to me.
> >
>
> >ri String#to_f
> ------------------------------------------------------------ String#to_f
> str.to_f => float
> ------------------------------------------------------------------------
> Returns the result of interpreting leading characters in _str_ as a
> floating point number. Extraneous characters past the end of a
> valid number are ignored. If there is not a valid number at the
> start of _str_, +0.0+ is returned. This method never raises an
> exception.
>
> "123.45e1".to_f #=> 1234.5
>
> "45.67 degrees".to_f #=> 45.67
>
> "thx1138".to_f #=> 0.0
>
> I don't use String#to_f a lot, because I'd prefer Float( a_string ).
> Float() is stricter and raises an ArgumentError if the whole String
> can't be interpreted as a Float, while String#to_s tries to create a
> Float from the first characters and stops when/if there are any chars
> that wouldn't yield a Float.
>
> Happy rubying
>
> Stephan



Mikael Brockman

10/12/2004 4:22:00 PM

0

Markus <markus@reality.com> writes:

> Yes, but his point is that "NaN" _is_ the string representation of
> valid floating point value, defined and handled by the system, with
> standardized semantics, etc.
> "NaN".to_f should return NaN. How else are you going to reliably
> get NaN if you need it?

0.0/0.0? :-)



Stephan Kämper

10/12/2004 4:49:00 PM

0

Mikael Brockman wrote:
>> Yes, but his point is that "NaN" _is_ the string representation of
>>valid floating point value, defined and handled by the system, with
>>standardized semantics, etc.
>> "NaN".to_f should return NaN. How else are you going to reliably
>>get NaN if you need it?

When would you *need* NaN in the first place? To show the the result of
some calculation is NaN? The just return that calculation.

To test whether something is NaN? -> There's Float#nan? to do that.

>
> 0.0/0.0? :-)

Happy rubying

Stephan

Markus

10/12/2004 5:08:00 PM

0

On Tue, 2004-10-12 at 09:22, Mikael Brockman wrote:
> Markus <markus@reality.com> writes:
>
> > Yes, but his point is that "NaN" _is_ the string representation of
> > valid floating point value, defined and handled by the system, with
> > standardized semantics, etc.
> > "NaN".to_f should return NaN. How else are you going to reliably
> > get NaN if you need it?
>
> 0.0/0.0? :-)

I almost went off on an IEEE spiel until I saw the ":-)"

-- Markus





Mikael Brockman

10/12/2004 5:16:00 PM

0

Markus <markus@reality.com> writes:

> On Tue, 2004-10-12 at 09:22, Mikael Brockman wrote:
> > Markus <markus@reality.com> writes:
> >
> > > Yes, but his point is that "NaN" _is_ the string representation of
> > > valid floating point value, defined and handled by the system, with
> > > standardized semantics, etc.
> > > "NaN".to_f should return NaN. How else are you going to reliably
> > > get NaN if you need it?
> >
> > 0.0/0.0? :-)
>
> I almost went off on an IEEE spiel until I saw the ":-)"

Actually, I'm as ignorant as you first assumed. Feel free to spiel if
you want. :-)



Markus

10/12/2004 5:20:00 PM

0

> When would you *need* NaN in the first place? To show the the result of
> some calculation is NaN? The just return that calculation.

I don't have a specific example in mind. Perhaps you're writing
something that evaluates a parameterized infinite series and want to
return NaN in the case where it doesn't converge (this being
significantly faster than an infinite loop). Who knows?

There may be other, better examples, but the point is _someone_ has
to be able to generate NaN, at some level (else we'd never see it) and
so it is at least plausible that that someone might be one of us.

> To test whether something is NaN? -> There's Float#nan? to do that.

Good point.

-- Markus




Markus

10/12/2004 5:33:00 PM

0

On Tue, 2004-10-12 at 10:15, Mikael Brockman wrote:
> Markus <markus@reality.com> writes:
>
> > On Tue, 2004-10-12 at 09:22, Mikael Brockman wrote:
> > > Markus <markus@reality.com> writes:
> > >
> > > > Yes, but his point is that "NaN" _is_ the string representation of
> > > > valid floating point value, defined and handled by the system, with
> > > > standardized semantics, etc.
> > > > "NaN".to_f should return NaN. How else are you going to reliably
> > > > get NaN if you need it?
> > >
> > > 0.0/0.0? :-)
> >
> > I almost went off on an IEEE spiel until I saw the ":-)"
>
> Actually, I'm as ignorant as you first assumed. Feel free to spiel if
> you want. :-)

Short form: there are several NaN's, and "NaN like values" (e.g. +/-
infinity) and IIRC the form you gave could arguably return almost any of
them (except maybe -0.0).

-- Markus

http://www.freesoft.org/CIE/RFC/1...