Project

General

Profile

Actions

Bug #17183

closed

Float round working weirdly

Added by agustincornu (Agustin Cornu) about 4 years ago. Updated over 3 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 master - Bug #14635: Float#round(n) returns a wrong result when n is bigClosedActions
Actions #1

Updated by agustincornu (Agustin Cornu) about 4 years ago

  • ruby -v set to 2.7

Updated by agustincornu (Agustin Cornu) about 4 years ago

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

Actions #3

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

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

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

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

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

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

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

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

Updated by shyouhei (Shyouhei Urabe) about 4 years ago

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

Actions #8

Updated by jeremyevans (Jeremy Evans) over 3 years ago

  • 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: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0