Skip to content
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

Merged
merged 4 commits into from Jun 11, 2017

Conversation

straight-shoota
Copy link
Member

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).

@asterite
Copy link
Member

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.

@straight-shoota
Copy link
Member Author

I've switched mult/div to use int operators for digits < 0 so that the calculation works without floats. This seems to be a better solution here anyway...

src/number.cr Outdated
y = base ** digits
self.class.new((x * y).round / y)
if digits < 0
y = base ** (digits * -1)
Copy link
Contributor

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

Copy link
Member Author

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

base ** (-digits) works, though...

Copy link
Contributor

@RX14 RX14 Jun 11, 2017

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?

Copy link
Member

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 :-)

@asterite
Copy link
Member

@straight-shoota Thanks! Could you add a spec with the failing case I put above? (so that I know that it works well now)

@straight-shoota
Copy link
Member Author

It's already included in the recent commit.

@asterite asterite added this to the Next milestone Jun 11, 2017
@asterite asterite merged commit aaf000c into crystal-lang:master Jun 11, 2017
@asterite
Copy link
Member

@straight-shoota Thank you!

@straight-shoota straight-shoota deleted the jm-number-round branch June 11, 2017 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants