Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: Can ceil/floor return an integer?

Why don't ceil() and floor() return an integer? How can I return an integer?

a = 10
b = 3
typeof(a/b)
  ## Float64

c = ceil(a/b)
typeof(c)
  ## Float64

This issue bothered me in the context of calculating a range, e.g.

k = 0:1:c
  ## 0.0:1.0:4.0
typeof(k)
  ## StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}}

Full disclosure: I think I have an answer, so I'm going to answer my own question, however please do post an answer if you have a better one. Hopefully next time someone looks, they'll find an answer easily here.

like image 921
PatrickT Avatar asked Apr 13 '21 02:04

PatrickT


People also ask

What does ceil () function do?

The ceil() function computes the smallest integer that is greater than or equal to x.


Video Answer


1 Answers

At times you want ceil() and floor() to return something other than an integer, e.g. Inf, NaN. However, you can return an integer like this:

julia> c = ceil(Int64, a/b)
julia> typeof(c)
Int64 

julia> k = 0:1:c
0:1:4

julia> typeof(k)
StepRange{Int64, Int64}

See the docs

like image 138
PatrickT Avatar answered Oct 10 '22 18:10

PatrickT