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 bug with negative numbers #8

Merged
merged 6 commits into from
Jan 23, 2017
Merged

Conversation

ruipneves
Copy link
Contributor

@ruipneves ruipneves commented Jan 20, 2017

I was looking for Math.round() documentation when I came across this bug. After concluding that roundTo was not behaving as expected I tried to fix the problem. The problem was that with negative numbers, Math.round() would go to the hight number, thus, not producing the expected results.

To solve this I checked if the requested function was the "round" function and if this was verified then the function would be applied to the absolute value of the number to round and then multiplied by it's original sign.

Is this the correct way to do it?
Any feedback is appreciated.

Fixes #6

Copy link

@satazor satazor left a comment

Choose a reason for hiding this comment

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

Looks good, handing over to @sindresorhus to approve

@satazor
Copy link

satazor commented Jan 22, 2017

@sindresorhus Should we remove support for node < 4 I guess?

@sindresorhus
Copy link
Owner

Should we remove support for node < 4 I guess?

Same answer as sindresorhus/to-single-quotes#14 (comment).

@sindresorhus
Copy link
Owner

@ruipneves Can you give the pull request a more descriptive title. Also, always link to the issue you're fixing using Fixes #134. That way it will close the issue when the pull request is merged.

@@ -30,7 +30,7 @@
"increment"
],
"dependencies": {
"number-is-integer": "^1.0.0"
"number-is-integer": "^1.0.1"
Copy link
Owner

Choose a reason for hiding this comment

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

Don't do unrelated changes. This change also makes no difference.

@@ -14,6 +14,9 @@ function round(fn, x, precision) {
var exponentNeg = precision > 0 ? 'e-' : 'e';
precision = Math.abs(precision);

if (fn === 'round') {
return Number(Math.sign(x) * (Math[fn](Math.abs(x) + exponent + precision) + exponentNeg + precision));
Copy link
Owner

Choose a reason for hiding this comment

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

Math[fn] => Math.round. We already know what the fn is.

@ruipneves ruipneves changed the title Hotfix for roundTo Bug fix for roundTo for negative numbers Jan 23, 2017
@ruipneves
Copy link
Contributor Author

@sindresorhus Thank you again for your feedback! The requested changes have been made.

@sindresorhus
Copy link
Owner

Also, always link to the issue you're fixing using Fixes #134.

134 was just an example. That should be the actual issue number of what you're fixing. I'd recommend reading https://guides.github.com/features/mastering-markdown/ and https://github.com/tiimgreen/github-cheat-sheet

@sindresorhus sindresorhus changed the title Bug fix for roundTo for negative numbers Fix bug with negative numbers Jan 23, 2017
@sindresorhus sindresorhus merged commit 1b5e5d6 into sindresorhus:master Jan 23, 2017
@sindresorhus
Copy link
Owner

Thank you @ruipneves :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rounding 5 with negative numbers
3 participants