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 / When to use floating point variables...

Author
Message
Vidiot
18
Years of Service
User Offline
Joined: 15th May 2006
Location: TN
Posted: 18th Nov 2007 16:29
Even though this is baffling me, I'm thinking its a newb question so I posted it here. In the following example:

I understand why "a" should be a float. Its telling the compiler to prepare to do some floating point math. What makes no sence to me is why "x" needs to be float for this to run correctly. Its illogical since in this scenerio, x will only be an integer. Why can't a componant of a floating point math problem be an integer? It happens in real life every day.

The answer to Life, the Universe, and Everything? "Tea for Two". Deep Thought was Dyslexic.
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 18th Nov 2007 16:36
Well, to get a resulting float number, you have to have atleast one float in an operation. x/30 is two integers, so it will give you a resulting integer. You could just do x/30.0

"It's like floating a boat on a liquid that I don't know, but I'm quite happy to drink it if I'm thirsty enough" - Me being a good programmer but sucking at computers
Vidiot
18
Years of Service
User Offline
Joined: 15th May 2006
Location: TN
Posted: 18th Nov 2007 20:20 Edited at: 18th Nov 2007 20:21
Cool. I never noticed the two constants were also integers. I assumed that since I was using division, and the result would obviously be a fraction, that the compiler could see that.

This problem really came up because I was trying to us the index variable of a for/next loop in some floating point math and it kept failing. The work-around was needlessly overcomplicated. And all I had to do was add a decimal to one of the constants!

Thanks!

The answer to Life, the Universe, and Everything? "Tea for Two". Deep Thought was Dyslexic.

Login to post a reply

Server time is: 2024-09-27 08:28:59
Your offset time is: 2024-09-27 08:28:59