1

I am creating a program that verifies that an integer is a valid number for a chosen base. The program conditionally throws a Floating point exception even though I am fairly certain that it is impossible for a division by 0, and I have double checked to make sure my variables are initialized.

const int MAX_DIGIT = 5;
int num, base;
int digit = 1;
bool verified_num = true;

cin>>num;
cin>>base;

while (verified_num && digit < pow(10, MAX_DIGIT)) {

    if (num % (digit * 10) / digit >= base)
        verified_num = false;
    digit *= 10;
}

if (verified_num)
    cout<<"\n"<<num<<" is a valid base "<<base<<" number!\n";
else
    cout<<"\n"<<num<<" is not a valid base "<<base<<" number!\n";

This code works properly when the number is invalid. For example, when num = 54321 and base = 3 the output is

"54321 is not a valid base 3 number!"

Any time the number is valid, however, an error occurs. For example, when num = 12345 and base = 6

Floating point exception (core dumped)

I cannot figure out why this is happening, since in the first example,

if (num % (digit * 10) / digit >= base)

evaluates to false several times and does not cause an error

I am running this code on GCC 7.3.0, and the code seems to work on a couple online compilers that I have tried

EDIT: It appears I made an error in copying my code that concealed the error. In the original code, the variable digit was accidentally stored in a short integer and was overflowing unless the loop exited early. Thank you to everyone that gave their insight

Minihoot
  • 11
  • 4
  • 4
    Note that "floating point exception" is **not** "throwing an exception". A "floating point exception" has nothing in common with C++ exceptions. It's FP terminology for an error, and simply aborts execution. – Pete Becker Sep 21 '18 at 20:24
  • @PeteBecker I was not aware of that distinction, thank you – Minihoot Sep 21 '18 at 20:29
  • @Minihoot Why are you using the `pow` function for a (seemingly homework) assignment that is based on integers? `pow` is a floating point function -- you shouldn't be using it here. Just use a simple array of integers `1 10, 100, 1000, 10000, 100000` and index into that array. – PaulMcKenzie Sep 21 '18 at 20:31
  • @PaulMcKenzie I was advised to use this function by my instructor. Is there a reason that integers should not be used with the pow function? We are not allowed to use arrays for this assignment – Minihoot Sep 21 '18 at 20:40
  • 2
    That is garbage advice. So your instructor wants you to keep 1) calling a function that returns a floating point value, thus incurring possible rounding issues and 2) computing the same value over and over and over again instead of just using the constant `100000`? Just for kicks, replace that `pow` call with 100000. Do you then see the error? Oh, and to your question about `pow`, [read this](https://stackoverflow.com/questions/25678481/why-does-pown-2-return-24-when-n-5-with-my-compiler-and-os) – PaulMcKenzie Sep 21 '18 at 20:43
  • Why would you need this check `digit < pow(10, MAX_DIGIT)` at all? – n. m. could be an AI Sep 21 '18 at 21:31
  • Please post a [mcve]. – n. m. could be an AI Sep 21 '18 at 21:31
  • @n.m. the program has a maximum number of digits that the number can have (represented by a constant). That check is there to prevent the code from continuing to check digits until it overflows. The reason I didn't use a for loop is so that the loop can exit early if a failing digit is found. – Minihoot Sep 21 '18 at 21:52
  • *the program has a maximum number of digits* Thanks I understood this much. I'm programming for a living for the last 30 years. The question is why do you need to have any maximum number of digits. You have a number with a certain number of digits. You need to check all these digits one by one. It's only because you are doing it in an incredibly roundabout way with an ever-increasing helper number you have to worry about overflow. The normal regular way to do that is to chop digits off your number. This operation **decreases** the number. Nothing is ever increased, no overflow is possible. – n. m. could be an AI Sep 21 '18 at 22:02
  • The only thing "floating point" is the `pow` function, and the constant `eps`, so the logical thing if you are not going to use your debugger is to remove those things that can cause the issue. In any event, none of those are required to do this assignment. `verified = true;while (num > 0 && verified) {int digit = num%10; verified = (digit < base); num/=10;}` – PaulMcKenzie Sep 21 '18 at 22:41
  • Run it under a debugger so you can see what line is raising the signal. – Davis Herring Sep 22 '18 at 07:47

0 Answers0