When/Why Oracle 是否将 NaN 添加到数据库中的行 table

When/Why does Oracle adds NaN to a row in a database table

我知道 NaN 代表 Not a Number。但是,我无法理解 Oracle 何时以及为何将其添加到一行中。

是遇到负数之类小于0的值还是垃圾值时

From the documentaton:

The Oracle Database numeric data types store positive and negative fixed and floating-point numbers, zero, infinity, and values that are the undefined result of an operation—"not a number" or NAN.

据我所知,您只能在 binary_float or binary_double column; those data types have their own literals for NaN too, and there's an is nan condition for them too, and the nanvl() function 中获取 NaN 来操作它们。

获得这样一个值的方法的一个例子是将零 float/double 值除以零:

select 0f/0 from dual;

0F/0
----
NaN  

... 因此,如果您看到 NaN,您的应用程序逻辑或基础数据可能已损坏。 (注意你不能用 'normal' 数字类型得到这个;你得到 ORA-01476: divisor is equal to zero 除非分子是 float 或 double)。

但是对于零数或负数,您不会得到 NaN。也有可能您有一个字符串列,并且应用程序将单词 'NaN' 放入其中,但是将数字存储为字符串在很多层面上都是一个坏主意,所以希望情况并非如此。

不 <=0 仍然是一个数字,所以不完全是。 NaN(或无穷大)是 DB 在处理不可计算的数字(+-∞,或者只是不是数字的东西)时用来保持理智的特殊值。 这是一些代码:

DECLARE
  l_bd_test   binary_double;
  l_int_test  INTEGER;
BEGIN
  l_bd_test   := 'NAN';
  l_int_test  := 0;
  IF l_bd_test IS NAN THEN 
    DBMS_OUTPUT.PUT_LINE(l_bd_test || ' IS NAN');
  ELSE
    DBMS_OUTPUT.PUT_LINE(l_bd_test || ' IS A #');
  END IF;
  IF l_int_test IS NAN THEN 
    DBMS_OUTPUT.PUT_LINE(l_int_test || ' IS NAN');
  ELSE
    DBMS_OUTPUT.PUT_LINE(l_int_test || ' IS A #');
  END IF;
END;
/

NAN代替INFINITY甚至否定它并查看结果。