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 / Why can't i do this? (Integers and floats)

Author
Message
Xlaydos
20
Years of Service
User Offline
Joined: 26th Mar 2004
Location:
Posted: 9th Apr 2007 16:11
Why can't i do this?!?



It gives 1

Instead i have to do


which correctly gives 1.5
Crit
18
Years of Service
User Offline
Joined: 24th May 2006
Location:
Posted: 9th Apr 2007 16:33
If you want to force it to to float division, do it like this:

print 6.0/4.0
Xlaydos
20
Years of Service
User Offline
Joined: 26th Mar 2004
Location:
Posted: 9th Apr 2007 18:25 Edited at: 9th Apr 2007 18:26
Okay, is that the only way? Or am i missing a easy command or something

Because that would require me to setup my variables as 6.0 when i'd only be wanting float division for 1/100th of my code or something
spooky
22
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 10th Apr 2007 17:44 Edited at: 10th Apr 2007 17:45
If the variables are integers and you want a float answer, then simply introduce any number with a . in it.

a=6
b=4
print (a+0.0)/b
wait key

or

a=6
b=4
print a*1.0/b
wait key

Boo!
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 10th Apr 2007 21:11 Edited at: 10th Apr 2007 21:20
Quote: "Because that would require me to setup my variables as 6.0 when i'd only be wanting float division for 1/100th of my code or something"


That's how programming works!

You're lucky you are using DB - which makes it easier for you. Some languages make you declare all variables prior to using them and won't run if you declare a variable as one type but try to store something else in it.

By rights, any float (#) variable in DB should contain a decimal point value Eg: A#=12.0 or A#=12.33 though DB is lenient and allows you to use A#=12 instead of A#=12.0 without erroring.

6/4 is an integer calculation and forces an integer result whereas 6.0/4.0 is a float calculation. Programming languages have both types of number because float calculations are a lot slower than integer and the majority of calculations tend to be integer anyway.

TDK_Man

Xlaydos
20
Years of Service
User Offline
Joined: 26th Mar 2004
Location:
Posted: 11th Apr 2007 00:11
Okay thanks a lot for that explanation

Thanks spooky i'll use that

Login to post a reply

Server time is: 2024-09-25 21:24:08
Your offset time is: 2024-09-25 21:24:08