Timer with 0.1 increment

malukorj

  • Posts: 22
Whe I creat a timer with a 0.1 increment I receive sometimes strange numbers, like 3.299999.

How can I fix that ? Or it's a Stencyl bug ?


Joe

  • *
  • Posts: 2480


malukorj

  • Posts: 22
I saw your post, but I don't understand how to fix that, even with your post, sorry but I'm a graphic designer.
I try doing something like that on my attatchment. is that right ??

Because it's not working.

p.s: sorry about my english, that's not my primary lang.


nev

  • *
  • Posts: 386
No problem with the English: it is not my primary language too. ;)
Where did you put the timer block? Usually you have to put in "when created", because if put in "always" it can give problems.

Have a look here:
http://static.stencyl.com/pedia2/blocks/flow/flow_time/periodic.html
for the correct use of timers (under "Advanced Uses" there is an example similar to yours).

Sunflower

  • Posts: 591
Nev: it isn't anywhere, it's a separate event. ;)

Malukorj: strangely, when I tried something similar to your solution (with addition, multiplication, rounding, division), the statements it printed were normal, with only one digit after point. If it still persists, however, you might want to try another solution; for example, adding 1 rather than 0.1 to timer, and dividing it by 10 when needed (or if it still doesn't work, make a text [number without last digit] + "." + [last digit] ).

malukorj

  • Posts: 22
No problem with the English: it is not my primary language too. ;)
Where did you put the timer block? Usually you have to put in "when created", because if put in "always" it can give problems.

Have a look here:
http://static.stencyl.com/pedia2/blocks/flow/flow_time/periodic.html
for the correct use of timers (under "Advanced Uses" there is an example similar to yours).

I was trying without "created". I was using the: "Add Event / Time / Every N second".
So I deleted this one and create a Basic / When Creating event, with your round block.
And my timer it's not working.

My timer is a Scene Behavior with a attibute time.

I think Stencyl need to upgrade the help section with this kind of solution.

If it's an inherit limitation of floating point numbers maybe a official explanation on the Help page will explain a lot for the new users, not programmers like me.

Thanks Nev, I'm gonna check your link right now, and Sunflower post, if I suceed I'll post the full design block here, for future users.

I'm building for iOS.


-------------------UPDATE-------------------

So I'm trying with 1/10 and no luck.
Gonna try with a text [number without last digit] + "." + [last digit].

« Last Edit: May 16, 2012, 02:27:34 pm by malukorj »

hansbe

  • Posts: 262
Since I did a computer arithmetic lab today I thought I might 'geek' this one up a bit.   ;D

Double precision floating point should represent numbers to an acurracy of about 2^(-51.5).

For your 0.1 seconds that error would be about 30 attoseconds per iteration.

Linearly your error would build up to an accumulated error of 1 second over 100.91 million years running that code. The error build-up does however speed up abit, as the accuracy drops when the time counter value gets large compared to what you add to it. The error is anyway still small enough to neglect for almost all applications.

Two ways to handle it are:
 1. Keep the error and round off when presenting the number.
 2. Add 1 every time and divide by 10 before using the number (you'll still need to round off, though).

Choosing the amount of digits when converting a Number to a String doesn't seem to be supported in code blocks though. Although it's not a bug it would be a missing feature ..
In the meantime in the "anything as text" block you could probably write "_Time.toFixed(1)" when you use the number in a draw or print block. I then assume _Time is the internal representation of the number you want to present (it would be the default but to be sure the internal representation of attributes can be seen on the attribute tab) and 1 means to get 1 digit after the decimal point. i.e. 3.2999 will be converted to the string "3.3".


« Last Edit: May 16, 2012, 04:29:14 pm by hansbe »

malukorj

  • Posts: 22
I try with 1/10 but no luck. Maybe my design blocks are wrong.

hansbe

  • Posts: 262
Show me the block where you draw / output the number and I'll show you what I mean ..

malukorj

  • Posts: 22
That's my created block with 1/10 and my drawing block with draw text, but I have to change to labels for iOS later.

hansbe

  • Posts: 262
Ok so if you change your 'When drawing' block to the below. Does that work ?

malukorj

  • Posts: 22
Ok so if you change your 'When drawing' block to the below. Does that work ?

No doesn't work. Just show the "_Time.toFixed(1)" on the screen.

captaincomic

  • *
  • Posts: 6108
You'll need to put it in a code block (the small one without the notch), otherwise it will just be interpreted as a string.

hansbe

  • Posts: 262
Sorry, I didn't play-test it.

The below code should work better.

If you want to add a string in front of it / after it you can use + and enclose text in "".

E.g.:

"Elapsed time : " + _Time.toFixed(1)