Page 1 of 1

How to tell if a variable is a whole number or decimal

Posted: Fri Oct 16, 2009 2:05 pm
by JadeLana
Maybe I'm not using the right search words, but I can't find it! Is it possible?

I want to know if say:

LET X = 5
LET Y = 3
LET Z = X/Y

So Z would be 1.66666... would there be a way to know that for any values of X and Y?

Posted: Fri Oct 16, 2009 3:39 pm
by burger2227
That is the Prime reason programs use variable name assignments and can use type suffixes. You can use almost ANY X and Y values to calculate something. In the division example you gave however Y can NEVER become 0! That would create a division by 0 error. Programs cannot do infinity.

One thing you probably may not know is that the number type is SINGLE(floating decimal point) by default when a variable uses no type suffix or is not defined in a DIM AS statement. For example, if you used Z% = X / Y then the return would be an integer instead of a decimal point fraction.

DIM X AS SINGLE, Y AS SINGLE, Z AS INTEGER

Also LET is NEVER necessary in Qbasic! Does your teacher require it? If so, just shoot him in the foot LOL.

Posted: Fri Oct 16, 2009 4:34 pm
by JadeLana
No, I'm not even in a programming class. I learned BASIC a long, long time ago and redundant statements like LET are second nature. Also I assumed that no one would think I would want to divide by zero. XD

If I declare Z as an integer, it will round, right? What I want is like a way for the program to tell me IF the number has a decimal. I don't really care what it is. Like something so I could say

IF (number is has decimals) THEN...
IF (number is a whole number) THEN...

Posted: Fri Oct 16, 2009 7:03 pm
by burger2227
Yes, Integers round up or down according to the value. Z% rounds like the CINT function in my example. Anything over .5 rounds up to 2. A value of .5 or under rounds down to 1.

As long as you are dividing Integers, use MOD to see if there is an integer remainder. MOD is called remainder division (Y cannot be 0). You may recall remainders from grade school before they introduced fractions and decimal point values:

IF X MOD Y <> 0 THEN PRINT "NOT a whole number" ELSE PRINT "A whole number"

Whole numbers are INTEGER or LONG Integer values in QB.

Posted: Fri Nov 06, 2009 2:40 pm
by markm
[quote]What I want is like a way for the program to tell me IF the number has a decimal. [/quote]

DIM I AS INTEGER, Z AS SINGLE
....
Z = X/Y
I = Z
IF ABS(Z-I) < 0.0001 THEN
'INTEGER
ELSE
'NOT INTEGER
END IF

That is, force it to round-off to an integer (there are functions that could be used instead), then see if it changed. Note that you cannot expect a floating point calculation such as 6.0/2.0 to come out exactly to an integer, so you have to decide how close will count as "integer".

Posted: Fri Nov 06, 2009 7:02 pm
by JadeLana
Good idea. I'll try that out.

Posted: Thu Dec 03, 2009 10:14 am
by 411161555
Good idea.

Posted: Thu Dec 03, 2009 5:34 pm
by burger2227
Oh my, a newbie here! They always seem to post in OLD threads and may say useless things.

Is there an ECHO in here?

Posted: Thu Dec 03, 2009 11:31 pm
by bongomeno
burger2227 wrote:Oh my, a newbie here! They always seem to post in OLD threads and may say useless things.

Is there an ECHO in here?
at least the forum is not dead..

Posted: Fri Dec 04, 2009 2:35 am
by burger2227
JUST what we need here! A SPAMMER.

Click the little boxes under Good Idea or check the website!

You ain't so smart Bongo man... :wink: