How to Check if Quotient is a whole number

I've been working on several programs to help me with a calculus class (we are allowed to use programmable calculators in the class and I am simply writing a calculator program that is run on a computer) and a major problem I keep running into is the issue of trying to figure out how to write a function that will make sure that the quotient is a whole number. My idea for how this function would work is it would be a bool type function that for the number int n, it would check to make sure that n is a positive whole number (1, 2, 3, etc) and return either 0 or false if it is not, and return either 1 or true if it is. Thanks for any help you can offer.
Last edited on
Do integer division, then verify that a) the quotient is positive and b) the quotient multiplied by the divisor equals the dividend.
n%div == 0
Edit: Missed the sign part (n<0) xor (div<0)
Last edited on
PanGalactic,
Do integer division, then verify that a) the quotient is positive and b) the quotient multiplied by the divisor equals the dividend.

First off, thanks for the help. However I'm new to C++ so I'm not understanding exactly what you mean about the second part; I understand the first part about seeing that the quotient is greater than 0. I see the purpose of this and it makes sense. However, what will the quotient multiplied by the divisor part do? The quotient multiplied by the divisor should always equal the dividend; so what will this do? Will a division problem with int variables give an incorrect result or something? Please explain. Thanks.
I was thinking about what ne555 said and realized that he meant I should check to make sure that the modlue of my number divided by the dividend is equal to 0. I understand now. Thanks a lot!
Using integers:

3 / 2 = 1
2 * 1 != 3

The divisor times quotient is not equal to the original dividend.
A way I made a very simple isInteger() check for doubles was use fmod.
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <cmath>

bool isInt(double number)
{
    if(std::fmod(number, 1.0) == 0) return true;
    // If the number divided by 1.0 yields no decimal portion then it is an integer.

    return false;
}


Here's a nice reference to the working of fmod:
http://www.cplusplus.com/reference/clibrary/cmath/fmod/
Try it:

1
2
3
4
5
6
7
8
9
10
int main()
{
    std::cout << "( 9.0 / 7.0 ) * 3.0 * 7.0"
        << std::endl
        << "What does the oracle say? Is the result an integer?"
        << std::endl
        << std::boolalpha << isInt(( 9.0 / 7.0 ) * 3.0 * 7.0)
        << std::endl;
    return 0;
}


Topic archived. No new replies allowed.