Project

General

Profile

Actions

Bug #6120

closed

Float and BigDecimal bug in remainder in corner cases

Bug #6120: Float and BigDecimal bug in remainder in corner cases

Added by marcandre (Marc-Andre Lafortune) over 13 years ago. Updated over 4 years ago.

Status:
Closed
Target version:
-
[ruby-core:43109]
Tags:

Description

Currently:

4.2.remainder(+Float::INFINITY) # => 4.2, ok
4.2.remainder(-Float::INFINITY) # => NaN, should be 4.2
# (same with all signs reversed)

Reasons the remainder should be 4.2 and not NaN:

  1. foo.remainder(bar) == foo.remainder(-bar)
  2. foo.remainder(bar) == foo when bar.abs > foo.abs

Similarly:
require 'bigdecimal'
bd = BigDecimal.new("4.2")
bd.remainder(BigDecimal.new("+Infinity")) # => NaN, should be bd
bd.remainder(BigDecimal.new("-Infinity")) # => NaN, should be bd
# (same with all signs reverse)

Reasons: same as float.

Finally:
bd = BigDecimal.new("4.2")
bd.modulo(BigDecimal.new("0")) # => ZeroDivisionError, probably ok?
bd.remainder(BigDecimal.new("0")) # => NaN, should be probably raise a ZeroDivisionError?

Like in #6044, this could be decided either way, as long as there is consistency. Anyone prefer NaN to raising a ZeroDivisionError?

Updated by naruse (Yui NARUSE) over 13 years ago Actions #1 [ruby-core:43118]

Ruby's math should portably follow SuS. [ruby-core:28206]
You can fix it for Float/Math if it is obviously wrong and the right implementation is clear.

BigDecimal is little another world and it is up to mrkn.

Updated by shyouhei (Shyouhei Urabe) over 13 years ago Actions #2

  • Status changed from Open to Assigned

Updated by mame (Yusuke Endoh) over 12 years ago Actions #3 [ruby-core:52445]

  • Target version changed from 2.0.0 to 2.6

Should it be assigned to mrkn?

--
Yusuke Endoh

Updated by marcandre (Marc-Andre Lafortune) over 12 years ago Actions #4 [ruby-core:52487]

Didn't get around fixing it for 2.0.0. Will fix and then assign to mrkn for BigDecimal.

Updated by naruse (Yui NARUSE) almost 8 years ago Actions #5

  • Target version deleted (2.6)

Updated by jeremyevans0 (Jeremy Evans) over 4 years ago Actions #6 [ruby-core:102816]

I've submitted a pull request to fix this issue for Integer/Float: https://github.com/ruby/ruby/pull/4257

I've submitted an issue to the bigdecimal repository to fix the issue in BigDecimal: https://github.com/ruby/bigdecimal/issues/187

Updated by jeremyevans (Jeremy Evans) over 4 years ago Actions #7

  • Status changed from Assigned to Closed

Applied in changeset git|aaab3b1de943c3317e115d623ffc7908b4c96578.


Fix integer/float remainder with infinity argument of opposite sign

Previously, the result was incorrect:

4.remainder(-Float::INFINITY)
Before: => NaN
After: => 4

4.2.remainder(-Float::INFINITY)
Before: => NaN
After: => 4.2

Fixes [Bug #6120]

Actions

Also available in: PDF Atom

Like0
Like0Like0Like0Like0Like0Like0Like0