Floor
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Livecode Opensource Backer
- Posts: 9476
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Floor
How to perform 'floor' in LIvecode.
- Attachments
-
- floor.livecode.zip
- Here it is.
- (10.23 KiB) Downloaded 259 times
Re: Floor
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.
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.
Charles Buchwald
xTalk coding since 1990
Developer of lcResTool and lcMover and other developer tools
https://charlesbuchwald.com
xTalk coding since 1990
Developer of lcResTool and lcMover and other developer tools
https://charlesbuchwald.com
-
- VIP Livecode Opensource Backer
- Posts: 36
- Joined: Thu Jul 27, 2006 5:40 am
- Location: England
- Contact:
Re: Floor
This handles when the negative number is a whole number...Peregrine wrote: function Floor pValue
if pValue < 0 then return trunc(pValue) - 1 else return trunc(pValue)
end Floor
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
The Flexible Learning Company
• ChartMaker: www.FlexibleLearning.com/chartmaker
• ControlManager: www.FlexibleLearning.com/controlmanager
• The Scripter's Scrapbook: www.FlexibleLearning.com/ssbk
• DatePicker: www.FlexibleLearning.com/datepicker
• ChartMaker: www.FlexibleLearning.com/chartmaker
• ControlManager: www.FlexibleLearning.com/controlmanager
• The Scripter's Scrapbook: www.FlexibleLearning.com/ssbk
• DatePicker: www.FlexibleLearning.com/datepicker
Re: Floor
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
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
Charles Buchwald
xTalk coding since 1990
Developer of lcResTool and lcMover and other developer tools
https://charlesbuchwald.com
xTalk coding since 1990
Developer of lcResTool and lcMover and other developer tools
https://charlesbuchwald.com
-
- VIP Livecode Opensource Backer
- Posts: 36
- Joined: Thu Jul 27, 2006 5:40 am
- Location: England
- Contact:
Re: Floor
So floor(5) = trunc(5) -1 = 4function Floor pValue
if pValue is an integer OR pValue < 0 then return trunc(pValue) - 1
else return trunc(pValue)
end Floor
Don't think so. Read my post above.
The Flexible Learning Company
• ChartMaker: www.FlexibleLearning.com/chartmaker
• ControlManager: www.FlexibleLearning.com/controlmanager
• The Scripter's Scrapbook: www.FlexibleLearning.com/ssbk
• DatePicker: www.FlexibleLearning.com/datepicker
• ChartMaker: www.FlexibleLearning.com/chartmaker
• ControlManager: www.FlexibleLearning.com/controlmanager
• The Scripter's Scrapbook: www.FlexibleLearning.com/ssbk
• DatePicker: www.FlexibleLearning.com/datepicker
Re: Floor
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
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
Charles Buchwald
xTalk coding since 1990
Developer of lcResTool and lcMover and other developer tools
https://charlesbuchwald.com
xTalk coding since 1990
Developer of lcResTool and lcMover and other developer tools
https://charlesbuchwald.com
-
- VIP Livecode Opensource Backer
- Posts: 36
- Joined: Thu Jul 27, 2006 5:40 am
- Location: England
- Contact:
Re: Floor
-- Comment removed --
Last edited by FLCo on Thu Sep 04, 2014 11:04 am, edited 1 time in total.
The Flexible Learning Company
• ChartMaker: www.FlexibleLearning.com/chartmaker
• ControlManager: www.FlexibleLearning.com/controlmanager
• The Scripter's Scrapbook: www.FlexibleLearning.com/ssbk
• DatePicker: www.FlexibleLearning.com/datepicker
• ChartMaker: www.FlexibleLearning.com/chartmaker
• ControlManager: www.FlexibleLearning.com/controlmanager
• The Scripter's Scrapbook: www.FlexibleLearning.com/ssbk
• DatePicker: www.FlexibleLearning.com/datepicker
-
- VIP Livecode Opensource Backer
- Posts: 36
- Joined: Thu Jul 27, 2006 5:40 am
- Location: England
- Contact:
Re: Floor
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.
The Flexible Learning Company
• ChartMaker: www.FlexibleLearning.com/chartmaker
• ControlManager: www.FlexibleLearning.com/controlmanager
• The Scripter's Scrapbook: www.FlexibleLearning.com/ssbk
• DatePicker: www.FlexibleLearning.com/datepicker
• ChartMaker: www.FlexibleLearning.com/chartmaker
• ControlManager: www.FlexibleLearning.com/controlmanager
• The Scripter's Scrapbook: www.FlexibleLearning.com/ssbk
• DatePicker: www.FlexibleLearning.com/datepicker