Project

General

Profile

Actions

Bug #20654

closed

Floor and ceil have unexpected behaviour when ndigits is large

Added by peterzhu2118 (Peter Zhu) about 1 month ago. Updated 14 days ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:118699]

Description

GitHub PR: https://github.com/ruby/ruby/pull/11257

There is unexpected behaviour in Integer#floor, Float#floor, Integer#ceil, Float#ceil when ndigits is large such that 10**ndigits is a bignum.

Previously, it would return 0 in such cases. However, this would cause unexpected behaviour such as:

puts -1.floor(-5) # => -100000
puts -1.floor(-10) # => -10000000000
puts -1.floor(-20) # => 0
puts 1.ceil(-5) # => 100000
puts 1.ceil(-10) # => 10000000000
puts 1.ceil(-20) # => 0

The PR changes the last result so that it will return -100000000000000000000 and 100000000000000000000, respectively.

Actions #1

Updated by peterzhu2118 (Peter Zhu) about 1 month ago

  • Status changed from Open to Closed

Applied in changeset git|3af2a7fbe12e11bac7b26bd991d3d02349f47295.


Fix floor when ndigits is large

[Bug #20654]

This commit fixes Integer#floor and Float#floor when the number is
negative and ndigits is large such that 10**ndigits is a bignum.

Previously, it would return 0 in such cases. However, this would cause
unexpected behaviour such as:

puts -1.floor(-5) # => -100000
puts -1.floor(-10) # => -10000000000
puts -1.floor(-20) # => 0

This commit changes the last result so that it will return
-100000000000000000000.

Updated by nagachika (Tomoyuki Chikanaga) 14 days ago

  • Backport changed from 3.1: UNKNOWN, 3.2: REQUIRED, 3.3: REQUIRED to 3.1: UNKNOWN, 3.2: DONE, 3.3: DONE

Thank you for preparing the backport PRs. They were merged.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0