Re: round 6.0 rounding down -->5 ?
Re: round 6.0 rounding down -->5 ?
- Subject: Re: round 6.0 rounding down -->5 ?
- From: Nigel Garvey <email@hidden>
- Date: Sun, 12 Aug 2001 15:37:28 +0100
Paul Skinner wrote on Sat, 11 Aug 2001 20:55:14 -0400:
>
set remainingseconds to (remainingminutes - thefullminutes) * 60
>
--> 6.0
>
tell current application to ,
>
round 6.0 rounding down
>
--> 5
>
set thefullseconds to (round remainingseconds rounding down)
>
--> 5
>
>
I was never really strong at math. But something seems amiss here.
I can't duplicate the result of your second line, but the rest depends on
the values of remainingminutes and thefullminutes:
set remainingminutes to 6.54
set thefullminutes to 6.44
set remainingseconds to (remainingminutes - thefullminutes) * 60
--> 6.0
set thefullseconds to (round remainingseconds rounding down)
--> 5
set thefullseconds to remainingseconds div 1
--> 5
Other start values that are 0.1 apart will produce the correct end result
of 6. Floating point operations (arithmetic with real numbers) don't
always produce precisely accurate results. They're sometimes a whisker
under or whisker over. If you ask AppleScript what the result is, it'll
compensate for the inaccuracy in its reply, but the inaccuracy will still
be there in the variable you've set. If you round down a result that's
slightly below 6.0, it'll be rounded down to 5. Similarly, rounding up a
value that's slightly above 6.0 (as I get with the start values 36.54 and
36.44) will return 7. The inaccuracy can also lead to other puzzling
results:
set remainingseconds to (remainingminutes - thefullminutes) * 60
--> 6.0
remainingseconds = 6.0
--> false
remainingseconds as integer
--> error: Can't make 6.0 into a integer.
For your purposes, coercing the result to string first might help:
set thefullseconds to (remainingseconds as string) div 1
--> 6
The inaccuracy is ironed out for the coercion to string and the string is
then automatically coerced back to a real for the math operation.
NG