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 / Convert integer to float?

Author
Message
ET3D
10
Years of Service
User Offline
Joined: 2nd Jul 2013
Location:
Posted: 7th Jul 2013 11:07
I'm using the AppGameKit trial with Tier 1. I'm trying to do a calculation involving an integer and a float, the result is wrong, and the values I print for debug are even stranger.

I ended up trying the following code

a1 = 10
a2 as float = a1 + 0.0
Print(a1)
Print(a2)

(I have lots of other code, which is what I'm trying to debug, but it's not relevant.)

The result is:

10
1.000000

??????
haliop
User Banned
Posted: 7th Jul 2013 14:14
I do it a bit diffrent:



you got it right but instead of +0.0 just
a2 = a1.
once it is declared as float it will add the extra .0000000 by itself.

haliop
User Banned
Posted: 7th Jul 2013 14:15
and if you want to something higher then 0.0 like 0.0001
then you do what you did...

a2 = a1 + 0.0001

result will be 10.0001

Marl
12
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 7th Jul 2013 15:09
Quote: "a2 as float = a1 + 0.0"

I'm pretty sure you can only use values (not variables) when you do this form of declaration.

As pointed out by haliop;

is better, but;

is even simpler.
haliop
User Banned
Posted: 7th Jul 2013 15:11
true
i mostly actually almost always declare my vars
as integer
as float
as string

ET3D
10
Years of Service
User Offline
Joined: 2nd Jul 2013
Location:
Posted: 7th Jul 2013 15:19
Thanks, people. It looks like assigning while declaring is what doesn't work. I didn't think it would work differently than declaring and then assigning, but that works perfectly.

The reason I used "+ 0.0" was because just doing "a2 as float = a1" throws up an error about incompatible types.

Looks to me like a bug. AppGameKit should either throw an error if this kind of assignment on declaration doesn't work, or it should work. Still, I can now happily continue and everything works fine.
Marl
12
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 7th Jul 2013 15:24
I vary, but use fixed declarations for UDTs and for function parameters;

So they can be changed easily during optimization.
(saves having to remove a dozen # characters if I decide I don't need to use floats)
haliop
User Banned
Posted: 7th Jul 2013 16:38
swissolo
14
Years of Service
User Offline
Joined: 9th Jan 2010
Location:
Posted: 8th Jul 2013 02:41
@ET3D
It's a long standing bug. It used to be worse, it would simply crash the compiler I never declare anything 'as' primitives in AppGameKit because of this problem. I used to do it for cleanliness, but it just isn't worth it.

swis
Joined: Tue Dec 16th 2008
Interstellar
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 8th Jul 2013 05:59
It has been an issue trying to do both an explicit typing ('x as float') and assign using a formula.

It is safer to do all definitions first.

Also, when doing stuff, if you are assigning to a float variable, at least one element in the stuff on the right side of the equals sign must be a float value. If all things on the right are integers, then the calculation will use pure integer math. So 'x# = 3/4' will produce zero, not 0.75.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
ET3D
10
Years of Service
User Offline
Joined: 2nd Jul 2013
Location:
Posted: 8th Jul 2013 11:54
Quote: "Also, when doing stuff, if you are assigning to a float variable, at least one element in the stuff on the right side of the equals sign must be a float value. If all things on the right are integers, then the calculation will use pure integer math. So 'x# = 3/4' will produce zero, not 0.75."


I'm a long time programmer, so that's something I'd expect. I just didn't expect the Spanish Inquisition, em, I mean for assignment to not work correctly.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 8th Jul 2013 18:12
I think you ran into a compiler fault, more than a language fault.

In order for a complex line to be parsed properly in a compiler, the compiler coder must expect that kind of combination. And it is possible that the line you used pushed the limits a bit.

It can be hard for a programmer to anticipate everything that a user might do (the 'user' in this case is another programmer using the IDE).

I used to pull my hair out when some clients asked for things that were quite simple in their mind and incredibly complex to actually implement so that it was transparent to them. Then they would ask for something they thought was very hard and it was actually either already an existing capability or only required adding a very simple bit to the code. Clients have made my life interesting.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 8th Jul 2013 18:39
it would be better if the automatic type conversion are optional.

means. float<>int compiler error or not
wrong
x# = 3/4
ok
x# = 3.0/4.0
ok
x = 3/4
wrong
x = 3.0/4.0
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 8th Jul 2013 18:53
In Tier 2 you do get warnings about that sort of thing. But it isn't considered an error.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 9th Jul 2013 00:34
These are the sort of problems you get with Basic's loose or non-existent typing, unfortunately.

-- Jim DO IT FASTER, EASIER AND BETTER WITH AppGameKit FOR PASCAL
haliop
User Banned
Posted: 9th Jul 2013 05:43
does this apply also to

x as float

x = 3/4 - Error?

or does this apply only when you declare a float with a #
x# = 3/4 - Error ?

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 9th Jul 2013 09:15
@haliop
if the conversion would be optional, yes.
if you not use decimal places like .0 or .123 the interpreter read integer but the target in your example is float,
this can not assigned direct.
this automatic conversion take also time you can save often.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 9th Jul 2013 16:24
haliop, the result of assigning '3/4' to a float value (regardless of how it is declared) will be 0 because the calculation is done as integer math. It is not considered an error in AppGameKit Tier 1 (or any other Basic). And in Tier 2 (C/C++), it would issue a warning about losing precision. Still not an error and still produces zero.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 9th Jul 2013 16:35
@Ancient Lady
yes, but for a Basic dialect it should be float
because divide two numbers the result are possible with decimal places or more humane.
its really not a error but unusual.
if you input 3 / 4 in a calculator you get also 0,75

visual basic output:
Dim a As Double
a = 3 / 4
Debug.Print a
0,75
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 9th Jul 2013 17:12
Markus it isn't a matter of what you get in the real world on a calculator, it is the assumptions that need to be made in compilers.

If a compiler sees only integer values (or variables) in a math expression, it assumes that an integer result is desired.

If any of the values (or variables) is a float, than it assumes a floating point result is desired (unless the float is in some function that turns it into an integer) and calculates appropriately.

In my opinion (and yours, apparently), the calculation should be done based on the type of the target of the calculation.

But, that is not how AppGameKit Basic works.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 9th Jul 2013 17:28
@Ancient Lady
jo,anyway,for me i just input all numbers with decimal point by default because it was a trap so often.
LVNeptune
13
Years of Service
User Offline
Joined: 17th Sep 2010
Location:
Posted: 10th Jul 2013 20:09
@ET3D

maybe I'm missing something but why not just do this:

a1 = 10
a2# = a1

Does the exact same thing you want.
ET3D
10
Years of Service
User Offline
Joined: 2nd Jul 2013
Location:
Posted: 14th Jul 2013 00:39
I noticed today that apparently anything added to the declaration makes any further assignment fail.

I had a line 'v = LoadImage("image.png")'. Changed it to 'global v = LoadImage("image.png")' and it stopped working. Took me a while to realise that was the problem.

I'm sure I'll adapt to declaring variables separately, but I wish this got fixed.

Login to post a reply

Server time is: 2024-05-04 04:47:45
Your offset time is: 2024-05-04 04:47:45