# Behavior of floor(), ceiling(), etc

**URL:** <https://discourse.julialang.org/t/behavior-of-floor-ceiling-etc/4179>\
**Category:** General Usage\
**Created:** [June 9, 2017, 3:11pm UTC](https://discourse.julialang.org/t/behavior-of-floor-ceiling-etc/4179 "2017-06-09T15:11:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ko56](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ko56/32/2684_2.png) [@ko56](https://discourse.julialang.org/u/ko56)\
**Post date:** [June 9, 2017, 3:11pm UTC](https://discourse.julialang.org/t/behavior-of-floor-ceiling-etc/4179/1 "2017-06-09T15:11:05Z")

</div>

Is there a reason for the unusual behavior of floor(x), ceiling(x), etc., when x is a float type?  
I mean the fact that they return another float, not an integer. Unless you explicitly specify Int as the result type.

In mathematics, engineering, etc., these functions are well-known, and defined as producing integers. One therefore has a strong expectation that that’s how they would behave in Julia.  
(This unusual behavior caused a bug that cost me a few hours.)

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 9, 2017, 3:25pm UTC](https://discourse.julialang.org/t/behavior-of-floor-ceiling-etc/4179/2 "2017-06-09T15:25:27Z")

</div>

This is not unique to Julia; e.g. C++ has the same behaviour:  
[http://www.cplusplus.com/reference/cmath/floor/](http://www.cplusplus.com/reference/cmath/floor/)

---

<div class="post-metadata">

**Author:** ![ko56](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ko56/32/2684_2.png) [@ko56](https://discourse.julialang.org/u/ko56)\
**Post date:** [June 9, 2017, 3:38pm UTC](https://discourse.julialang.org/t/behavior-of-floor-ceiling-etc/4179/3 "2017-06-09T15:38:56Z")

</div>

Ok, I was not aware of that. But I’m still curious about the rationale, be it for C++ or Julia, as it contradicts common usage.

Would it create some other problem if the behavior were changed just in the case of floats, so as to accord with expectations?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 9, 2017, 3:40pm UTC](https://discourse.julialang.org/t/behavior-of-floor-ceiling-etc/4179/4 "2017-06-09T15:40:45Z")

</div>

> [@ko56](#):
>
> therefore has a strong expectation that that’s how they would behave in Julia.

These functions _do_ produce an integer. They just store the integer in a floating-point type by default. Floating-point integers are still integers.

This behavior is pretty universal in programming languages (it is true in C/C++, Python, Go, …), because floating-point values have a much wider range than (hardware) integers. e.g. what result would you want `floor(1e100)` to give?

---

<div class="post-metadata">

**Author:** ![ko56](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ko56/32/2684_2.png) [@ko56](https://discourse.julialang.org/u/ko56)\
**Post date:** [June 9, 2017, 4:12pm UTC](https://discourse.julialang.org/t/behavior-of-floor-ceiling-etc/4179/5 "2017-06-09T16:12:08Z")

</div>

Yes, I realize that “floating-point integers” are integers, in the same sense that 5 is a real number. But thanks for the floor(1e100) example, that’s convincing!

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [June 9, 2017, 7:19pm UTC](https://discourse.julialang.org/t/behavior-of-floor-ceiling-etc/4179/6 "2017-06-09T19:19:15Z")

</div>

At this point I think it’s useful to point out that there is a type argument to these functions, so you can do:

```julia
julia> floor(Int, 1.5)
1

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [June 9, 2017, 8:11pm UTC](https://discourse.julialang.org/t/behavior-of-floor-ceiling-etc/4179/7 "2017-06-09T20:11:57Z")

</div>

Maybe it is also useful to point out `floor(value)` is much faster than `floor(Int, value)`. Essentially, it is free (measured on intel i7). This could be important for hot loops! Details:

```julia
julia> using BenchmarkTools

julia> v = rand(1000000);

julia> f1(v) = ( s = 0.0 ; for value in v s+=floor(value) ; end ; s )
f1 (generic function with 1 method)

julia> f2(v) = ( s = 0 ; for value in v s+=floor(Int,value) ; end ; s )
f2 (generic function with 1 method)

julia> f3(v) = ( s = 0.0 ; for value in v s+=value ; end ; s )
f3 (generic function with 1 method)

julia> f1(v), f2(v), f3(v) # run once for good luck
(0.0, 0, 500335.41928945674)

julia> @btime f1($v) # round to Float
  1.040 ms (0 allocations: 0 bytes)
0.0

julia> @btime f2($v) # round to Int
  2.368 ms (0 allocations: 0 bytes)
0

julia> @btime f3($v) # do no rounding
  1.037 ms (0 allocations: 0 bytes)
500335.41928945674

```

Rough calculation:  
Extra-time for Float rounding: 1.040ms - 1.037ms ~ 3ns (free)  
Extra-time for Int rounding: 2.368ms - 1.037ms ~ 1331ns (400X)

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 9, 2017, 8:39pm UTC](https://discourse.julialang.org/t/behavior-of-floor-ceiling-etc/4179/8 "2017-06-09T20:39:54Z")

</div>

2x as far as I can see. But nice example.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [June 9, 2017, 8:47pm UTC](https://discourse.julialang.org/t/behavior-of-floor-ceiling-etc/4179/9 "2017-06-09T20:47:35Z")

</div>

Compare the times when an “empty” loop is subtracted (i.e. `f3(v)`). These are the nanoseconds in the calculation at the bottom, and they are \>400X.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 9, 2017, 8:56pm UTC](https://discourse.julialang.org/t/behavior-of-floor-ceiling-etc/4179/10 "2017-06-09T20:56:24Z")

</div>

Ahh, I see what you mean.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 9, 2017, 8:57pm UTC](https://discourse.julialang.org/t/behavior-of-floor-ceiling-etc/4179/11 "2017-06-09T20:57:23Z")

</div>

But I’m not sure that’s a very useful way of thinking about it. The actual calculation took twice as long.
