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 / strange math in DBP

Author
Message
GatorHex
19
Years of Service
User Offline
Joined: 5th Apr 2005
Location: Gunchester, UK
Posted: 3rd May 2007 01:42 Edited at: 3rd May 2007 01:55
I've been awake over 24hrs so it might be sleep depravation, but isn't something screwy happening with this math or do I just need to to bed -.-

DBP v 1.066

scaleCuts# = 10 / 100
print str$(scaleCuts#)
scaleCuts# = 100 / 10
print str$(scaleCuts#)
wait key

output should be
0.1
10

but im getting
0
10

http://www.KumKie.com http://bulldog.servegame.com
vorconan
17
Years of Service
User Offline
Joined: 4th Nov 2006
Location: Wales
Posted: 3rd May 2007 01:46
are you serious thats this is happening? 24hrs sleep deprevation can mess with your head a bit.
GatorHex
19
Years of Service
User Offline
Joined: 5th Apr 2005
Location: Gunchester, UK
Posted: 3rd May 2007 01:48 Edited at: 3rd May 2007 01:51
yeah i'm starting to see wierd flashes of movement out of the corner of my eyes
and yeah I've run it a few times and I seem to be getting 0 instead of 0.1

Can anyone replicate it or is it just me? What version of DBP are you on if it works for you?

http://www.KumKie.com http://bulldog.servegame.com
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 3rd May 2007 02:09
Quote: "I've run it a few times and I seem to be getting 0 instead of 0.1"


Which you should, of course.

10 and 100 are both interpreted as integers so you get integer division. To get a float result you need at least one of the operands to be floats, i.e. try 10.0/100 or 10/100.0 or 10.0/100.0 .

Time to go to bed probably (for me as well).
Dabbler
17
Years of Service
User Offline
Joined: 3rd Mar 2007
Location: Minnesota
Posted: 3rd May 2007 05:20 Edited at: 3rd May 2007 05:22
Try this:


I have not tried it, but I think it should work. I also think you will see your mistake.

I think it had something to do with staying up for 24 hours.

Edit: I see Green Gandalf has beaten me to it.


Whatever...
GatorHex
19
Years of Service
User Offline
Joined: 5th Apr 2005
Location: Gunchester, UK
Posted: 3rd May 2007 12:17
Thanks, just thought it would type cast to # as both the value come from things like screen width()

I'll try pre loading it into a # before doing the calculation

http://www.KumKie.com http://bulldog.servegame.com
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 3rd May 2007 14:54
Casting is done as it is needed.

Integer 10 divided by integer 100 is integer zero, which is then cast to floating-point zero so that it can be stored.

If you have two integer values that you need to apply a calculation to, just add 0.0 to one of them.

David Gervais
Retired Moderator
18
Years of Service
User Offline
Joined: 28th Sep 2005
Location: Montreal, Canada
Posted: 3rd May 2007 15:26
just a FYI.. when I run this code..



the results I get are,

0.10000000149
10

what's with the 7 zreo's followed by 149? is it a math tax?

Cheers!
Humanoid
20
Years of Service
User Offline
Joined: 20th Sep 2003
Location: Finland
Posted: 3rd May 2007 16:44 Edited at: 3rd May 2007 16:45
try this


Suomi Finland PERKELE!
<AMD athlon 64 3000+> <Asus A8N-E nForce4 ultra> <GF6600LE PCI-E> <1GB ram>
Cash Curtis II
19
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 3rd May 2007 17:53
Quote: "what's with the 7 zreo's followed by 149? is it a math tax?"

Floating point numbers must be able to be represented by base 2. .1 cannot be, so the closest base 2 approximation is used. No matter what language you use it is the same. In VS, you have additional data types that double or quadruple the size of the data type and display only the significant digits which give the illusion of perfect accuracy. The thing is, big data types are very slow, and not necessary in game development.


Come see the WIP!
Lucy
17
Years of Service
User Offline
Joined: 19th Apr 2007
Location: Roanoke, VA USA
Posted: 3rd May 2007 18:15 Edited at: 3rd May 2007 18:19
I've had this problem a lot.

What's happening is that DBPro's default type is "integer" and for some strange reason, even if you put math directly after "as float" such as "myvar as float = x / n" it screws up and casts it to an integer before feeding it into myvar.

The way I fix it is by making a float to feed the math into but don't put the math into the float at the same place you're declaring it to be a float but rather after, such as



Fixes it for me every time.

Quote: "what's with the 7 zreo's followed by 149? is it a math tax?"

It's inaccuracy in floating points... You need a bignum to get perfect precision, but those are slow as hell on DBPro. I've tried already. What I'd recommend is trimming it back so many decimal places by casting it to a string and then back to a value if the slight inaccuracy is too disconcerting. Of course you lose accuracy if something is supposed to extend that far.

Nothing I say is intended to be rude. My autism means that I do not know what is rude and what isn't rude. I apologize if I seem rude. It is not my intention.
Cash Curtis II
19
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 3rd May 2007 18:34
In DBP, mathematical expressions are evaluated from left to right, as would be expected.

Quote: "print (10/100.0)"


Will return .1

Quote: "print (10.0/100)"


Will return 0.

Expressions in parentheses are evaluated first, then applied in the same left to right manner as regular expressions.

Bottom line - mixing different data types is extremely slow. There is a misconception that using integers for everything is faster, because every 3D related function in DBP returns floats, and mixing the two is very, very slow.


Come see the WIP!
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 3rd May 2007 18:44
Quote: "Bottom line - mixing different data types is extremely slow"

How so? I can't see it making a significant impact on performance.

Tempest (DBP/DBCe)
Multisync V1 (DBP/DBCe)
Cash Curtis II
19
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 3rd May 2007 19:07 Edited at: 3rd May 2007 19:09
Using mixed datatype in an expression is significantly slow than using the same datatypes. For a single expression, the difference might seem insignificant, but during the course of a program loop there can be thousands of calculations like this. It certainly can impact performance.

A good example is the native ABS function. This function is pretty slow as it is, but much, much slower when you use an integer on it because it natively supports floats and has to type cast a couple times to give you an integer result. A replacement absInt() function written in DBP is way faster than the included command.

I've tested all of this extensively. I'm a stickler for efficiency. Poorly written code can cause a project to fail.


Come see the WIP!
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 3rd May 2007 19:19 Edited at: 3rd May 2007 19:20
Quote: "Using mixed datatype in an expression is significantly slow than using the same datatypes."

Comparatively of course.

Quote: "It certainly can impact performance."

I'm still not seeing it. There are tonnes of operations that take longer than type casting, the operation of converting an int to a float is like an ant among these other operations. I guess it depends just on the routine, if it's a lengthy routine that simply does calculations then it may have some impact on performance.

Tempest (DBP/DBCe)
Multisync V1 (DBP/DBCe)
Cash Curtis II
19
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 3rd May 2007 19:25
Of course lots of operations take longer than type casting, but don't underestimate how slow it really is. If in every expression that you evaluate, you add a type cast to it, in many cases you've doubled the length of time it takes to calculate.

For instance, let's say you do this...

Quote: "x=object position x(obj)
y=object position x(obj)
z=object position x(obj)"


33 times per loop. That's 999 additional type casts that you just don't need. The object position commands are fast, so you are adding comparatively significant overhead to these commands. Let's make it worse now...

Quote: "Position object obj,x,y,z"


Let's do that 33 times per loop. X, Y, and Z all have to be recast from integer to float to work with this command. That's another 999 unecessary conversions.

In a standard game, it is easy to have thousands, if not more, occurrences like this. It is definitely slower.


Come see the WIP!
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 3rd May 2007 19:32
Actually, after just doing some experimenting, it seems DBP is rather slow at type casting. You win this round.

On the subject of optimization, I think it would be really great for DBP to have macros for certain small functions, for instance abs, rgb, rgbr, etc, where the overhead of calling the function could be removed.

Tempest (DBP/DBCe)
Multisync V1 (DBP/DBCe)
Cash Curtis II
19
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 3rd May 2007 19:39 Edited at: 3rd May 2007 19:40
I'm doing some optimizations like that, included in my animation plugin. I've included an absFloat and absInt, both of which are significantly faster than the native DBP commands. IanM already made an overloaded integer version, and my integer version is the same speed as his. I thought it important though to include a float version. I'm using them in Geisha House, and I've had some small speed increases.

If you have specific commands that act poorly with type casts and would like to see replacement commands, let me know and I'll include them. The best way to do it is to create two versions of the command, an integer and float version.

Are the RGB commands broken? The integer to Dword conversion is unavoidable, and I can't really think of a situation where another datatype would be used with that command.

Let me know what you think and I'll definitely add them.

It's not even really DBPs fault that the conversions are so slow, it's just how data types work. Much like the much asked "Why are floats imprecise!! DBP is broken!!!"


Come see the WIP!
GatorHex
19
Years of Service
User Offline
Joined: 5th Apr 2005
Location: Gunchester, UK
Posted: 3rd May 2007 19:53
OK interesting info, I got it scaling to all sreen sizes now

now does anyone know how I can make my underwater flilter look any nicer, maybe a wobble effect or something, hows it done?



http://www.KumKie.com http://bulldog.servegame.com
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 3rd May 2007 19:56 Edited at: 3rd May 2007 20:02
Quote: "Are the RGB commands broken? The integer to Dword conversion is unavoidable, and I can't really think of a situation where another datatype would be used with that command."

There are more optimizations to do other than avoiding type casting. Take this for example, rgb versus calculating the value manually:



On here it's faster to do it manually, although if I add in the alpha component it's actually slower. The actual idea is that putting the code in directly should be faster than calling a function to do the same thing. A macro would replace rgb with the code itself. The same goes for abs:



I wouldn't personally consider using a function for something that can be so easily done manually for more speed.

Quote: "It's not even really DBPs fault that the conversions are so slow, it's just how data types work."

Well I haven't really done a proper comparison, but it's pretty fast in C++ if that means anything.

Tempest (DBP/DBCe)
Multisync V1 (DBP/DBCe)
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 3rd May 2007 19:58
Uh, CC, 10.0/100 and 10/100.0 will give the same correct result.

In the first case DBPro will compile the equivalent of:
Load 10.0, load 100, cast 100 to 100.0, divide.

In the second case DBPro will compile the equivalent of:
Load 10, cast 10 to 10.0, load 100.0, divide.

Basically, you evaluate each operator in turn:
If both arguments are integers, carry out the integer operation.
If one is a float, cast the other to float, then carry out the operation.

So, (10/2)+1.0 will carry out the bit in brackets as an integer operation, then cast the result to a float to add the 1.0

Cash Curtis II
19
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 3rd May 2007 20:00
Oh damnit, that's right Well, I'm still right about not mixing data types. I appreciate the correction.


Come see the WIP!
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 4th May 2007 02:38
And 100% integer calculations are a lot faster than calculations with floats, so you should use integers unless floats are absolutely necessary.

As CC states, most of DB's 3D commands return floats and it might take longer to convert from float to ints for your integer calculation than it would to have done the float calculation in the first place!

But, as a rule of thumb, if you can make variable that are to be used in calculations integers, then you should.

I see lots of programs floating around where all the variable names have # on the end - even though they don't need to be...

TDK_Man

Cash Curtis II
19
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 4th May 2007 09:07
Quote: "But, as a rule of thumb, if you can make variable that are to be used in calculations integers, then you should."

Absolutely. I use them everywhere I can. Realizing that DBP deals with mainly floats tends to limit that, but it is very important.


Come see the WIP!

Login to post a reply

Server time is: 2024-05-10 21:06:37
Your offset time is: 2024-05-10 21:06:37