Floor

Want to talk about something that isn't covered by another category?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9446
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Floor

Post by richmond62 » Sat Aug 23, 2014 8:23 pm

How to perform 'floor' in LIvecode.
Attachments
floor.livecode.zip
Here it is.
(10.23 KiB) Downloaded 255 times

Peregrine
Posts: 53
Joined: Sun Jan 10, 2010 10:33 pm
Location: Always changing!
Contact:

Re: Floor

Post by Peregrine » Sat Aug 23, 2014 9:41 pm

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.
Charles Buchwald
xTalk coding since 1990
Developer of lcResTool and lcMover and other developer tools
https://charlesbuchwald.com

FLCo
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 36
Joined: Thu Jul 27, 2006 5:40 am
Location: England
Contact:

Re: Floor

Post by FLCo » Sat Aug 23, 2014 10:07 pm

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
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

Peregrine
Posts: 53
Joined: Sun Jan 10, 2010 10:33 pm
Location: Always changing!
Contact:

Re: Floor

Post by Peregrine » Wed Aug 27, 2014 7:28 pm

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
Charles Buchwald
xTalk coding since 1990
Developer of lcResTool and lcMover and other developer tools
https://charlesbuchwald.com

FLCo
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 36
Joined: Thu Jul 27, 2006 5:40 am
Location: England
Contact:

Re: Floor

Post by FLCo » Wed Aug 27, 2014 8:02 pm

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.
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

Peregrine
Posts: 53
Joined: Sun Jan 10, 2010 10:33 pm
Location: Always changing!
Contact:

Re: Floor

Post by Peregrine » Wed Aug 27, 2014 11:25 pm

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
Charles Buchwald
xTalk coding since 1990
Developer of lcResTool and lcMover and other developer tools
https://charlesbuchwald.com

FLCo
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 36
Joined: Thu Jul 27, 2006 5:40 am
Location: England
Contact:

Re: Floor

Post by FLCo » Thu Aug 28, 2014 7:28 am

-- 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

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Floor

Post by [-hh] » Thu Sep 04, 2014 9:43 am

..........
Last edited by [-hh] on Thu Sep 04, 2014 11:59 am, edited 1 time in total.
shiftLock happens

FLCo
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 36
Joined: Thu Jul 27, 2006 5:40 am
Location: England
Contact:

Re: Floor

Post by FLCo » Thu Sep 04, 2014 11:02 am

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

Post Reply

Return to “Off-Topic”