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.

Newcomers DBPro Corner / variable type conversion

Author
Message
jerryd
11
Years of Service
User Offline
Joined: 20th Feb 2013
Location:
Posted: 14th Mar 2013 04:38
DarkBasic forum,

I'm trying to use x and y locations of a sprite to calculate the
atan of an angle. The sprite command says the x and y locations
must be integers but if I use integers to do math that yields
a real(float) number it doesn't work.

I've tried using floats for sprite x,y but that doesn't work.

ex:

xloc as integer = 7
yloc as integer = 2
n as float
x as float
y as float


DIVISION
n = yloc/xloc `yeilds a 0
n = 2.0/7.0 `yields 0.285714285

CONVERSION
x = xloc `yields 0
y = yloc `yields 0

I've read that DBP has problems with type conversion so how do
I do this calculation?

jerryd
pcRaider
16
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 14th Mar 2013 05:46
A version of your DBP?

This is a result of my test.
jerryd
11
Years of Service
User Offline
Joined: 20th Feb 2013
Location:
Posted: 14th Mar 2013 05:58
pcRaider,

Sorry I should have included this in my post.
The compiler is version 1.0761.

jerryd
pcRaider
16
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 14th Mar 2013 06:12
Please be careful.
integer = integer / integer
float = float / float
Therefore
float = integer*1.0 / integer*1.0


and sprite did not become an error of float.
jerryd
11
Years of Service
User Offline
Joined: 20th Feb 2013
Location:
Posted: 14th Mar 2013 06:26
pcRaider,
I ran your example and it worked. Looks like the key is
the *1.0.
I'll try using floats in a sprite command again.

Thanks

jerryd
Chris Tate
DBPro Master
15
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 14th Mar 2013 10:51
It will cast the result to a float when at least one floating point operand is used.

pcRaider
16
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 14th Mar 2013 11:54
Quote: "It will cast the result to a float when at least one floating point operand is used."


Why will these be different?
Chris Tate
DBPro Master
15
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 14th Mar 2013 19:22 Edited at: 14th Mar 2013 19:23
A decimal place or floating point variable will cast the result to a float. When you added 1.0 or 2.0 you specified that you want a floating point result, an outcome with a decimal place. When all the numbers where integers, you lost the remainder of the division which gives you zero.

Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 15th Mar 2013 03:44 Edited at: 15th Mar 2013 03:45
except in the case of

nf = yi / xi * 1.0

yielding 0, though this can be attributed to order of operations in which it appears to be dividing before multiplication so that

yi / xi = 0
nf = 0 * 1.0 = 0

as it does not continue to use this order of ops in the case of

nf = yi * 1.0 / xi

it seems to simply be operating in order of left to right when it comes to multiplication and division. explicitly defining the order with ( ) should fix it.

nf = yi / (xi * 1.0)

pcRaider
16
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 15th Mar 2013 04:46
OK, thank you.
Chris Tate
DBPro Master
15
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 15th Mar 2013 20:05 Edited at: 15th Mar 2013 20:50
Yes, I should have pointed that out.

The division sign is calculated before the multiplication sign. From left to right, zero enters the equation, anything multiplied or divided by zero must result with a zero; additionally, dividing by zero is technically incorrect in any-case, and DBP is fussy about dividing or dividing by zero.

As shown with this snippet, the division is still calculated before the multiplication sign even from right to left.



1.0 * 2 = 2.0 | 2.0 / 7 = 0.28574 but the integer division took place first which equates to zero, zero is then multiplied by 1.0.

Login to post a reply

Server time is: 2024-04-18 22:24:13
Your offset time is: 2024-04-18 22:24:13