Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Highlighted
Level 7

Is there a Ceiling function?

Most languages have a ceiling function. That is to say a function that takes a number and rounds it up to the next largest integer

Gives results like this:
1.1 -> 2
1.5 -> 2
1.9 -> 2
2.0 -> 2
2.1 -> 3

I don't see it in the list of the Built-in functions (http://sdkdocs.roku.com/display/sdkdoc4 ... -functions). Does Brightscript not have this basic method?
0 Kudos
7 Replies
Highlighted
Level 7

Re: Is there a Ceiling function?

No but you can easily do something like

if Int(x) = x then ceiling = x else ceiling = Int(x) + 1


--Mark
0 Kudos
Highlighted
Level 10

Re: Is there a Ceiling function?

"greengiant83" wrote:
Most languages have a ceiling function. ... Does Brightscript not have this basic method?


That's not true. Most languages have "floor" and "round" kind of conversion float->int. And the reason "ceiling" might not be included is you can easily do that with floor(x + 1 - epsilon), where epsilon is some platform specific small value like 1e-30. Or you know, floor(x + 0.9999...). Duh.
0 Kudos
Highlighted
Level 10

Re: Is there a Ceiling function?

Most - can't be used as an adjective in this thread. The only language I know of with a ceiling function is javascript. Spreadsheets have the function as well, but those aren't programming languages.
Int and Fix are already filling the requirement, so there isn't a real need for a ceiling specific function in brightscript.
0 Kudos
Highlighted
Level 7

Re: Is there a Ceiling function?

"The only language I know of with a ceiling function is javascript."

I guess you haven't used any of the top ten programming languages then: http://bestteneverything.com/top-ten-mo ... ages-2013/

They all have a ceil/ceiling function.

Anyway, would it make sense to do something like "-(Int(-x))" instead of the other solutions?
0 Kudos
Highlighted
Level 10

Re: Is there a Ceiling function?

Yup, guess not. Anyway, you could show off by creating your own ceiling function in Brightscript - impress your friends.
0 Kudos
Highlighted
Level 7

Re: Is there a Ceiling function?

There is: just use Int(variable as float)

http://sdkdocs.roku.com/pages/viewpage. ... tAsInteger
0 Kudos
Highlighted
Level 10

Re: Is there a Ceiling function?

"damon1337" wrote:
There is: just use Int(variable as float)

No. What int() does is actually floor():
BrightScript Debugger> ? int(1.4), int(-1.4)
1 -2

The right way to do it is what RokuMarkn said. Slightly optimized:
function ceiling(x):
i = int(x)
if i < x then i = i + 1
return i
end function

Or a one-liner for the console:
BrightScript Debugger> ceiling = function(x): return int(x) + sgn(x - int(x)): end function
BrightScript Debugger> ? ceiling(1.4), ceiling(-1.4)
2 -1
0 Kudos