gpt4 book ai didi

c++ - 当方程式返回 nan 作为答案时该怎么办?

转载 作者:行者123 更新时间:2023-11-28 03:07:42 24 4
gpt4 key购买 nike

我的程序一直有一个小问题,我想做的是为用户开发一种方法来模拟可能的密码强度。这是假设所有密码都是排列(我知道这很奇怪,但我认为这是为了阻止数据变得更加笨拙。)使用等式......

//n!/(n-r)!当 n! = (e^-n)*(n^n) 平方根 (2(pi)n)。当n为使用的字符数,r为密码长度时

无论我输入什么,我都会收到 nan 作为答案。我认为也许我的方程式不对(也许我以某种方式除以零)所以我重新设计并大大简化了它。但这似乎不是问题所在,尽管我觉得这让我更接近于正确。但我想也许数字溢出在这里有影响?但我真的不知道如何解决这样的问题。我尝试从不同的数据类型跳转,但似乎没有任何效果。

我也有模数问题。它返回的时间小于零的数字,所以根据我的菜鸟知识告诉我,也许我又溢出了但是我还怎么使用 % 而不将它定义为 int?也许解决上述问题会解决这个问题?

如有任何帮助,我将不胜感激。如何处理 nan 的返回值?有没有一步步解决的现状?它几乎总是溢出还是可能是其他原因?

代码本身。

#include <iostream>
#include <cmath>

using namespace std;

const int SECONDS_IN_YEAR = 31556926;
const int SECONDS_IN_DAY = 86400;
const int SECONDS_IN_HOUR = 3600;
const int SECONDS_IN_MIN = 60;

int main()
{

int passwordLength ,characterSymbols;
double instructionsPerSecond, instructionSuccess;

////////////////////////////////////////////////////////////////////////////////
//Equations needed
// n!/(n-r)!
//n is the number of letters in the alphabet
//and r is the number of letters in the password

// n! = (e^-n)*(n^n) sqrt(2(pi)n)
double numeratorFactorial = (pow(M_E,-characterSymbols))
*(pow(characterSymbols,characterSymbols))
*(sqrt(2*M_PI*characterSymbols));
// (n-r)
double characterMinusLength= (characterSymbols-passwordLength);

// (n-r)! = (e^-(n-r)) * ((n-r)^(n-r)) * sqrt(2(pi)(n-r))
double denominatorFactorial = ((pow(M_E, -(characterMinusLength)))*
(pow((characterMinusLength),(characterMinusLength)))
* (sqrt(2*M_PI*(characterMinusLength))));

// n!/(n-r)!
long double passwordPermutation = (numeratorFactorial / denominatorFactorial);

// (passwords)* (instructions/Password) * (seconds/instruction) = sec
int passwordSeconds = (passwordPermutation * instructionSuccess)
*(1/instructionsPerSecond);



int passwordMin = passwordSeconds / SECONDS_IN_MIN ;
int passwordHour = passwordSeconds / SECONDS_IN_HOUR;
int passwordDay = passwordSeconds / SECONDS_IN_DAY ;
int passwordYear = passwordSeconds / SECONDS_IN_YEAR;


////////////////////////////////////////////////////////////////////////////////
//Explain purpose of program
cout << "This program is designed to simulate the strength of passwords." << endl;

//Ask for alphabet
cout << "But first, share with me the max number of characters you'd be using."
<< endl;
cin >> characterSymbols;
//Reflect information
cout << "We will be using " << characterSymbols << " character symbols to "
<< " construct the password.\n" << endl;



///////////////////////////////////////////////////////////////////////////////
//Input length of password
cout << "\n\nWill you give me the length of proposed password?" << endl;
cin >> passwordLength;

//Repeat information
cout << "The password length will be " << passwordLength << "." <<endl;



//cout permutations
cout << "This would lead to " << passwordPermutation << " unique password\n"
<< endl;



////////////////////////////////////////////////////////////////////////////////
//Ask for computer strength
cout << "How powerful is this computer? How many instructions per second " << endl;
cout << "can it accomplish?" << endl;
cin >> instructionsPerSecond;

//Read out computer strength
cout << "The computer can do " << instructionsPerSecond << " instructions/second"
<< endl << endl;



////////////////////////////////////////////////////////////////////////////////

//Ask for instructions/password
cout << "The number of instructions needed to test your password is." << endl
<< endl;
cin >> instructionSuccess;

//reflect
cout << "This computer can do " << instructionSuccess
<< " instructions/password" << endl;


////////////////////////////////////////////////////////////////////////////////
cout << "\n\nThe amount of seconds it'll take to crack this passcode is... "
<< endl << passwordSeconds << " seconds.\n\n\n\n\n" << endl;

////////////////////////////////////////////////////////////////////////////////
//Reflect all information in an easily readable table
cout << "Number of character symbols using... " << characterSymbols << endl;
cout << "Length of password... " << passwordLength << endl;
cout << "Number of permutations... " << passwordPermutation << endl;
cout << "Instructions per second... " << instructionsPerSecond << endl;
cout << "Instructions per password..." << instructionSuccess << endl;

cout << endl << endl << endl;

////////////////////////////////////////////////////////////////////////////////
//Add in conversions for min, hour, day, years
cout << "Number of seconds to break..." << passwordSeconds << endl;


cout << "Converted to minutes..." << passwordMin << endl;
passwordMin = passwordSeconds / SECONDS_IN_MIN;
passwordSeconds = passwordSeconds % SECONDS_IN_MIN;

cout << "Converted to hours..." << passwordHour << endl;
passwordHour = passwordSeconds / SECONDS_IN_HOUR;
passwordSeconds = passwordSeconds % SECONDS_IN_MIN;

cout << "Converted to days..." << passwordDay << endl;
passwordDay = passwordSeconds / SECONDS_IN_DAY;
passwordSeconds = passwordSeconds % SECONDS_IN_DAY;

cout << "Converted to years..." << passwordYear << endl;
passwordYear = passwordSeconds / SECONDS_IN_YEAR;
passwordSeconds = passwordSeconds % SECONDS_IN_YEAR;

return (0);
}

最佳答案

“nan”代表“不是数字”。发生这种情况是因为您声明了变量 characterSymbols 和 passwordLength 而没有给它们初始值。

您必须在使用任何变量之前对其进行初始化 - 如果您不这样做,那么您将有未确定的行为。例如:

int x;
int y;
int z = x + y;

这里无法预测 z 等于什么,因为我们不知道 x 或 y 等于什么。同样,您的代码应该是这样的:

int characterSymbols = 10;  //or whatever you want the initial value to be

...

double numeratorFactorial = (pow(M_E,-characterSymbols))
*(pow(characterSymbols,characterSymbols))
*(sqrt(2*M_PI*characterSymbols));

这样,numeratorFactorial 就会有一个有效的值。

关于c++ - 当方程式返回 nan 作为答案时该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19303847/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com