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

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

问题描述

我有双(或浮动)变量可能是空,因为在保持没有有效的值。如何用内置类型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和boolean的包装器,但不能工作,因为我的库具有存储双精度的容器,而不是表现为双精度的对象。另一个将使用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) 函数,您可以通过 math.h

In C, there is a isnan(double) function that you can import through math.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天全站免登陆