-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Add floor and ceil functions to Float #1924
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
Conversation
Thanks @bitwalker! I have a couple things: we don't need to return error. Just use |
Ah, if you receive an integer, it should be a no-op. :) That should help simplify stuff. Finally, we should probably add a test case for "-0.32". |
@josevalim I think the proper place is |
@meh That's a fair point, thanks. Let's keep them in float, but with no |
Sure thing, I'll push those changes shortly! |
Let me know if that looks good. I'm not sure how I feel about not returning |
@bitwalker in the case of parse it makes sense to return an error in case of failed parse, since you could be giving it unchecked inputs. In the case of |
Good point, I suppose I'm a bit overzealous with validation sometimes, and you're right it probably doesn't make sense for this. My only thought on returning an error from |
@bitwalker that's why you only add two heads with |
-56 | ||
|
||
""" | ||
def ceil(num) do |
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.
You probably want this:
def ceil(num) when is_integer(num), do: num
def ceil(num) when is_float(num) do
...
end
This way, if anything else besides float/integer is given, it will raise like :erlang.round
does.
Oh man, I can't believe I forgot using function guards, I'll fix that up and add that test :) |
If you guys think this looks good I can squash the commits prior to merging. |
It looks great, please squash and I will merge! :D |
Squashed! |
Add floor and ceil functions to Float
Thanks! |
Let me know if you spot anything wrong with this, I think the tests are fairly comprehensive, but if you can think of any edge cases I may have missed, I'll certainly add tests for them.
Right now this will take either an integer or a float, truncate it as part of the floor/ceil operation, then convert it back to a float. The result of calling Float.floor or Float.ceil will always be a float this way. I think that makes more sense than returning an integer, but let me know if that's an incorrect assumption.