Kernel#Float() parsing is not Locale aware #93

Closed
riffraff opened this Issue Nov 1, 2011 · 9 comments

4 participants

@riffraff

In ruby 1.8.7 and 1.9.2 this

 Float("0.000000000")

and

Float("0.0")

return 0.0but on maglev-head on OSX snow leopard I get a

ArgumentError: invalid value for Float(): "0.0"
from (irb):2:in `__compileEval'

ruby -v: maglev 1.0.0 (ruby 1.8.7) (2011-10-31 rev 1.0.0-27184)[Darwin x86_64]
uname -a: 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun 7 16:32:41 PDT 2011; root:xnu-1504.15.3~1/RELEASE_X86_64 x86_64
machine: macbook pro intel

@pbm
MagLev Engineering Team member
pbm commented Nov 1, 2011

I'm running same version of Snow Leopard and MagLev, but works for me. Did you install with RVM or from github?

One thing you can try:
1. export MAGLEV_OPTS="-d"
2. Run the code again.

When it gets the error, it should leave you at the topaz prompt. Type "where", and let us see the stack trace.

@riffraff

installed via rvm, stacktrace and pre-prompt is at https://gist.github.com/1330812 (github would not let me post it as a comment)

it appears to me that this could be a locale issue (I assume my machine has "italian" in some system variable), as Float('0,1') appears to work and I can see this happening:

$ ruby -e 'p 1/2.to_f'
 0,5
@riffraff

so, I omitted: it is not zero related as I expected at first, but float related (Float("0,0") also returns a zero float).
this seems to affect string representation and the #Float method, while at the repl it seems stuff after the dot is silently dropped:

maglev-head :001 > a= 0.1
=> 0,
maglev-head :002 > b=0
=> 0
maglev-head :003 > a == b
=> true
maglev-head :004 > c= 0,1
=> [0, 1]
maglev-head :005 > d= Float('0,1')
=> 0,10000000000000001

@pbm
MagLev Engineering Team member
pbm commented Nov 1, 2011

Thanks for the followup, I think I have enough to start working on it.

@riffraff

no problem, I can also confirm you that changing locale fixes the issue:

mecbucpro:~ riffraff$ LANG="en_US.UTF-8" ruby -e 'p Float("0.0")'
 0.0
@pbm
MagLev Engineering Team member
pbm commented Nov 1, 2011

And for US English users, we can reproduce with:

```$ LANG="it_IT.UTF-8" maglev-ruby -e 'p Float("0.0")'
ERROR 2702 , invalid value for Float(): "0.0" (ArgumentError)
topaz 1> exitifnoerror
End of initialization files
topaz 1>

@pbm
MagLev Engineering Team member
pbm commented Nov 1, 2011

The problem is that Smalltalk is locale aware, but Ruby isn't. Some operations (i.e., string to float conversion) are done in Smalltalk in a locale aware manner, but other operations are done in Ruby, which is ignorant of locale.

A quick workaround is to add export LANG=C to the beginning of bin/maglev-ruby. That will erase locale for sub-processes, but should "fix" the current problem, and provide consistent behavior for any other locale aware operations provided by Smalltalk.

@herrtreas

Using LANG=C in front of any MagLev related command (e.g. LANG=C $MAGLEV_HOME/bin/bundle install) works for me..

@timfel
MagLev Engineering Team member

@pbm export LANG=C might be a problem when running "system" from within maglev and expecting it to pick up the surrounding locale. but maybe this is just nitpicking. I'd say go for it :)

@pbm pbm added a commit that closed this issue Nov 2, 2011
@pbm pbm Workaround issues with Locale aware Smalltalk methods.
Set LANG=C in maglev-ruby script.  This essentially turns off locale aware
processing in Smalltalk.  The down-side is that processes forked from
maglev-ruby will also have LANG=C.

fixes #93
ad98531
@pbm pbm closed this in ad98531 Nov 2, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment