Page 1 of 1

Floor

Posted: Sat Aug 23, 2014 8:23 pm
by richmond62
How to perform 'floor' in LIvecode.

Re: Floor

Posted: Sat Aug 23, 2014 9:41 pm
by Peregrine
Here's an alternative bit of scripting:

function Floor pValue
if pValue < 0 then return trunc(pValue) - 1 else return trunc(pValue)
end Floor

Here's a ceiling function, just to kind of fill out the thread:

function Ceiling pValue
if pValue < 0 then return trunc(pValue) else return trunc(pValue) + 1
end Ceiling

Note that it's been submitted as a contribution to the engine (by Mark W.) and should appear in some future version of LC as a native function.

Re: Floor

Posted: Sat Aug 23, 2014 10:07 pm
by FLCo
Peregrine wrote: function Floor pValue
if pValue < 0 then return trunc(pValue) - 1 else return trunc(pValue)
end Floor
This handles when the negative number is a whole number...

function Math.floor pNum
if pNum < 0 then
if trunc(pNum)=pNum then return pNum
else return trunc(pNum) - 1
else return trunc(pNum)
end Math.floor

Re: Floor

Posted: Wed Aug 27, 2014 7:28 pm
by Peregrine
Whoops! Here are both floor and ceiling functions accounting for negative integers:

function Floor pValue
if pValue is an integer OR pValue < 0 then return trunc(pValue) - 1
else return trunc(pValue)
end Floor

function Ceiling pValue
if pValue is an integer OR pValue > 0 then return trunc(pValue) + 1
else return trunc(pValue)
end Ceiling

Re: Floor

Posted: Wed Aug 27, 2014 8:02 pm
by FLCo
function Floor pValue
if pValue is an integer OR pValue < 0 then return trunc(pValue) - 1
else return trunc(pValue)
end Floor
So floor(5) = trunc(5) -1 = 4

Don't think so. Read my post above.

Re: Floor

Posted: Wed Aug 27, 2014 11:25 pm
by Peregrine
Thanks, FLCo. I have to admit to being confused about how to properly handle the integer.

Wikipedia http://en.wikipedia.org/wiki/Floor_and_ ... _functions says this:
In mathematics and computer science, the floor and ceiling functions map a real number to the largest previous or the smallest following integer, respectively. More precisely, floor(x) = \lfloor x\rfloor is the largest integer not greater than x and ceiling(x) = \lceil x \rceil is the smallest integer not less than x.

I took this to mean that the floor of 5 is 4. Thanks to your persistence, I see that the floor of 5 is 5, and the ceiling of 5 is 5.

Is this a record for number of incorrect scripts? How about these:

function Floor pValue
if pValue is an integer OR pValue > 0 then return trunc(pValue)
else return trunc(pValue) - 1
end Floor

function Ceiling pValue
if pValue is an integer OR pValue < 0 then return trunc(pValue)
else return trunc(pValue) + 1
end Ceiling

Re: Floor

Posted: Thu Aug 28, 2014 7:28 am
by FLCo
-- Comment removed --

Re: Floor

Posted: Thu Sep 04, 2014 9:43 am
by [-hh]
..........

Re: Floor

Posted: Thu Sep 04, 2014 11:02 am
by FLCo
I have no idea why I wrote that... You and Charles are absolutely correct. I shall try to delete my comment to remove any further confusion.