greengiant83
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2012
03:14 PM
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?
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?
7 REPLIES 7

RokuMarkn
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2012
03:24 PM
Re: Is there a Ceiling function?
No but you can easily do something like
--Mark
if Int(x) = x then ceiling = x else ceiling = Int(x) + 1
--Mark
EnTerr
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2012
05:27 PM
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.
destruk
Streaming Star
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2012
05:44 PM
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.
Int and Fix are already filling the requirement, so there isn't a real need for a ceiling specific function in brightscript.
rjbrown
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2013
03:10 PM
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?
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?
destruk
Streaming Star
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2013
04:22 PM
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.
damon1337
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2015
11:28 AM
Re: Is there a Ceiling function?
EnTerr
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2015
02:06 PM
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