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 / Dividing by integers and explicit casting question?

Author
Message
Railgun
18
Years of Service
User Offline
Joined: 2nd Sep 2006
Location: Galveston Bay
Posted: 6th Sep 2006 21:44
So I noticed when you divide two integer variables in DBP, the result is always a truncated integer even if you assign the results to a float

a = 2
b = 3
z# = a / b
Print z# `prints 0

but if you copy the dividend to a float variable you get

x# = a
z# = x# / b
Print z# ` prints 0.666 ...

So, obviously DBP doesn't do any kind of implicit up or down casting.

So, is there any way to do explicit casting?

` this doesn't work but gives the idea
z# = (a AS FLOAT) / b
Cave Man
18
Years of Service
User Offline
Joined: 22nd Aug 2006
Location: North Carolina, US
Posted: 6th Sep 2006 22:39 Edited at: 6th Sep 2006 22:40
Thats strange that I've never ran into that problem. You can use a function and enter the parameters as floats.


It's still annoying that it does that though...


Language: DBP 6.2
cm 2 my hse aftr skool so we cn ply aoe3
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 6th Sep 2006 23:02
Quote: "So, obviously DBP doesn't do any kind of implicit up or down casting"


Yes it does - you are just expecting it in the wrong place. DBPro casts when it needs to as it evaluates the expression.

z# = a / b

... will do an integer division of a and b, and only then cast it to a float to store the value.

z# = a / (b + 0.0)

... will convert b to a float to do the addition and give a float result. Then a will be converted to a float to do the division. Then the result will be stored in z#.

The same result can be gotten using

z# = (a + 0.0) / b

You have to be careful where you force the up-cast though.

z# = (a / b) + 0.0

... will do the integer division, then cast the result to a float to do the addition.

jinzai
18
Years of Service
User Offline
Joined: 19th Aug 2006
Location: USA
Posted: 7th Sep 2006 03:46
I would like to add that that is not casting, but variable promotion/demotion. Casting would be COOL.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 7th Sep 2006 23:05
Hmm ... promotion/demotion are implicit conversions of one type to another to allow mathematical operations to take place. Casting is the explicit conversion of one type to another. They aren't quite the same, but they're close enough to make no difference to most people.

jinzai
18
Years of Service
User Offline
Joined: 19th Aug 2006
Location: USA
Posted: 9th Sep 2006 03:09 Edited at: 9th Sep 2006 05:23
I disagree. Casting does not change anything in C/C++, it only implies to perceive a variable as another type for that statement's scope. When a compiler promotes or demotes a variable, it is not casting...it is making a new variable of the required type. Precision is lost when variables are demoted, even with no casting by the user. For example, I can cast a variable back and forth with no loss of precision.

Not that it applies at all to DBPro (I wish it did!), but you can cast an address (pointer), but you can't promote, or demote and address.

Implicit is when the compiler does it...explicit is when you do it. I understand that it make no difference at all to DBPro users, but the difference is more obvious and useful in C/C++.

Login to post a reply

Server time is: 2024-11-26 11:36:10
Your offset time is: 2024-11-26 11:36:10