Project

General

Profile

Actions

Bug #14026

closed

RUBY ERROR ON DIVISION with FDIV

Added by frankpimenta (Frank Pimenta) about 7 years ago. Updated over 5 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
>2.4
[ruby-core:83339]

Description

~$ ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin16]
~$ rvm use 2.4.2
Using /Users/frankpimenta/.rvm/gems/ruby-2.4.2
~$ ruby -rbigdecimal -e 'p 196.fdiv(BigDecimal("13.0"))'
2.203413367e-314
~$ rvm use 2.4.1
Using /Users/frankpimenta/.rvm/gems/ruby-2.4.1
~$ ruby -rbigdecimal -e 'p 196.fdiv(BigDecimal("13.0"))'
2.1358685693e-314
~$ rvm use 2.3.5
Using /Users/frankpimenta/.rvm/gems/ruby-2.3.5
~$ ruby -rbigdecimal -e 'p 196.fdiv(BigDecimal("13.0"))'
#<BigDecimal:7fefe380a428,'0.1507692307 6923076923E2',27(36)>
~$ ruby -rbigdecimal -e 'p 196.fdiv(BigDecimal("13.0")).to_s("F")'
"15.076923076923076923"
~$ rvm use 2.4.2
Using /Users/frankpimenta/.rvm/gems/ruby-2.4.2
~$ ruby -rbigdecimal -e 'p 4.fdiv(BigDecimal("2.0"))'
2.236682878e-314
~$ ruby -rbigdecimal -e 'p 4.fdiv(BigDecimal("2"))'
2.2136573e-314
~$ man ruby
~$ ruby -e 'p Float::MIN'
2.2250738585072014e-308
~$ ruby -rbigdecimal -e 'p 4.fdiv(BigDecimal("2")).class'
Float
~$ ruby -e 'p Float::MAX'
1.7976931348623157e+308
~$


Files

Screen Shot 2017-10-18 at 16.50.46.png (205 KB) Screen Shot 2017-10-18 at 16.50.46.png frankpimenta (Frank Pimenta), 10/18/2017 02:51 PM

Updated by frankpimenta (Frank Pimenta) about 7 years ago

Screen shot is better for the eye.

Updated by jeremyevans0 (Jeremy Evans) over 5 years ago

  • Status changed from Open to Feedback

So it looks like there was a behavior change between Ruby 2.3 and 2.4, in that Integer#fdiv with a BigDecimal argument used to return BigDecimal instead of Float. However, I consider that change a bug fix, as the documentation for fdiv (even in Ruby 2.3) states Returns float division. You should get the same results in all cases with the latest releases of Ruby:

$ ruby23 -v -rbigdecimal -e 'p 196.fdiv(BigDecimal("13.0"))'
ruby 2.3.8p459 (2018-10-18 revision 65136) [x86_64-openbsd]
#<BigDecimal:8f824da70d0,'0.1507692307 6923076923E2',27(36)>

$ ruby23 -v -rbigdecimal -e 'p 196.fdiv(BigDecimal("13.0")).to_f'
ruby 2.3.8p459 (2018-10-18 revision 65136) [x86_64-openbsd]
15.076923076923077

$ ruby24 -v -rbigdecimal -e 'p 196.fdiv(BigDecimal("13.0"))'
ruby 2.4.6p354 (2019-04-01 revision 67394) [x86_64-openbsd]
15.076923076923077

$ ruby25 -v -rbigdecimal -e 'p 196.fdiv(BigDecimal("13.0"))'
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-openbsd]
15.076923076923077

$ ruby26 -v -rbigdecimal -e 'p 196.fdiv(BigDecimal("13.0"))'
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-openbsd]
15.076923076923077

$ ruby27 -v -rbigdecimal -e 'p 196.fdiv(BigDecimal("13.0"))'
ruby 2.7.0preview1 (2019-05-31 trunk c55db6aa271df4a689dc8eb0039c929bf6ed43ff) [x86_64-openbsd]
15.076923076923077

Can you check and see if you still are having the same problems with the most recent release of Ruby 2.4 or a later version of Ruby?

Actions #4

Updated by jeremyevans0 (Jeremy Evans) over 5 years ago

  • Status changed from Feedback to Closed
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0