Home > Enterprise >  Why am I getting: Floating point exception (core dumped)
Why am I getting: Floating point exception (core dumped)

Time:04-04

#include <cs50.h>
#include <stdio.h>
#include <string.h>

// Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products’ digits together.
// Add the sum to the sum of the digits that weren’t multiplied by 2.
// If the total’s last digit is 0 (or, put more formally, if the total modulo 10 is congruent to 0), the number is valid!

// 4003600000000014 - VISA TEST
// 123456789


int length(long n);

int main(void)
{
    long ccnumber = get_long_long("Number: ");

    long x = 10; //Gets the 2nd digit.
    long y = 1; //gets the first.
    long sum1 = 0; //Holds the doubled sum.
    long sum2 = 0; //Holds the undoubled sum.
    int n = 8;

    while (n > 0)
    {
        long cc1 = ccnumber / y;
        cc1 = cc1 % 10;
        long cc2 = ccnumber / x;
        cc2 = cc2 % 10;

        y = y * 100;
        x = x * 100;

        // Times CC2 by 2.
        cc2 = cc2 * 2;

        // Check if double digit then mine by 9, as to add the two digits together.
        if (cc2 / 10 != 0)
        {
            cc2 = cc2 - 9;
        }

        sum1 = sum1   cc2;
        sum2 = sum2   cc1;

    }

}

I have tried checking if it is 0 before dividing by 10. I have tried some other checks with the cc1 and cc2 but cant work it out. I am doing the CS50 course and tring to do the Credit problem set, This is the best I have done so far but it is suddenly throwing this error since trying to do the next step of checking if the total  == 0.

CodePudding user response:

The problem is really the infinite loop. But that, by itself, doesn't result in the core dump. What happens is best illustrated by adding a print statement at the bottom of the loop to display the values of a few variables:

#include <cs50.h>
#include <stdio.h>
#include <string.h>

// Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products’ digits together.
// Add the sum to the sum of the digits that weren’t multiplied by 2.
// If the total’s last digit is 0 (or, put more formally, if the total modulo 10 is congruent to 0), the number is valid!

// 4003600000000014 - VISA TEST
// 123456789


int length(long n);

int main(void)
{
    long ccnumber = get_long_long("Number: ");

    long x = 10; //Gets the 2nd digit.
    long y = 1; //gets the first.
    long sum1 = 0; //Holds the doubled sum.
    long sum2 = 0; //Holds the undoubled sum.
    int n = 8;

    while (n > 0)
    {
        long cc1 = ccnumber / y;
        cc1 = cc1 % 10;
        long cc2 = ccnumber / x;
        cc2 = cc2 % 10;

        y = y * 100;
        x = x * 100;

        // Times CC2 by 2.
        cc2 = cc2 * 2;

        // Check if double digit then mine by 9, as to add the two digits together.
        if (cc2 / 10 != 0)
        {
            cc2 = cc2 - 9;
        }

        sum1 = sum1   cc2;
        sum2 = sum2   cc1;

        printf("%d, %ld, %ld, %ld, %ld\n",n,cc2,cc1,x,y);
    }

}

What you'll see is that x and y are eventually overloaded and reset to 0, which will cause division by 0 with associated fp error and core dump.

$ ./a.out
Number: 5
8, 0, 5, 1000, 100
8, 0, 0, 100000, 10000
8, 0, 0, 10000000, 1000000
8, 0, 0, 1000000000, 100000000
8, 0, 0, 100000000000, 10000000000
8, 0, 0, 10000000000000, 1000000000000
8, 0, 0, 1000000000000000, 100000000000000
8, 0, 0, 100000000000000000, 10000000000000000
8, 0, 0, -8446744073709551616, 1000000000000000000
8, 0, 0, 3875820019684212736, 7766279631452241920
8, 0, 0, 200376420520689664, 1864712049423024128
8, 0, 0, 1590897978359414784, 2003764205206896640
8, 0, 0, -6930898827444486144, -2537764290115403776
8, 0, 0, 7886392056514347008, 4477988020393345024
8, 0, 0, -4570789518076018688, 5076944270305263616
8, 0, 0, 4089650035136921600, -8814407033341083648
8, 0, 0, 3136633892082024448, 4003012203950112768
8, 0, 0, 68739955140067328, -5527149226598858752
8, 0, 0, 6873995514006732800, 687399551400673280
8, 0, 0, 4870020673419870208, -5047021154770878464
8, 0, 0, 7386721425538678784, -6640025486929952768
8, 0, 0, 802379605485813760, 80237960548581376
8, 0, 0, 6450984253743169536, 8023796054858137600
8, 0, 0, -537617205517352960, 9169610316303040512
8, 0, 0, 1578511669393358848, -5376172055173529600
8, 0, 0, -8169529724050079744, -2661627379775963136
8, 0, 0, -5296233161787703296, -7908320945662590976
8, 0, 0, 5332261958806667264, 2377900603251621888
8, 0, 0, -1729382256910270464, -2017612633061982208
8, 0, 0, -6917529027641081856, 1152921504606846976
8, 0, 0, -9223372036854775808, 4611686018427387904
8, 0, 0, 0, 0
Floating point exception (core dumped)
  • Related