|
| 20 Oct 2016 09:47 PM |
How do you make a timer count down from 10 and prints the decimals?
e.g: 10, 9.9, 9.8, ect.
|
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 20 Oct 2016 09:50 PM |
for timer = 100, 0, -1 do print(timer/10) wait(0.1) end |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 20 Oct 2016 09:54 PM |
time = 10 while not time == 0 and wait(.1) do time = time - 0.1 print(time) end
|
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 20 Oct 2016 09:54 PM |
| that'll actually never end :) |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
| |
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 20 Oct 2016 10:09 PM |
You keep subtracting by 0.1 (which can't be perfectly stored) so the error accumulates over time. Also the condition is wrong, NOT takes precedence over == but that's just another problem.
Through my test in 5.3, the closest to 0 I got from that loop was this: 1.8790524691781e-14 |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 20 Oct 2016 10:20 PM |
Why can't it perfectly store such a simple number as 0.1? Does that have to do with floats? Also, what's wrong in the second line here? At this point it's not as coherent as your solution and doesn't fix the problem of accuracy, I'm just wondering how to use "not"
tock = 10 while not tock <.1 do wait() tock = tock - 0.1 print(tock) end
23:18:46.896 - Workspace.Script:2: attempt to compare boolean with number 23:18:46.897 - Stack Begin 23:18:46.897 - Script 'Workspace.Script', Line 2 23:18:46.897 - Stack End |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 20 Oct 2016 10:22 PM |
| Could rounding the number after every operation prevent the inaccuracy, or is it possible that the inaccuracy is too large and it would round in the undesirable direction? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 20 Oct 2016 10:23 PM |
"Why can't it perfectly store such a simple number as 0.1? Does that have to do with floats?" Yeah. Floats are essentially scientific notation in binary, you can't store 0.1 in binary (it repeats forever)
"Also, what's wrong in the second line here?"
The problem is while not time == 0 do
is really the same as: while (not time) == 0 do
Which will always evaluate to false because a boolean will never equal an integer |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 20 Oct 2016 10:24 PM |
"Could rounding the number after every operation prevent the inaccuracy, or is it possible that the inaccuracy is too large and it would round in the undesirable direction?" It's actually just better to use integers and divide when needed (look at my solution) so you don't have to deal with floating point imprecision in the first place. |
|
|
| Report Abuse |
|
|