Math problem
Bob Sneidar
bobs at twft.com
Mon Feb 13 15:12:55 EST 2012
For anyone who is good at math, here is (for me) a sticky problem. I can of course solve this with a conditional statement, and have done, but I want to see if there is a way by formula alone to solve it. This is not essential, just an exercise in math.
I have 2 times, a start time and an end time. I want to calculate the hours between them, but in increments of say a quarter of an hour. If there is even one minute or second over the increment, I want to add the increment. Get it?
So given 9:00 AM and 10:00 AM I want 1 as a result, but given 9:00 AM and 10:01 AM I want 1.25, assuming a 15 minute increment.
I did this in a handler:
FUNCTION countTheHours pStartTime, pEndTime, pIncrement
IF pIncrement is empty THEN put 0 into pIncrement
TRY
convert pStartTime to seconds
convert pEndTime to seconds
put ((pEndTime - pStartTime) / 3600) into theTotalTime -- decimal hours
put (theTotalTime div pIncrement) * pIncrement into theResult -- integer hours
put theTotalTime mod pIncrement into theModulus -- modulus
IF theModulus is not 0 THEN add pIncrement to theResult
CATCH theError
put "ERROR: Not a valid time!" into theResult
END TRY
return theResult
END countTheHours
This handler works famously. Note however the 2 lines to get the modulus and the conditional statement. Is there a way to do this with nothing but a formula?
Bob
More information about the use-livecode
mailing list