Project

General

Profile

Actions

Bug #17183

closed

Float round working weirdly

Added by agustincornu (Agustin Cornu) 12 months ago. Updated about 1 month ago.

Status:
Closed
Priority:
Normal
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

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

Updated by agustincornu (Agustin Cornu) 12 months ago

  • ruby -v set to 2.7

Updated by agustincornu (Agustin Cornu) 12 months 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) 12 months 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) 12 months 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) 12 months ago

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

Updated by marcandre (Marc-Andre Lafortune) 12 months ago

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

Updated by shyouhei (Shyouhei Urabe) 12 months ago

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

Actions #8

Updated by jeremyevans (Jeremy Evans) about 1 month 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 mame@ruby-lang.org

Actions

Also available in: Atom PDF