[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Math.sqrt(NaN) behaves differently on windows and linux

Guillaume Marcais

4/4/2005 6:07:00 PM

On Tue, 2005-04-05 at 02:50 +0900, Berger, Daniel wrote:
> From what I'm reading at
> http://www.opengroup.org/onlinepubs/009695399/functions..., it
> appears that in this case, Windows is, in fact, doing the right thing
> (though see below). From the text:
>
> "For finite values of x < -0, a domain error shall occur, and either a
> NaN (if supported), or an implementation-defined value shall be
> returned."

It seems to me that the the relevant part in the text is:

"If x is NaN, a NaN shall be returned."

So the *nix implementation is correct and the Windows one is not.


> It looks to me like *nix is only returning NaN, but isn't actually
> raising an error while Windows is raising an error, but is not returning
> NaN.

Well, there is a problem here between the way C and Ruby raise error. In
C, the eerno variable is set and a specific value is returned. In Ruby,
when an error is raised, the call doesn't return and the call stack is
traversed to find the next rescue clause. So in Ruby, it will return NaN
or raise an error, but never both.

Guillaume.




2 Answers

djberg96

4/4/2005 8:11:00 PM

0

Guillaume Marcais wrote:
> On Tue, 2005-04-05 at 02:50 +0900, Berger, Daniel wrote:
> > From what I'm reading at
> > http://www.opengroup.org/onlinepubs/009695399/functions...,
it
> > appears that in this case, Windows is, in fact, doing the right
thing
> > (though see below). From the text:
> >
> > "For finite values of x < -0, a domain error shall occur, and
either a
> > NaN (if supported), or an implementation-defined value shall be
> > returned."
>
> It seems to me that the the relevant part in the text is:
>
> "If x is NaN, a NaN shall be returned."
>
> So the *nix implementation is correct and the Windows one is not.

I think it's open for debate. It causes a ZeroDivision error on both
Perl and Python if we want to look at what other languages do.

> > It looks to me like *nix is only returning NaN, but isn't actually
> > raising an error while Windows is raising an error, but is not
returning
> > NaN.
>
> Well, there is a problem here between the way C and Ruby raise error.
In
> C, the eerno variable is set and a specific value is returned. In
Ruby,
> when an error is raised, the call doesn't return and the call stack
is
> traversed to find the next rescue clause. So in Ruby, it will return
NaN
> or raise an error, but never both.
>
> Guillaume.

Yeah, I wasn't thinking. It would be kinda neat if we could (though I
have no idea how that would be implemented).

What about a compromise where an error is raised, and "NaN" is included
in part of the error message, e.g.

Errno::EDOM: NaN (sqrt)

Or something like that?

Dan

PS - I think this is my 4th attempted reply - if a bunch of similar
messages show up later, it's my mail server acting up.

Guillaume Marcais

4/5/2005 3:11:00 AM

0

Le 4 avr. 05, à 16:14, Daniel Berger a écrit :
>> It seems to me that the the relevant part in the text is:
>>
>> "If x is NaN, a NaN shall be returned."
>>
>> So the *nix implementation is correct and the Windows one is not.
>
> I think it's open for debate. It causes a ZeroDivision error on both
> Perl and Python if we want to look at what other languages do.

Well, I should have said: "it behaves according to what the quoted
article specifies".

Looking at the Ruby code, Ruby relies on the the OS implementation of
the mathematic library. So here it is not Ruby that generate the error
or Nan in the first, but the OS. So Windows and *nix disagree
apparently on the behavior.

So the question is: should Ruby rely on the system for its mathematical
computation, or should it provide an abstraction layer to offer a
consistent behavior across platforms?

> Yeah, I wasn't thinking. It would be kinda neat if we could (though I
> have no idea how that would be implemented).
>
> What about a compromise where an error is raised, and "NaN" is included
> in part of the error message, e.g.
>
> Errno::EDOM: NaN (sqrt)
>
> Or something like that?

Sure, sounds good. But I am not in the right person to make that
decision though.

Guillaume.