C++ 程序意外退出

c++ programm exiting unexpectedly

本文关键字:退出 意外 程序 C++      更新时间:2023-10-16

我有一个Programm,其中我有两个嵌套的for循环。外部迭代参数为 x,内部参数为 y。这段代码继续到 x:5 和 y:4,但我希望它能持续到 x:1000 和 y:999。

你能解释一下发生了什么吗?
所以我有这个代码:

#include <iostream>
#include <cmath>
using namespace std;

int main() {
printf("start");
long double  result;
for (unsigned int x = 0; x < 1000; x++) {
for (unsigned int y = 0; y < x; y++) {
result = pow(50, x) - pow(50, y);
if (((int)result) == result && result!=0) {
printf("x: %07d y:%07d rn", x, y);
}
}
}
}

问题就在这里:

if (((int)result) == result && result!=0) {
printf("x: %07d y:%07d rn", x, y);

更准确地说,在条件下

((int)result) == result      // the same as (int)result == result

仅将您的输出限制在x == 5y == 4的点 — 循环本身继续x == 1000y == 999

如果在这种情况下将int更改为long int,您可能会看到它:

if (((long int)result) == result && result!=0) {
printf("x: %07d y:%07d rn", x, y);

您的输出会更长(最多x == 11y == 10在我的机器中(。

为了调试您的程序,我在"if"语句之前添加了以下"printf"

printf("x: %07d y:%07d %d %Lfrn", x, y, (int)result, result);

我可以看到以下结果:

...
x: 0000005 y:0000004 306250000 306250000.000000 
x: 0000006 y:0000000 -2147483648 15624999999.000000
...

当您将长双精度转换为 in 和结果 != (int(result 时,您可以看到整数类型溢出,这就是为什么您的程序在 x:4 y:5 之后停止打印数据的原因

但是为什么需要"如果"语句呢?您要检查什么条件?也许你可以删除它?

编辑:因为,如前所述,我提出的答案非常不正确,所以请允许我修复它。

数据类型的最小值/最大值

我想首先参考代码,您可以在其中轻松显示系统上某些数据类型的最大值:

template<typename T>
void showMinMax() {
cout << "tmin: " << numeric_limits<T>::min() << endl;
cout << "tmax: " << numeric_limits<T>::max() << endl;
cout << endl;
}

并在您的main()函数中

cout << "int: " << endl;
showMinMax<int>();
cout << "long int: " << endl;
showMinMax<long int>();
cout << "float: " << endl;
showMinMax<float>();
cout << "double: " << endl;
showMinMax<double>();
cout << "long double: " << endl;
showMinMax<long double>();

打印这些限制一次以进行调试,并牢记它们。

您的 if 子句

现在,@MarianD的评论非常正确:您(尝试(将结果保存在long double变量中,但随后将其压缩回long int(甚至int(进行比较。这不会让你得到你最喜欢的效果。

看看if-子句中的另一个调试打印语句:

if (((long int)result) == result && result!=0) {
// Compare the value for the long int with the max value for the long int data type
printf("Comparing:nt(int)result: %dnt(long int)result: %ldntresult: %Len",(int)result,(long int)result,result);
printf("x: %07d y:%07d rn", x, y);
}

在这里,您可以观察成功比较后比较的值。您会注意到,(long int)result的最后一个值(在x==11y == 10处(接近系统上long int的最大值。

我不太明白你为什么要做这样的比较,但是当你在迭代中前进时,你会遇到其他麻烦:

pow(( 函数

正如您在pow()函数的 C++ 引用中看到的那样,根据输入参数,它返回不同的输出参数。我添加了另一个调试语句,如果pow()函数的结果Inf,则中断循环:

long double ld50 = 50.0;
rx = pow(ld50,x);
if(isinf(rx)){
printf("pow(50,%d) = Infn",x); rx = pow(ld50,x-1);
printf("Last valid value pow(50,%d) = %Len",x-1,rx);
break;
}

另请参阅isinf()函数的C++参考。
取决于您使用的C++标准,例如 C++98

波(双基数,双指数(;
浮点数 pow(浮点基数、浮点指数(;长双
基(长双基、长双指数(;
双 Pow (双基数,整数指数(;
长双基(长双基,整数指数(;

您必须改变输入参数的类型才能获得pow()函数所需的输出参数。

建议的解决方案

#include <iostream>
#include <cmath>
using namespace std;
int main() {
printf("startn");
long double result, rx, ry;
long double ld50 = 50.0;
for (unsigned int x = 0; x < 1000; x++) {
// Computing the power apriori in long doubles
rx = pow(ld50,x);
// Checking for infinity, if true, break
if(isinf(rx)){
printf("pow(50,%d) = Infn",x); rx = pow(ld50,x-1);
printf("Last valid value pow(50,%d) = %Len",x-1,rx);
break;
}
for (unsigned int y = 0; y < x; y++) {
ry = pow(ld50,y);
result = rx-ry;
printf("x: %07d y:%07d res: %Lern", x, y,result);
}
}
}