Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / If / Elseif issue [Tier 1]

Author
Message
Funnell7
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 6th Aug 2012 16:38 Edited at: 13th Jan 2015 20:14
I think I may be going mad as I cannot work out why this simple bit of code just won't work! Would be good if someone would mind taking a look for me...



Basically, the RandomSpeed float should be increased by 0.05. Then when RandonSpeed = 1.0 it should hit the Print command (first elseif statement). However, it just goes straight to the else statement?... Am I going nuts? I've looked at this for ages as I'm certain there must be something I've missed but its very simple and I just don't get it... Thanks, as always!

Using AppGameKit V2 Tier 1
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 6th Aug 2012 16:55
I tested your code and slowly incremented the initial speed by 0.05. When I reached 0.65 as the initial speed, the 'correct' output occurred (swearing in a forum is not nice by the way).

What you are probably running into is a precision thing. The way a floating point value is stored can sometimes make it tricky to compare to exact values, especially with fractions.

When you start with 0.2 and increment by 0.05, a slow bit creep sort of issue is probably what is happening.

If you want to be sure you are working with exact numbers, use integers and then divide by 100.0 for the float value when you need it.

Using integers and incrementing the value will also be faster during execution.

Cheers,
Ancient Lady
AGK Community Tester
Funnell7
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 6th Aug 2012 17:04
Thanks for responding Ancient Lady, thats well worth knowing...

I'll try with an integer as you suggest!

Oh, sorry about the 'swear' word, I get carried away sometimes...

Thanks again!!
DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 6th Aug 2012 18:23

I would say using 2 else commands like that is not a good way to do this. Why the second one?

I must admit I am unsure exactly what you are doing with this, so may be wrong, but will my little edited code work better? Although the last else command is fairly redundant here.

Funnell7
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 6th Aug 2012 18:44 Edited at: 6th Aug 2012 18:47
Hello DVader, the 2nd one is an 'elseif'. The way I have it coded is that if the first 'if' statement hits true (RandomSpeed < 1.0) then there is no need to continue checking the next 'elseif'. With your code, you would be checking both 'if' statements everytime, which is uneccessary. As for the last 'else' this is there in case none of the above 'if/elseif' statements hit true (which in this particular example wouldn't make sense as I took out all the other code, but originally, the first 'elseif' would have done other stuff for which would have meant the last 'else' was relevant. I basically stripped it back just to illustrate the issue)...
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 6th Aug 2012 18:48
Quote: "Using integers and incrementing the value will also be faster during execution."


This may not be the case. If the DBP codebase is being used, then integers are being converted to floats before they are used in calculations, and then back again afterwards.

I may sound wise and learned, but I'm just quoting what the gurus tell me (IanM or Diggsey came up with this one I think).

Marl
12
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 6th Aug 2012 19:41
Quote: "What you are probably running into is a precision thing. The way a floating point value is stored can sometimes make it tricky to compare to exact values, especially with fractions."

I'm in agreement with AL here.

When using FP values you should not really use '=', but instead check within a range (one which is smaller than the amount of change).

Changing the check to

Will get you the right result.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 6th Aug 2012 19:43 Edited at: 6th Aug 2012 19:47
Quote: "integers are being converted to floats before they are used in calculations, and then back again afterwards"


I'd be very surprised if it was doing this. It is not efficient.

If the code looks like this:


Then the RandomSpeed will stay only as an integer. When it is used to create the floating point value, a temporary float is created, the actual variable should not be touched.

Moving the check time calculation the way I did means it does not get done every check, only when the new time is reached.

Comparisons on integer values are much faster, so the checks on the integer RandomSpeed value happen faster. And using the INC command is also faster, no temporary variable is used to do the calculation before assigning it back to RandomSpeed.

But, I could be wrong about integers being actually stored as floats. I don't know how the AppGameKit core Tier 1 compiler works. Certainly, if you are in Tier 2, nothing like that happens at all.

Cheers,
Ancient Lady
AGK Community Tester
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 7th Aug 2012 00:14
The general rule is that any function that can handle floats will convert everything to a float and back again, I found this example in relation to ABS()

http://forum.thegamecreators.com/?m=forum_view&t=189861&b=18&msg=2264826#m2264826

and this post here says all integers are cast to floats before they are stored...

http://forum.thegamecreators.com/?m=forum_view&t=105513&b=1&msg=1183173#m1183173

Maybe it has been updated for AppGameKit, but when you look at some of the files that get generated while compiling, it look like a lot of codebase may be the same.

Login to post a reply

Server time is: 2024-04-27 21:58:27
Your offset time is: 2024-04-27 21:58:27