Hi,
At Thu, 22 Apr 2004 11:33:44 +0900,
Jeff Mitchell wrote in [ruby-talk:97988]:
Oddly enough, with your patch the first two floats are fixed,
showing 24.0 and 28.0 (which is entirely a coincidence; this
list didn’t exist when I came up with the original numbers).
The rest are as shown.
$ cat f.rb
#! /usr/bin/ruby
require ‘hexfloat’ # http://nokada.jin.gr.jp/ruby/hexfloat.rb
require ‘stringio’
def show(a)
d = a%1
printf “%s %-20s %-20s %-20s %-20s\n”, a.class, a.to_s, a.hex, d.hex, (1.0-d).hex
end
[5.00000000000007, 5.000000000000007, 5.0000000000000007, 5.00000000000000007,
1.00000000000004, 1.000000000000004, 1.0000000000000004,
1.99999999999999, 1.999999999999999, 1.9999999999999999].each(&method(:show))
StringIO.open(*DATA.read.unpack(“m”)) do |file|
begin
while a = Marshal.load(file)
show(a)
end
rescue EOFError
end
end
END
BAhmGjI0LjAwMDAwMDAwMDAwMDAwNAAAAQQIZhoyNy45OTk5OTk5OTk5OTk5OTYA//8ECGYaNDQu
MDAwMDAwMDAwMDAwMDA3AAABBAhmGjQ1Ljk5OTk5OTk5OTk5OTk5MwD//wQIZho0OC4wMDAwMDAw
MDAwMDAwMDcAAAEECGYaNTEuOTk5OTk5OTk5OTk5OTkzAP//BAhmGjU0LjAwMDAwMDAwMDAwMDAw
NwAAAQQIZho4My45OTk5OTk5OTk5OTk5ODYA//8=
$ ./ruby f.rb
Float 5.00000000000007 0x5.000000000013c 0x1.3cp-44 0xf.fffffffffec4p-4
Float 5.00000000000001 0x5.000000000002 0x2.0p-48 0xf.ffffffffffep-4
Float 5.0 0x5.0000000000004 0x4.0p-52 0xf.fffffffffffcp-4
Float 5.0 0x5.0 0x0.0p-4 0x1.0
Float 1.00000000000004 0x1.00000000000b4 0xb.4p-48 0xf.ffffffffff4cp-4
Float 1.0 0x1.0000000000012 0x1.2p-48 0xf.ffffffffffeep-4
Float 1.0 0x1.0000000000002 0x2.0p-52 0xf.fffffffffffep-4
Float 1.99999999999999 0x1.fffffffffffd3 0xf.ffffffffffd3p-4 0x2.dp-48
Float 2.0 0x1.ffffffffffffb 0xf.fffffffffffbp-4 0x5.0p-52
Float 2.0 0x2.0 0x0.0p-4 0x1.0
Float 24.0 0x1.8000000000001p+4 0x1.0p-48 0xf.fffffffffffp-4
Float 28.0 0x1.bffffffffffffp+4 0xf.fffffffffffp-4 0x1.0p-48
Float 44.0 0x2.c000000000002p+4 0x2.0p-48 0xf.ffffffffffep-4
Float 46.0 0x2.dfffffffffffep+4 0xf.ffffffffffep-4 0x2.0p-48
Float 48.0 0x3.0000000000002p+4 0x2.0p-48 0xf.ffffffffffep-4
Float 52.0 0x3.3fffffffffffep+4 0xf.ffffffffffep-4 0x2.0p-48
Float 54.0 0x3.6000000000002p+4 0x2.0p-48 0xf.ffffffffffep-4
Float 84.0 0x5.3fffffffffffcp+4 0xf.ffffffffffcp-4 0x4.0p-48
The range to be .f format is affected by also its precision.
This might be rather realistic.
Index: numeric.c
···
===================================================================
RCS file: /cvs/ruby/src/ruby/numeric.c,v
retrieving revision 1.106
diff -u -2 -p -r1.106 numeric.c
— numeric.c 14 Apr 2004 04:06:25 -0000 1.106
+++ numeric.c 22 Apr 2004 04:34:22 -0000
@@ -517,8 +517,6 @@ flo_to_s(flt)
else fmt = “%.16e”;
}
- else if ((d1 = modf(value, &d2)) == 0) {
- fmt = “%.1f”;
- }
sprintf(buf, fmt, value);
–
Nobu Nakada