[erlang-questions] math ceil floor

Richard A. O'Keefe ok@REDACTED
Fri Jun 3 04:59:12 CEST 2016



On 2/06/16 9:56 PM, Dan Gudmundsson wrote:
> Hi
> 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.
> The implementation in the PR currently returns integers and also 
> implements ceilf and floorf which returns floats.


Just what we needed, yet another name for ffloor.
Let me draw your attention to
http://www.lispworks.com/documentation/HyperSpec/Body/f_floorc.htm
which defines
     floor      ffloor
     ceiling    fceiling
     round      fround
     truncate   ftruncate
and to ANSI INCITS 319-1998, where #floor, #ceiling, #rounded,
and #truncated return Integers, whereas x integerPart (truncation),
and x // 1 (flooring) answer the same type as x.

Since your 'ceil' function doesn't have the semantics of C's,
it's not clear why you want to use the C name.

For what it's worth, my Smalltalk library includes several
versions of rounding differing in what they do with a value
exactly half-way between two integers.  floor(X+0.5) is only
one alternative...



More information about the erlang-questions mailing list