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

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
JadeLana
Newbie
Posts: 3
Joined: Fri Oct 16, 2009 2:02 pm

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

Post 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?
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post 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.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
JadeLana
Newbie
Posts: 3
Joined: Fri Oct 16, 2009 2:02 pm

Post 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...
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post 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.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
markm
Coder
Posts: 14
Joined: Thu May 01, 2008 11:08 am

Post 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".
JadeLana
Newbie
Posts: 3
Joined: Fri Oct 16, 2009 2:02 pm

Post by JadeLana »

Good idea. I'll try that out.
411161555
Newbie
Posts: 3
Joined: Thu Dec 03, 2009 9:58 am
Contact:

Post by 411161555 »

Good idea.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post 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?
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
User avatar
bongomeno
Veteran
Posts: 266
Joined: Wed Dec 10, 2008 9:08 am
Location: Arizona
Contact:

Post 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..
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post 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:
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Post Reply