[erlang-questions] math ceil floor

Grzegorz Junka list1@REDACTED
Sat Jun 4 10:08:02 CEST 2016


On 04/06/2016 06:33, Dan Gudmundsson wrote:
> On Fri, Jun 3, 2016 at 11:38 PM Samuel <samuelrivas@REDACTED 
> <mailto:samuelrivas@REDACTED>> wrote:
>
>     >> I made pull request https://github.com/erlang/otp/pull/1084which
>     >> implements ceil and floor in the math module.
>     >>
>     >> The question is, should they return integer or floats?
>     >
>     >
>     > Yes, they should.  For each of floor, ceiling, round, truncate
>     there are
>     > two sensible functions: one returns the appropriate integer as
>     an integer,
>     > and the other returns it as a floating-point number.
>
>     What is the use case when you want floor/ceil to return a float and
>     not an integer in Erlang?
>
>     Since Erlang has bignums, which I understand can represent the integer
>     part of any float,  and those operations are supposed to return an
>     integer I can't see a reason for a float result (other than, maybe,
>     performance).
>
>
> My reason for a variant that returns a float is purely performance the 
> beam compiler is good at avoiding conversion/type checking floats in 
> code blocks that handle floats, and sometimes you want to continue to 
> work with the result as float, e.g. multiply the result with another 
> float.
>

Is there any reason why there may not be two functions, one returning 
float and one returning integer? One function won't cover all scenarios 
and trying to discuss which one is better in that context will never 
end. If only one type of function is possible then it would be best to 
verify which usage is more common out there - in real applications - so 
that the decision isn't speculative but backed by some data.

Grzegorz
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160604/03c2b0a5/attachment.htm>


More information about the erlang-questions mailing list