Problem with division of variables

About advanced scenes, and the Thyme scripting language used in Algodoo.

Problem with division of variables

Postby hpstricker » Thu Feb 01, 2018 1:47 pm

I defined two variables Scene.my.up and Scene.my.time which are updated in update()

Then I tried to get Scene.my.up/Scene.my.time but this is what happened:

> Scene.my.up
847.1
> Scene.my.time
1484.1
> Scene.my.up/Scene.my.time
1
> 847.1/1484.1
0.57078362

That means, I get always 1 when dividing variables, but another number when dividing their values.

The problem seems to lie deeper, because I also get

> math.log(Scene.my.up)
9.38304
> math.log(Scene.my.time)
9.4027033
> math.log(Scene.my.up) - math.log(Scene.my.time)
0
What is wrong?

Thanks in advance
hpstricker
 
Posts: 16
Joined: Wed Jan 03, 2018 11:10 am

Re: Problem with division of variables

Postby hpstricker » Thu Feb 01, 2018 2:03 pm

I find a solution (but why is this necessary?)

First define two dummy variables up = Scene.my.up, time = Scene.my.time.
Then calculate x/y.

This works.
hpstricker
 
Posts: 16
Joined: Wed Jan 03, 2018 11:10 am

Re: Problem with division of variables

Postby hpstricker » Thu Feb 01, 2018 2:34 pm

But the problem doesn't cease to exist:

When using this inside (e)=>{} I get the same wrong result:

(e)=>{
x = Scene.my.up;
y = Scene.my.time;
ratio = x/y;
Console.print(ratio)
}

It is written constantly 1 to the console, even though x, y have different values.
hpstricker
 
Posts: 16
Joined: Wed Jan 03, 2018 11:10 am

Re: Problem with division of variables

Postby Kilinich » Fri Feb 02, 2018 12:21 am

Couldn't reproduce. In my case (Algodoo 2.1, Windows) there is no such problem at all.
Anyway, you colud use math.toFloat() to ensure you work with float, not integer variables.
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Problem with division of variables

Postby hpstricker » Mon Feb 05, 2018 1:42 pm

Hi Kilinich, would you mind having a look at the attached .phn file? The update function is defined for the gray box labeled with 1. On the console you can type Scene.my.up (a float), Scene.my.time (another float) and Scene.my.up/Scene.my.time (integer 1).
Thanks a lot in advance
Hans-Peter
Attachments
9.0.phn
(55.61 KiB) Downloaded 96 times
hpstricker
 
Posts: 16
Joined: Wed Jan 03, 2018 11:10 am

Re: Problem with division of variables

Postby Kilinich » Tue Feb 06, 2018 12:52 am

OMG :crazy:
There are many errors in code:
1) Actually Scene.my.up and Scene.my.time has same value (increased every update, but same) so there is no wonder division returns 1
2) You should use := for creating local scope variables, not =
3) You mess up with object type and your scene simple broke after save-load
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Problem with division of variables

Postby hpstricker » Fri Feb 09, 2018 3:06 pm

Sorry, but I don't understand:
1. Scene.my.up = Scene.my.up + (Scene.my.sensor.pos(1) > 3.0 ? 1.0 : 0.0);
Scene.my.time = Scene.my.time + 1.0;
Why do you say it's always the same?
2. Thanks for the hint to use :=, but it didn't help
3. Which messing up of object type do you mean? (And which kind of crash? There has never been a crash for me.)

Please check out Scene.my.up and Scene.my.time on the console: they are definitely NOT the same. But Scene.my.up/Scene.my.time is always 1. That's the problem/bug I am looking for a solution/answer.

Thanks in advance
Hans-Peter
hpstricker
 
Posts: 16
Joined: Wed Jan 03, 2018 11:10 am

Re: Problem with division of variables

Postby FRA32 » Tue Jun 05, 2018 6:13 pm

When I looked at your scene, the ratio only stayed 1 as long as the box was above 3. When I pulled the sensor box down below 3, the ratio decreased. Btw, as a Tip: You dont need math.tofloat when the scene.my.values are already non-integer numbers. I also recommend you to use poststep for your script since update ALWAYS runs, not just when the scene runs. Poststep only runs while the scene runs, once per scene frame.
FRA32
 
Posts: 227
Joined: Wed Dec 03, 2014 9:51 pm


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 5 guests

cron