rand()引起的浮点异常在c ++中

Ale*_*lex 3 c++ floating-point-exceptions

我有一个似乎无法解决的问题.我随机生成数字以确定我的数字是否是相对论素数.

这是给我一个浮点异常的函数:

bool modularExponentiationTest(unsigned long long exponent, unsigned long long modulus)
{
    short index = 0;
    unsigned long long base;
    unsigned long long result;

    do
    {
            result = 1;
            base = rand() % exponent; // <--CAUSED BY THIS

            while (exponent > 0) 
            {
                if (exponent & 1)       
                        result = (result * base) % modulus;
                exponent >>= 1;
                base = (base * base) % modulus;
            }

            if (result != 1)
                return false;
    }while(++index < 10);

    return true;
}
Run Code Online (Sandbox Code Playgroud)

通过执行以下操作,我在不同的函数中实现随机种子:

 srand(time(NULL));
Run Code Online (Sandbox Code Playgroud)

非常感谢您的帮助!

f4.*_*f4. 5

exponent在while循环中向右移动直到它达到0.
所以第二次到达时base = rand() % exponent; exponent为0,你有一个除以0