gpt4 book ai didi

c++ - 如何检查 double 的未定义值?

转载 作者:行者123 更新时间:2023-11-30 02:07:04 24 4
gpt4 key购买 nike

我正在尝试找出一种方法来检查坡度的未定义值,在这种情况下它会是垂直的。我试过使用 NULL,但这似乎不起作用。

double Point::Slope(Point &p2)
{
double slop = 0;
slop = (y - p2.y) / (x - p2.x);

if (slop == NULL)
{
slop = 10e100;
}

return slop;
}

最佳答案

如果你用“undefined”表示 nan(“不是数字”),你应该首先避免计算 1,即通过检查“/”操作的分母不为零。其次,您始终可以通过以下方式检查 nan

#include <cmath>
bool std::isnan(x); // since C++11
bool isnan(x); // pre C++11, from the C math library, defined as macro

请参阅手册页,或 cppreference .

关于c++ - 如何检查 double 的未定义值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8219820/

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