为什么我得到:浮点异常(核心转储)

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

#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;

    }

}

我已经尝试在除以 10 之前检查它是否为 0。我已经尝试使用 cc1 和 cc2 进行其他检查,但无法解决。我正在学习 CS50 课程并尝试做 Credit 问题集,这是我迄今为止所做的最好的但是它突然抛出这个错误因为尝试执行下一步检查总数 %10 == 0.

问题确实是无限循环。但这本身并不会导致核心转储。通过在循环底部添加打印语句来显示几个变量的值,可以很好地说明发生了什么:

#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);
    }

}

您将看到 x 和 y 最终过载并重置为 0,这将导致除以 0 以及相关的 fp 错误和核心转储。

$ ./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)