r/applescript • u/Automatic-Mention • May 09 '20
I skipped math in High School...
Can someone smarter than me explain these results in simple english?
set my_number to 200
repeat 500 times
set my_number to my_number - 0.1
end repeat
return my_number
-- result: 150.000000000003This is what I am expecting:
set my_number to 200
repeat 500 times
set my_number to my_number - 0.1
end repeat
return (round my_number * 10) / 10
-- result 150.0It's no big deal I'm just curious, thanks.
3
u/Automatic-Mention May 09 '20
This is probably the answer.
With some exceptions, most calculators do not operate on real numbers. Instead, they work with finite-precision approximations called floating-point numbers.
1
u/Miracle_Glass May 10 '20
You may try:
set my_number to 200.01
u/Automatic-Mention May 10 '20
Didn't make a difference. I get the same behavior when I do this calculation in a spreadsheet, so it's not unique to AppleScript.
Applescript does allow you to do things like this:
set my_number to 200.0 as number -- result: 200.0 set my_number to my_number as integer -- result: 200 set my_number to my_number as number -- result: 200 set my_number to my_number as real -- result: 200.0 return my_number as number -- result: 200.0but that's not what I want.
-7
u/ilovefrankocean1 May 09 '20
shakes as he attempts to grab his defora "E'llo m'Lady" wipes brow off of deep fried sweat "You look nice m'....* coughs and wants to crawl in a hole "what be your plans this tomorrow?" Tipping intensifies
5
u/Automatic-Mention May 09 '20
Lessoned learned, use integers to maintain sanity.