Issue #5294 has been updated by Todd Kushner.


snippet:

a9e20s = %w{
0.9E+21
+0.9E+21
-0.9E+21
 .9E+21
+.9E+21
-.9E+21
9E+20
+9E+20
-9E+20
9.E+20
+9.E+20
-9.E+20
9.0E+20
+9.0E+20
-9.0E+20
(etc...)
}
a9e20s.each { |num|
  printf "string: '%-10s' float: %50.25f\n", num, num.to_f
}
----------------------------------------
Bug #5294: to_f does not convert floating point number the same as Fortran
http://redmine.ruby-lang.org/issues/5294

Author: Todd Kushner
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 
ruby -v: 1.9.2p180 (2011-02-18) [i386-mingw32]


We were converting some Fortran programs to Ruby, and found the following floating point number format did not convert as Fortran does on input, i.e., on:

'[+-]nn.E[+-]nn'

the exponent is ignored by to_f, but not by Fortran:  

string: '9.E+20    ' float:                        9.0000000000000000000000000
string: '+9.E+20   ' float:                        9.0000000000000000000000000
string: '-9.E+20   ' float:                       -9.0000000000000000000000000
string: '9.E20     ' float:                        9.0000000000000000000000000
string: '+9.E20    ' float:                        9.0000000000000000000000000
string: '-9.E20    ' float:                       -9.0000000000000000000000000
string: '9.E-20    ' float:                        9.0000000000000000000000000
string: '+9.E-20   ' float:                        9.0000000000000000000000000
string: '-9.E-20   ' float:                       -9.0000000000000000000000000

Listed below, we found all other variations we could think of seemed to work properly:

string: '0.9E+21   ' float:    900000000000000000000.0000000000000000000000000
string: '+0.9E+21  ' float:    900000000000000000000.0000000000000000000000000
string: '-0.9E+21  ' float:   -900000000000000000000.0000000000000000000000000
string: '.9E+21    ' float:    900000000000000000000.0000000000000000000000000
string: '+.9E+21   ' float:    900000000000000000000.0000000000000000000000000
string: '-.9E+21   ' float:   -900000000000000000000.0000000000000000000000000
string: '9E+20     ' float:    900000000000000000000.0000000000000000000000000
string: '+9E+20    ' float:    900000000000000000000.0000000000000000000000000
string: '-9E+20    ' float:   -900000000000000000000.0000000000000000000000000
string: '9.0E+20   ' float:    900000000000000000000.0000000000000000000000000
string: '+9.0E+20  ' float:    900000000000000000000.0000000000000000000000000
string: '-9.0E+20  ' float:   -900000000000000000000.0000000000000000000000000
string: '0.9E21    ' float:    900000000000000000000.0000000000000000000000000
string: '+0.9E21   ' float:    900000000000000000000.0000000000000000000000000
string: '-0.9E21   ' float:   -900000000000000000000.0000000000000000000000000
string: '.9E21     ' float:    900000000000000000000.0000000000000000000000000
string: '+.9E21    ' float:    900000000000000000000.0000000000000000000000000
string: '-.9E21    ' float:   -900000000000000000000.0000000000000000000000000
string: '9E20      ' float:    900000000000000000000.0000000000000000000000000
string: '+9E20     ' float:    900000000000000000000.0000000000000000000000000
string: '-9E20     ' float:   -900000000000000000000.0000000000000000000000000
string: '9.0E20    ' float:    900000000000000000000.0000000000000000000000000
string: '+9.0E20   ' float:    900000000000000000000.0000000000000000000000000
string: '-9.0E20   ' float:   -900000000000000000000.0000000000000000000000000
string: '0.9E-19   ' float:                        0.0000000000000000000900000
string: '+0.9E-19  ' float:                        0.0000000000000000000900000
string: '-0.9E-19  ' float:                       -0.0000000000000000000900000
string: '.9E-19    ' float:                        0.0000000000000000000900000
string: '+.9E-19   ' float:                        0.0000000000000000000900000
string: '-.9E-19   ' float:                       -0.0000000000000000000900000
string: '9E-20     ' float:                        0.0000000000000000000900000
string: '+9E-20    ' float:                        0.0000000000000000000900000
string: '-9E-20    ' float:                       -0.0000000000000000000900000
string: '9.0E-20   ' float:                        0.0000000000000000000900000
string: '+9.0E-20  ' float:                        0.0000000000000000000900000
string: '-9.0E-20  ' float:                       -0.0000000000000000000900000

Thanks!

Todd Kushner
Jeppesen
todd.kushner / jeppesen.com



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