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 / Division question

Author
Message
pictionaryjr
17
Years of Service
User Offline
Joined: 12th Mar 2009
Location:
Posted: 2nd Nov 2012 09:36
i'm trying to do a simple division of 200/480 and dbp continually makes this equal to 0. it doesn't even work for 1/2
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 2nd Nov 2012 11:13
Integers.

They are evil.

Try:

200.0/480.0

TheComet

- The codebase
Sergey K
22
Years of Service
User Offline
Joined: 4th Jan 2004
Location:
Posted: 2nd Nov 2012 11:46
dbp automaticly works with Integers. if you want to work with floats (wich 0.0000 = float) you have to define your number as float 1st. just like TheComet said earlier.

MyVar = 200/480
print MyVar

MyVar = 200.0/480.0
print MyVar

both of them will give you same result
why? cuz MyVar is automaticly defined by DBP as Integer.
to change that, you have to define it as float 1st..

MyVar as Float on top of your code.
or
use # in end of your var to help dbp to understand what you working with..

MyVar# = 200.0/480.0
Print MyVar#

Advanced Updater for your games!
pictionaryjr
17
Years of Service
User Offline
Joined: 12th Mar 2009
Location:
Posted: 3rd Nov 2012 08:00 Edited at: 3rd Nov 2012 08:01
thanks guys. i should have waited for a response lol. i just went through the code. did the math for all the division on a calculator and then put it into code lol looks like i'll have to go back through

i kept tryin 200/480 and then was like 1/2 and it still didnt work so i even made it variable#=200/480 that was when i got stumped
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 3rd Nov 2012 12:24
Just so you know, you don't need to make both numbers floats, only one.

200/480.0 would work just fine. And in the case of using integer variables:

float = X / (Y+0.0)

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 3rd Nov 2012 15:18
Not quite true - you need to bear in mind the order of calculations when the usual rules of arithmetic are followed. Have a look at the results of the third expression for example:



The safe answer if you're unsure is to make sure everything is a float if you want float calculations.
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 3rd Nov 2012 16:03
Plus, if you're doing something like this:

10.0/4

The "4" is actually casted to be a "4.0" before the calculation is made (if you look at the disassembly). It's more efficient to use only floats.

TheComet

- The codebase
Benjamin
23
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 3rd Nov 2012 16:26
TheComet, how is that code snippet relevant to the thread?

You're signature has been erased by a mod
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 3rd Nov 2012 16:53
It's part of my signature.

TheComet

- The codebase
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 3rd Nov 2012 21:35
lol

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 3rd Nov 2012 21:47
Seconded.
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 4th Nov 2012 08:51
Quote: "Not quite true "

You're not referring to my post are ya?

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 4th Nov 2012 09:13 Edited at: 4th Nov 2012 09:13
If you want to do float calculations with integer variables then you can simply add on the decimal point, literally!

num = 5
print 9 / (num+.)

There's a chance just having the point might not work in DBP but the principle is the same (num+0.0) will return a float value that can then be used in the division to return a float.

Shh... you're pretty.
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 4th Nov 2012 13:12
Quote: "You're not referring to my post are ya?"


Yes, I am.
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 4th Nov 2012 13:50
Interesting comments

Quote: "print 1/2, " ", 1/2.0, " ", (1/2)/1.0"


The third expression contains a parenthetical division that results with zero divided by 1.0, this is the same even without the parentheses.

All of the following expressions contain at least one floating point return and outputs a positive floating point result:

a = 1
b = 2
c = 3
d = 4
e# = 0.5
f# = 2.5
g# = 4.0
Print 1.0/2/1
Print a*e#
Print e#/c
Print b/f#
Print c/g#
Print e#*c
Print a/e#*f#/g#
Print e#/c*e#
Print b/f#/c
Print c/g#/d
Print e#*c/b
Print Screen Width()*e#
Print Int(Sin(35)*10)/4.0
Print 10/15.0
Print 15/10.0
Wait Key


And the following contains integer returns and outputs an integer result:

a = 1
b = 2
c = 3
d = 4
e# = 0.5
f# = 2.5
g# = 4.0
Print 4/2
Print 10*2
Print Int(e#)*1
Print Int(e#)*Screen Width()
Print 4/Ceil(e#)*a
Print 2/Floor(f#)*b
Print 1/Ceil(0.5)
Print 2/Int(2.2)
Wait Key


On the topic of division, you could use a similar function to prevent the division by zero error:



Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 4th Nov 2012 16:18
Quote: "The third expression contains a parenthetical division that results with zero divided by 1.0, this is the same even without the parentheses."


Indeed it does - I was merely making the order of arithmetic operations obvious. But your version perhaps makes my point better - i.e. that you really need to know what you're doing when you rely on integer arithmetic in the middle of a float calculation.
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 4th Nov 2012 23:19
Quote: "Yes, I am."

I'm unclear what you mean. Your examples were not the same as mine. Mine works perfectly fine. As far as order of operations go, that's why I used parenthesis for the addition with the zero float.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 4th Nov 2012 23:30
Quote: "Just so you know, you don't need to make both numbers floats, only one."


That sentence led me to believe that you were suggesting that it is sufficient to have "only one" float in an expression. If your post was really meant to refer to expressions involving only two values then your remark was rather restricted in scope and newcomers might well misunderstand your meaning. Hence my phrase "not quite true".
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 4th Nov 2012 23:59
And again, having only one float in the expression is slower than making them all floats, because the integers are casted to floats before they are processed (according to the disassembly).

TheComet

- The codebase
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 5th Nov 2012 20:08
Ok, I got what you're saying now. By needing only one, I was referring to a single operation.

"You're not going crazy. You're going sane in a crazy world!" ~Tick

Login to post a reply

Server time is: 2026-07-10 16:22:55
Your offset time is: 2026-07-10 16:22:55