浮动或双重特殊价值 [英] Float or Double Special Value

查看:100
本文介绍了浮动或双重特殊价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有double(或float)变量可能是空,因为没有保留有效的值。我如何用内置的float类型和double类型来表示这个条件?

I have double (or float) variables that might be "empty", as in holding no valid value. How can I represent this condition with the built in types float and double?

一个选项是一个包含一个float和一个布尔值的包装器,但是不能工作,因为我的库有容器存储双打,而不是双重的对象。另一个将使用NaN(std :: numeric_limits)。但是我没有办法检查一个NaN的变量。

One option would be a wrapper that has a float and a boolean, but that can´t work, as my libraries have containers that store doubles and not objects that behave as doubles. Another would be using NaN (std::numeric_limits). But I see no way to check for a variable being NaN.

如何解决需要特殊浮点值来表示数字以外的其他问题?

How can I solve the problem of needing a "special" float value to mean something other than the number?

推荐答案

在Visual C ++中,有一个非标准的 _isnan(double) 您可以通过导入的功能float.h

In Visual C++, there is a non-standard _isnan(double) function that you can import through float.h.

在C中,有一个 isnan(double) 您可以通过数学导入的功能.h

在C ++中,有一个isnan(double)函数可以通过 cmath

In C++, there is a isnan(double) function that you can import through cmath.

正如其他人所指出的,使用NaN可能会很麻烦。它们是一个特殊情况,必须像NULL指针那样处理。不同之处在于,NaN通常不会导致核心转储和应用程序失败,但它们难以追踪。如果您决定使用NaN,请尽量少使用它们。过度使用NaN是一种冒犯性的编码习惯。

As others have pointed out, using NaN's can be a lot of hassle. They are a special case that has to be dealt with like NULL pointers. The difference is that a NaN will not usually cause core dumps and application failures, but they are extremely hard to track down. If you decide to use NaN's, use them as little as possible. Overuse of NaN's is an offensive coding practice.

这篇关于浮动或双重特殊价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆