Issue #8299 has been updated by phasis68 (Heesob Park).


I guess this is a compiler specific issue.

I tested some cases with different compilers with Ruby 1.9.3-p392 on the same machine.
gcc 4.5.2 built version(rubyinstaller version) shows not disired result.
But gcc 4.7.2 and MSVC v16.0 built version shows a correct result.

C:\work\ruby-1.9.3-p392-mg5>.\miniruby -ve 'a=-1.1505945E-5;puts(a,"%0.20g"%a)'
ruby 1.9.3p392 (2013-02-22 revision 39386) [i386-mingw32]
-1.1505945000000001e-05
-1.1505945000000000847e-05

C:\work\ruby-1.9.3-p392-mg7>.\miniruby -ve 'a=-1.1505945E-5;puts(a,"%0.20g"%a)'
ruby 1.9.3p392 (2013-02-22 revision 39386) [i386-mingw32]
-1.1505945e-05
-1.1505944999999999153e-05

C:\work\ruby-1.9.3-p392-ms>.\miniruby -ve 'a=-1.1505945E-5;puts(a,"%0.20g"%a)'
ruby 1.9.3p392 (2013-02-22 revision 39386) [i386-mswin32_100]
-1.1505945e-05
-1.1505944999999999153e-05

I found the main cause is the different result of rounded quotient operation.
Here is the test case.

    C:\work>type foo.c
    #include <stdio.h>
    void main() {
            double a = 11505945.0;
            double b = 1000000000000.0;
            a /= b;
            printf("a = %0.20g\n",a);
    }
    
    C:\work>gcc --version
    gcc (tdm-1) 4.5.2
    Copyright (C) 2010 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.   
    C:\work>gcc foo.c -o foo    
    C:\work>foo
    a = 1.1505945000000001e-005
    
    
    C:\work>gcc --version
    gcc (rubenvb-4.7.2-release) 4.7.2
    Copyright (C) 2012 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    C:\work>gcc foo.c -o foo
    C:\work>foo
    a = 1.1505944999999999e-005
    
    C:\work>cl foo.c
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    foo.c
    Microsoft (R) Incremental Linker Version 10.00.40219.01
    Copyright (C) Microsoft Corporation.  All rights reserved.
    /out:foo.exe
    foo.obj
    C:\work>foo
    a = 1.1505944999999999e-005

----------------------------------------
Bug #8299: Minor error in float parsing
https://bugs.ruby-lang.org/issues/8299#change-38821

Author: bobjalex (Bob Alexander)
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 
ruby -v: trunk
Backport: 


I encountered a float that either parses [slightly] differently (or converts to string differently) in Ruby than it does in Python or Java. This looks like a  Ruby bug since the result "looks" incorrect.

It is easily reproduced by entering the magic number (-1.1505945E-5) into irb. It behaves the same in 2.0 and 1.9. I'm using Windows.

Below is an irb session that demonstrates. Also included are JRuby and Python trials that show better behavior.

This issue is not causing me any problems, but just in case someone there is interested in looking into it...

Bob

>ruby -v
ruby 2.0.0p0 (2013-02-24) [i386-mingw32]

>irb
irb(main):001:0> RUBY_VERSION
=> "2.0.0"
irb(main):002:0> -1.1505945E-5
=> -1.1505945000000001e-05

>ruby19 -v
ruby 1.9.3p392 (2013-02-22) [i386-mingw32]

>irb19
irb(main):001:0> RUBY_VERSION
=> "1.9.3"
irb(main):002:0> -1.1505945E-5
=> -1.1505945000000001e-05
irb(main):002:0>

>jirb
irb(main):001:0> -1.1505945E-5
=> -1.1505945e-05

>python
Python 2.7.4rc1 (default, Mar 24 2013, 14:34:32) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> -1.1505945E-5
-1.1505945e-05
>>> repr(-1.1505945E-5)
'-1.1505945e-05'




-- 
http://bugs.ruby-lang.org/