-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix negative digit precision for Number#round
#4535
Fix negative digit precision for Number#round
#4535
Conversation
Thank you! I like this, but: x = 123456.123456
x.round(-5) # => 99999.99999999999 I still don't understand why that happens, the correct result is 100000 and Ruby works well, so we need to investigate it a bit more. |
I've switched mult/div to use int operators for |
src/number.cr
Outdated
y = base ** digits | ||
self.class.new((x * y).round / y) | ||
if digits < 0 | ||
y = base ** (digits * -1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use the unary minus operator here? y = base ** -digits
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was my initial idea, but it gave a syntax error: https://carc.in/#/r/267o
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
base ** (-digits)
works, though...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@asterite should this parse without the brackets?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should: #4549 . I just fixed it in master. Let's use (-digits)
for now :-)
@straight-shoota Thanks! Could you add a spec with the failing case I put above? (so that I know that it works well now) |
It's already included in the recent commit. |
@straight-shoota Thank you! |
It is custom to call
Number#round
with a negative digits precision to round the integer part.Calling
11.round(-1)
resulted in an error:Cannot raise an integer to a negative integer power, use floats for that (ArgumentError)
.