BigDecimal with Float comparision bug

Hello,

I have noticed that BigDecimal may fail in many cases when you try to
compare it with float:

Float:

BigDecimal.new(“25.0”) == 25.0
=> false
Decimal:

BigDecimal.new(“25.0”) == 25
=> true

(1…1000).select { |d| BigDecimal.new("#{d}.0") != d.to_f }.size
=> 414

So 41.4% such comparisions are false!!!

What do you think about it?

Regards

On Tuesday 26 August 2008, Maciej Tomaka wrote:

Decimal:
What do you think about it?

Regards

It works correctly for me (ruby 1.8.7-p72) on gentoo linux (linux
2.6.25, gcc
4.3.1). Which version of ruby are you using?

Stefano

Stefano C. wrote:

On Tuesday 26 August 2008, Maciej Tomaka wrote:

Decimal:
What do you think about it?

Regards

It works correctly for me (ruby 1.8.7-p72) on gentoo linux (linux
2.6.25, gcc
4.3.1). Which version of ruby are you using?

Stefano

It worked previously for me now it does not work.
Sorry , I have forgot to write it:

The test was made on linux x64 ruby version:

Fails:

ruby 1.8.6 (2007-09-24 patchlevel 111) [x86_64-linux]
ruby 1.8.5 (2006-08-25) [x86_64-linux]

Good:

ruby 1.8.6 (2007-06-07 patchlevel 36) [i386-linux]
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-freebsd4]
ruby 1.8.5 (2006-08-25) [i386-freebsd6]
ruby 1.8.4 (2005-12-24) [i386-freebsd4]
o

Maciej Tomaka wrote:

Stefano C. wrote:

On Tuesday 26 August 2008, Maciej Tomaka wrote:

Decimal:
What do you think about it?

Regards

It works correctly for me (ruby 1.8.7-p72) on gentoo linux (linux
2.6.25, gcc
4.3.1). Which version of ruby are you using?

Stefano

It worked previously for me now it does not work.
Sorry , I have forgot to write it:

The test was made on linux x64 ruby version:

Fails:

ruby 1.8.6 (2007-09-24 patchlevel 111) [x86_64-linux]
ruby 1.8.5 (2006-08-25) [x86_64-linux]

Good:

ruby 1.8.6 (2007-06-07 patchlevel 36) [i386-linux]
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-freebsd4]
ruby 1.8.5 (2006-08-25) [i386-freebsd6]
ruby 1.8.4 (2005-12-24) [i386-freebsd4]

Full tests gave:

ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-freebsd4]
ruby 1.8.5 (2006-08-25) [i386-freebsd6]
ruby 1.8.2 (2004-12-25) [i386-freebsd5]

(1…1000).select { |d| BigDecimal.new("#{d}.0") != d.to_f }.size
=> 304

ruby 1.8.6 (2007-06-07 patchlevel 36) [i386-linux]

(1…1000).select { |d| BigDecimal.new("#{d}.0") != d.to_f }.size
=> 141

ruby 1.8.5 (2006-08-25) [x86_64-linux]
ruby 1.8.6 (2007-09-24 patchlevel 111) [x86_64-linux]

(1…1000).select { |d| BigDecimal.new("#{d}.0") != d.to_f }.size
=> 414

I am compiling most recent version of ruby now.

Regards

ruby-1.8.7-p72 returns 0 bad comparisions.

Sandor Szücs wrote:

Maybe it is a plattform or version issue.

Yea, it is version issue. 1.8.7 seems not to have the bug.

On ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-darwin8] it works as you
expected.

Regards
Maciej

On 26.08.2008, at 13:09, Maciej Tomaka wrote:

(1…1000).select { |d| BigDecimal.new("#{d}.0") != d.to_f }.size
=> 414

So 41.4% such comparisions are false!!!

What do you think about it?

Maybe it is a plattform or version issue.

On ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-darwin8] it works as you
expected.

irb> require ‘bigdecimal’
=> true
irb> BigDecimal.new(“25.0”)
=> #BigDecimal:4da044,‘0.25E2’,4(8)
irb> BigDecimal.new(“25.0”).to_f
=> 25.0
irb> BigDecimal.new(“25.0”) == 25
=> true
irb> BigDecimal.new(“25.0”) == 25.0
=> true
irb> (1…1000).select { |d| BigDecimal.new("#{d}.0") != d.to_f }.size
=> 0

regards, Sandor
Szücs

On Aug 26, 8:04 am, Sandor Szücs [email protected] wrote:

Maybe it is a plattform or version issue.
irb>BigDecimal.new(“25.0”) == 25
=> true
irb>BigDecimal.new(“25.0”) == 25.0
=> true
irb> (1…1000).select { |d|BigDecimal.new("#{d}.0") != d.to_f }.size
=> 0

regards, Sandor Szücs

Works for me
irb(main):002:0> require ‘bigdecimal’
=> true
irb(main):003:0> (1…1000).select { |d| BigDecimal.new("#{d}.0") !=
d.to_f }.size
=> 0

renard$ ruby -v
ruby 1.8.6 (2008-08-11 patchlevel 287) [i686-darwin9.4.0]

It works in my ruby in windows :slight_smile:

irb(main):003:0> BigDecimal.new(“25.0”) == 25.0
=> true
irb(main):004:0> BigDecimal.new(“25.0”) == 25
=> true
irb(main):005:0> exit

C:\ruby --version
ruby 1.8.6 (2008-07-17 patchlevel 279) [i386-mingw32]

The simple test comparing BigDecimal.new(‘25’) == 25.0 is true, but
BigDecimal.new(‘24’) == 24.0 is false.

The numbers from 1 to 1000 that don’t test like the rest of them on my
system are:
[3, 6, 12, 13, 21, 24, 26, 29, 31, 42, 45, 48, 51, 52, 55, 58, 61, 62,
69,
79, 84, 87, 90, 93, 96, 99, 101, 102, 104, 107, 110, 113, 116, 119, 121,
122, 124, 127, 138, 157, 158, 163, 168, 169, 174, 175, 180, 181, 185,
186,
191, 192, 197, 198, 202, 203, 204, 208, 209, 214, 215, 220, 221, 225,
226,
227, 231, 232, 237, 238, 242, 243, 244, 248, 249, 254, 255, 259, 276,
293,
299, 314, 316, 319, 321, 326, 331, 333, 336, 338, 343, 345, 348, 350,
353,
355, 360, 362, 365, 367, 370, 372, 377, 379, 382, 384, 387, 389, 394,
396,
399, 401, 404, 406, 408, 411, 413, 416, 418, 421, 423, 425, 428, 430,
433,
435, 440, 442, 445, 447, 450, 452, 454, 457, 459, 462, 464, 467, 469,
471,
474, 476, 479, 481, 484, 486, 488, 491, 493, 496, 498, 501, 503, 505,
508,
510, 518, 535, 552, 569, 581, 586, 598, 615, 627, 628, 632, 633, 637,
638,
642, 643, 647, 652, 657, 661, 662, 666, 667, 671, 672, 676, 677, 681,
686,
690, 691, 695, 696, 700, 701, 705, 706, 710, 715, 720, 724, 725, 729,
730,
734, 735, 739, 740, 744, 749, 753, 754, 758, 759, 763, 764, 768, 769,
773,
774, 778, 783, 787, 788, 792, 793, 797, 798, 802, 803, 807, 808, 812,
816,
817, 821, 822, 826, 827, 831, 832, 836, 837, 841, 842, 846, 850, 851,
855,
856, 860, 861, 865, 866, 870, 871, 875, 879, 880, 884, 885, 889, 890,
894,
895, 899, 900, 904, 905, 908, 909, 913, 914, 918, 919, 923, 924, 928,
929,
933, 934, 938, 939, 942, 943, 947, 948, 952, 953, 957, 958, 962, 963,
967,
968, 971, 972, 973, 976, 977, 981, 982, 986, 987, 991, 992, 996, 997]

Just thought I would share… Running Ruby 1.8.6 on Windows.

The code I used to generate the output is:
failed = Array.new
(1…1000).each { |d| failed << d if BigDecimal.new("#{d}.0") != d.to_f }
p failed

The doctest code I used for testing for 24 is:

doctest: Testing BigDecimal

>> BigDecimal.new(‘24.0’) == 24

=> true

>> BigDecimal.new(‘24.0’) == 24.0

=> false

end

This passes, so indicates what is happening. Why? I don’t know.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs