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.
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!
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) returntrue;
// If the number divided by 1.0 yields no decimal portion then it is an integer.
returnfalse;
}