Project

General

Profile

Actions

Bug #17183

closed

Float round working weirdly

Bug #17183: Float round working weirdly

Added by agustincornu (Agustin Cornu) about 5 years ago. Updated about 4 years ago.

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

Description

I encountered this issue when casting a specific float with a especific round argument.

0.5.round(15)
=> 0.5
0.5.round(16)
=> 0.5000000000000001
0.5.round(17)
=> 0.5

Related issues 1 (0 open1 closed)

Related to Ruby - Bug #14635: Float#round(n) returns a wrong result when n is bigClosedActions

Updated by agustincornu (Agustin Cornu) about 5 years ago Actions #1

  • ruby -v set to 2.7

Updated by agustincornu (Agustin Cornu) about 5 years ago Actions #2 [ruby-core:100091]

This is a known issue and not a bug. My bad. I cannot delete the ticket

Updated by marcandre (Marc-Andre Lafortune) about 5 years ago Actions #3

  • Is duplicate of Bug #14635: Float#round(n) returns a wrong result when n is big added

Updated by marcandre (Marc-Andre Lafortune) about 5 years ago Actions #4

  • Is duplicate of deleted (Bug #14635: Float#round(n) returns a wrong result when n is big)

Updated by marcandre (Marc-Andre Lafortune) about 5 years ago Actions #5

  • Related to Bug #14635: Float#round(n) returns a wrong result when n is big added

Updated by marcandre (Marc-Andre Lafortune) about 5 years ago Actions #6 [ruby-core:100092]

I think that's a bug. Looks different but maybe related to #14365.

Updated by shyouhei (Shyouhei Urabe) about 5 years ago Actions #7 [ruby-core:100097]

Agreed to @marcandre (Marc-Andre Lafortune) . This sounds like a real bug.

Updated by jeremyevans (Jeremy Evans) about 4 years ago Actions #8

  • Status changed from Open to Closed

Applied in changeset git|d16b68cb2204eeb5af8bd39149202b630374c67f.


Use Rational for Float#round with ndigits > 14

ndigits higher than 14 can result in values that are slightly too
large due to floating point limitations. Converting to rational
for the calculation and then back to float fixes these issues.

Fixes [Bug #14635]
Fixes [Bug #17183]

Co-authored by: Yusuke Endoh

Actions

Also available in: PDF Atom