Project

General

Profile

Bug #9979

Regression: BigDecimal::ROUND_HALF_DOWN/ROUND_HALF_EVEN behave incorrectly

Added by josephleniston (Joseph Leniston) about 5 years ago. Updated 29 days ago.

Status:
Closed
Priority:
Normal
Target version:
-
ruby -v:
2.1.2
[ruby-core:63326]

Description

The incorrect behaviour is that all fractional values between 0.5 (inclusive) and 0.6 (non-inclusive) are subject to the rounding policy for 'half', whereas it should only be applied for fractional values exactly equal to 0.5.

History

Updated by josephleniston (Joseph Leniston) about 5 years ago

This issue is a regression and is the same as issue #3803

Updated by nagachika (Tomoyuki Chikanaga) about 5 years ago

  • Status changed from Open to Assigned

Joseph, thank you for your report.

But I think current behavior on trunk is correct as described in #3803.

> BigDecimal.new("0.51").round(0, BigDecimal::ROUND_HALF_DOWN).to_s("F")
=> "1.0"
> BigDecimal.new("0.51").round(0, BigDecimal::ROUND_HALF_EVEN).to_s("F")
=> "1.0"

mrkn, could you judge this?

Updated by mrkn (Kenta Murata) over 2 years ago

  • Status changed from Assigned to Feedback

I think the current behavior is the correct one.

$ ruby -rbigdecimal -ve 'p Gem::Dependency.new("bigdecimal").to_spec.version.to_s; puts ("0.50"..."0.60").map {|x| [x, BigDecimal(x).round(0, BigDecimal::ROUND_HALF_DOWN).to_s("F")].join(" -> ") }'
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
"1.2.8"
0.50 -> 0.0
0.51 -> 1.0
0.52 -> 1.0
0.53 -> 1.0
0.54 -> 1.0
0.55 -> 1.0
0.56 -> 1.0
0.57 -> 1.0
0.58 -> 1.0
0.59 -> 1.0

$ ruby -rbigdecimal -ve 'p Gem::Dependency.new("bigdecimal").to_spec.version.to_s; puts ("0.50"..."0.60").map {|x| [x, BigDecimal(x).round(0, BigDecimal::ROUND_HALF_EVEN).to_s("F")].join(" -> ") }'
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
"1.2.8"
0.50 -> 0.0
0.51 -> 1.0
0.52 -> 1.0
0.53 -> 1.0
0.54 -> 1.0
0.55 -> 1.0
0.56 -> 1.0
0.57 -> 1.0
0.58 -> 1.0
0.59 -> 1.0

$ ruby -rbigdecimal -ve 'p Gem::Dependency.new("bigdecimal").to_spec.version.to_s; puts ("1.50"..."1.60").map {|x| [x, BigDecimal(x).round(0, BigDecimal::ROUND_HALF_EVEN).to_s("F")].join(" -> ") }'
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
"1.2.8"
1.50 -> 2.0
1.51 -> 2.0
1.52 -> 2.0
1.53 -> 2.0
1.54 -> 2.0
1.55 -> 2.0
1.56 -> 2.0
1.57 -> 2.0
1.58 -> 2.0
1.59 -> 2.0

Joseph, please show the wrong examples.

#4

Updated by jeremyevans0 (Jeremy Evans) 29 days ago

  • Status changed from Feedback to Closed

Also available in: Atom PDF