return type of ceil and floor

90 views
Skip to first unread message

Gabor

unread,
May 2, 2013, 3:49:42 AM5/2/13
to julia...@googlegroups.com
The return type of the ceil and floor functions seem to depend on the argument type.
 
Is this the intended behaviour?
 
> ceil(5)
5
> ceil(10/2)
5.0
> typeof(ceil(5))
Int32
> typeof(ceil(10/2))
Float64

 

Tim Holy

unread,
May 2, 2013, 9:57:02 AM5/2/13
to julia...@googlegroups.com
On Thursday, May 02, 2013 12:49:42 AM Gabor wrote:
> The return type of the ceil and floor functions seem to depend on the
> argument type.
>
> Is this the intended behaviour?

Yes. But from your question, I bet what you're looking for is iceil() and
ifloor().

--Tim

Gabor

unread,
May 2, 2013, 11:33:37 AM5/2/13
to julia...@googlegroups.com
Hi Tim,
 
I know iceil() and ifloor(),
they work consistently and according to the documentation.
 
However, for ceil() and floor() the documentation says:
ceil(x[, digits[, base]]) → FloatingPoint
floor(x[, digits[, base]]) → FloatingPoint
 
An integer return type is clearly not a FloatingPoint,
so either the documentation is wrong,
or (as I suspect) this is not the intended behaviour.
 
Gabor

Stefan Karpinski

unread,
May 2, 2013, 1:59:32 PM5/2/13
to Julia Users
The documentation is wrong here. The idea is that ceil and floor return a value that is the same type as x, which is often faster and/or better for the type stability of the ongoing computation (and is also what the functions of the same name do in C).

Stefan Karpinski

unread,
May 2, 2013, 2:08:57 PM5/2/13
to Julia Users
For what it's worth, I really dislike the type annotations in the manual. Generic functions are about a concept and the documented types are usually wrong even in base and also since it's expected that people extend generic functions to other types they are inherently wrong. It's a little unusual, but Julia's documentation for generic functions must be about what *concept* a function implements, not just the specific methods that Base happens to provide. That way others can understand how their extensions of these functions should work. Of course, documenting a concept is much harder than just describing what something does.

Stefan Karpinski

unread,
May 2, 2013, 2:09:39 PM5/2/13
to Julia Users

Gabor

unread,
May 2, 2013, 2:48:07 PM5/2/13
to julia...@googlegroups.com
 
Stefan, thanks for the explanation and the documentation fix.
Reply all
Reply to author
Forward
0 new messages