Skip to content

Conversation

bitwalker
Copy link
Contributor

Float.round allows rounding floats to arbitrary precision, as well
as specifying how midpoint rounding is performed (up/down).

This should provide accurate rounding for up to 15 digits of
precision (the max guaranteed by the IEEE-754 standard for
64-bit floating point numbers). If you can think of anything my
tests won't catch, let me know.

@bitwalker
Copy link
Contributor Author

I initially wanted to place this next to the Kernel.round function, since they do the same thing (more or less), and it might've been useful to have in guard clauses for the same reasons Kernel.round would be, but my initial effort there failed when I couldn't use and, in, and presumably .. at the point where Kernel.round is defined. Anyways, Float seems like a good module to put this in, and might make more sense anyways.

@@ -125,4 +125,28 @@ defmodule Float do
end
end

@doc """
Rounds a floating point value to an arbitrary number of fractional digits
with an optional midpoint rounding mode (:up or :down, defaults to :up).
Copy link
Member

Choose a reason for hiding this comment

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

Please mention in the docs that precision needs to be in between 0 and 15. :)

@josevalim
Copy link
Member

I agree this belongs to float. Thanks for the pull request! :D

Float.round allows rounding floats to arbitrary precision, as well
as specifying how midpoint rounding is performed (up/down).
@bitwalker
Copy link
Contributor Author

Added the clarification on the available range for precision. Let me know if you spot anything else!

josevalim pushed a commit that referenced this pull request Dec 13, 2013
Add Float.round function
@josevalim josevalim merged commit 35f599d into elixir-lang:master Dec 13, 2013
@bitwalker
Copy link
Contributor Author

Thanks!

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

Successfully merging this pull request may close these issues.

2 participants