How to tell if a variable is a whole number or decimal
How to tell if a variable is a whole number or decimal
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?
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?
- burger2227
- Veteran
- Posts: 2466
- Joined: Mon Aug 21, 2006 12:40 am
- Location: Pittsburgh, PA
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.
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
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
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...
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...
- burger2227
- Veteran
- Posts: 2466
- Joined: Mon Aug 21, 2006 12:40 am
- Location: Pittsburgh, PA
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.
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
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
[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".
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".
- burger2227
- Veteran
- Posts: 2466
- Joined: Mon Aug 21, 2006 12:40 am
- Location: Pittsburgh, PA
Oh my, a newbie here! They always seem to post in OLD threads and may say useless things.
Is there an ECHO in here?
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
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
- burger2227
- Veteran
- Posts: 2466
- Joined: Mon Aug 21, 2006 12:40 am
- Location: Pittsburgh, PA
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...
Click the little boxes under Good Idea or check the website!
You ain't so smart Bongo man...
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
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