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.

DarkBASIC Professional Discussion / float precision and integer casting ???

Author
Message
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 12th Feb 2013 21:52
I'm trying to figure out why the following two statements don't respond the same. (all variables in the udt are floats)

Casting to an integer and then assigning to a float:

zy# = int(oldBall.y + (ball.y - oldBall.y)*tb#)
ty = ceil(zy# / 32)


Casting to an integer upon assignment:

zy = oldBall.y + (ball.y - oldBall.y)*tb#
ty = ceil(zy / 32)


My initial thought was that given a purely integer value, the float should retain that value. Is DB's float precision really this bad that as soon as you assign "3" to a float it becomes "2.99999" or whatever?

"You're not going crazy. You're going sane in a crazy world!" ~Tick
BMacZero
20
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 13th Feb 2013 02:32
If zy is an integer in the second example, zy / 32 will be truncated to an integer and result in the opposite behavior you wanted to get from ceil.

If that isn't it, could you post a runnable code snippet that exhibits it? I couldn't find any weird float precision like you mentioned.

Dar13
18
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 13th Feb 2013 02:36
Why are assigning an integer to a float? That's just asking for weird stuff to happen.

And anyways, the way DBP handles floats or any other datatype(lots and lots of casting to DWORD pointers and such IIRC) could very well mean that it's losing precision that easily. All it would take to turn 3.0 into 2.999.. is a minus epsilon, so it's possible. Not really sure how you could go about testing that.

Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 13th Feb 2013 03:46
Regardless of using ceil, the zy value should technically be the same.

Whether I use INT on the float result or assign the float result to an integer, the decimal is truncated.

zy# = int(3.14)
zy = 3.14

When printed out, I would expect both to appear as 3. And in a simple demo is does.

However, my equation can obviously result is any possible decimal amount. But like I said, it shouldn't matter!


I just posted a new code snippet, which is what this issue involved.



Quote: "Why are assigning an integer to a float?"

Because that value is part of another computation which requires a float. Yea I could just put 32.0 instead for my division and get a decimal answer, but that's not the point. I've already resolved my issue, I'm more curious to know why there's a difference only sometimes.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Randomness 128
19
Years of Service
User Offline
Joined: 13th Feb 2007
Location:
Posted: 15th Feb 2013 17:19 Edited at: 15th Feb 2013 17:23
Quote: "zy# = int(oldBall.y + (ball.y - oldBall.y)*tb#)
ty = ceil(zy# / 32)

zy = oldBall.y + (ball.y - oldBall.y)*tb#
ty = ceil(zy / 32)"


When you divide two integers, the result is an integer that has been rounded down. zy / 32 will round down, making ceil completely useless. zy# / 32 or zy / 32.0 will divide using floats and return a value ceil can actually use.

Are the values of zy# and zy different before that?

320x224

Login to post a reply

Server time is: 2026-07-07 05:15:40
Your offset time is: 2026-07-07 05:15:40