Project

General

Profile

Actions

Bug #5367

closed

Floating point number display problem

Added by baryluk (Witold Baryluk) almost 11 years ago. Updated almost 11 years ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 1.8.7 (2011-06-30 patchlevel 352) [i486-linux]
[ruby-core:39722]

Description

Hi,
I'm not really an Ruby user/developer, but noticed this with irb1.8 from Debian sid.

irb(main):005:0> 9007199254740994.0
=> 9.00719925474099e+15
irb(main):006:0> 9007199254740994.0 - 9.00719925474099e+15
=> 4.0

It looks that Ruby do not convert floating point numbers to decimal/scientific notation correctly (preserving precisly value) by default. I think it is good behavior to preserve value 100% by default, as it can lead to nasty bugs otherwise.

It is quite common bug in various libraries, and known correct (not just hacks) solutions can be find on the Internet.

$ irb1.8 -v
irb 0.9.5(05/04/13)

$ dpkg -l ruby1.8
ii ruby1.8 1.8.7.352-2 Interpreter of object-oriented scripting language Ruby 1.8

$ uname
Linux sredniczarny 3.0.0-1-686-pae #1 SMP Sat Aug 27 16:41:03 UTC 2011 i686 GNU/Linux

$ cat /proc/cpuinfo | egrep 'model|flags|fpu'
model : 13
model name : Intel(R) Pentium(R) M processor 1.73GHz
fpu : yes
fpu_exception : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov clflush dts acpi mmx fxsr sse sse2 ss tm pbe nx up bts est tm2

Thanks.

Updated by naruse (Yui NARUSE) almost 11 years ago

  • Status changed from Open to Rejected

Ruby 1.8 rounds the floating point number value on showing it as string.
The behavior is changed in Ruby 1.9, but we won't change it in 1.8.

You can use '%f' % 9007199254740994.0 or '%.40f' % 9007199254740994.0 for an workaround.

Actions

Also available in: Atom PDF