简体   繁体   English

Oracle数据类型:有关选择NUMBER与BINARY DOUBLE的建议吗?

[英]Oracle data types: advice on choosing NUMBER versus BINARY DOUBLE?

I'm generating a scientific application that performs a lot of number crunching in Java and C, and accuracy is critical. 我正在生成一个在Java和C中执行大量数字运算的科学应用程序,准确性至关重要。 There is no number crunching done inside the Oracle database (it's merely used for storing variables between math computations). 在Oracle数据库中没有完成数字运算(它仅用于在数学计算之间存储变量)。

I've used double-precision data type for all of my Java and C variables, which is largely based on IEEE 754. So, the data written into the database and then read out of the database will both be from double-precision data types in either C or Java. 我已经为我的所有Java和C变量使用了双精度数据类型,它主要基于IEEE 754.因此,写入数据库然后从数据库中读出的数据都来自双精度数据类型在C或Java中。

What would you recommend I use to store the double-percision data in Oracle -- NUMBER or BINARY DOUBLE? 您建议我用什么来存储Oracle中的双精度数据 - NUMBER或BINARY DOUBLE?

For example, let's say I have a variable called X_Java that I write into the database as variable X_DB as a BINARY DOUBLE. 例如,假设我有一个名为X_Java的变量,我将其作为变量X_DB写入数据库,作为BINARY DOUBLE。 If I were to read this data back into Java from the database and store it in variable X_Java2 , would X_Java exactly match X_Java2 ? 如果我从数据库中将这些数据读回Java并将其存储在变量X_Java2 ,那么X_JavaX_Java2完全匹配?

How would things change I stored X_DB in the database as a NUMBER? 如果事情发生了变化我将数据库中的X_DB存储为NUMBER?

UPDATE 1: Note that my benchmark for "accuracy" is how close the number read OUT OF the database is to the number that was available before being written INTO the database. 更新1:请注意,我的“准确度”基准是数据库中读取的数字与在写入数据库之前可用的数字的接近程度。

One one hand, I'd think that if the number available before being written INTO the database is based on IEEE 754, then the data type used to store this value INSIDE the database should be an exact match if that data type was also based on IEEE 754. 一方面,我认为如果在编写INTO数据库之前可用的数字是基于IEEE 754,那么用于在数据库中存储该值的数据类型应该是完全匹配的,如果该数据类型也基于IEEE 754。

On the other hand, since a 64 bit (double precision) number can only accurately store up to 16 (sometime 17) digits of accuracy, then storing as a NUMBER in the database with 38 digits of precision should accurately map from and to double precision. 另一方面,由于64位(双精度)数字只能精确地存储精度的16位(有时17位),因此在数据库中以38位精度存储为NUMBER应准确地映射到双精度。 One downside is that the NUMBER data type cannot store as large (or as small) values as BINARY DOUBLE. 一个缺点是NUMBER数据类型不能存储与BINARY DOUBLE一样大(或小)的值。

Thus, my posting. 因此,我的帖子。

From Oracle's documentation: 来自Oracle的文档:

For Number datatype: 对于Number数据类型:

The NUMBER datatype stores fixed and floating-point numbers. NUMBER数据类型存储固定和浮点数。 Numbers of virtually any magnitude can be stored and are guaranteed portable among different systems operating Oracle Database, up to 38 digits of precision. 几乎任何数量级的数字都可以存储,并且可以在运行Oracle数据库的不同系统中保证可移植,最高可达38位精度。 ... Oracle guarantees portability of numbers with a precision equal to or less than 38 digits. ... Oracle保证数字的可移植性,精度等于或小于38位。

For Binary Double/Float datatype: 对于Binary Double / Float数据类型:

Oracle Database provides two numeric datatypes exclusively for floating-point numbers: BINARY_FLOAT and BINARY_DOUBLE. Oracle数据库专门为浮点数提供了两种数值数据类型:BINARY_FLOAT和BINARY_DOUBLE。 They support all of the basic functionality provided by the NUMBER datatype. 它们支持NUMBER数据类型提供的所有基本功能。 However, while NUMBER uses decimal precision, BINARY_FLOAT and BINARY_DOUBLE use binary precision. 但是,虽然NUMBER使用小数精度,但BINARY_FLOAT和BINARY_DOUBLE使用二进制精度。 This enables faster arithmetic calculations and usually reduces storage requirements. 这可以实现更快的算术计算,并且通常可以降低存

BINARY_FLOAT and BINARY_DOUBLE are approximate numeric datatypes. BINARY_FLOAT和BINARY_DOUBLE是近似数值数据类型。 They store approximate representations of decimal values, rather than exact representations. 它们存储十进制值的近似表示,而不是精确表示。 For example, the value 0.1 cannot be exactly represented by either BINARY_DOUBLE or BINARY_FLOAT. 例如,值0.1不能由BINARY_DOUBLE或BINARY_FLOAT精确表示。 They are frequently used for scientific computations. 它们经常用于科学计算。 Their behavior is similar to the datatypes FLOAT and DOUBLE in Java and XMLSchema. 它们的行为类似于Java和XMLSchema中的数据类型FLOAT和DOUBLE。

Based off the facts that you are not number crunching in the databases, and are seeking high accuracy retrievals - I would think that Number is the better datatype to use. 根据您在数据库中没有进行数字处理的事实,并且正在寻求高精度检索 - 我认为Number是更好的数据类型。

For your usecase I think BINARY DOUBLE might be the better match. 对于你的用例,我认为BINARY DOUBLE可能是更好的匹配。 While NUMBER could support higher precision, it would involve additional conversions when inserting and fetching. 虽然NUMBER可以支持更高的精度,但在插入和获取时会涉及额外的转换。

If you also need to support special IEEE754 numbers like positive/negative infinity or NaN then that would definitely require BINARY DOUBLE instead of NUMBER. 如果你还需要支持特殊的IEEE754数字,如正/负无穷大或NaN那么肯定需要BINARY DOUBLE而不是NUMBER。 The following is from the oracle documentation on datatypes for 10.2 以下内容来自10.2的数据类型oracle文档

In a NUMBER column, floating point numbers have decimal precision. 在NUMBER列中,浮点数具有小数精度。 In a BINARY_FLOAT or BINARY_DOUBLE column, floating-point numbers have binary precision. 在BINARY_FLOAT或BINARY_DOUBLE列中,浮点数具有二进制精度。 The binary floating-point numbers support the special values infinity and NaN (not a number) . 二进制浮点数支持特殊值无穷大和NaN(不是数字)

Note however that it does not support the distinction between positive and negative zero: 但请注意,它不支持正零和负零之间的区别:

The new datatypes do not conform to IEEE754 in the following areas: 新数据类型在以下方面不符合IEEE754:

  • -0 is coerced to +0. -0被强制为+0。
  • Comparison with NaN is not supported. 不支持与NaN比较。
  • All NaN values are coerced to either BINARY_FLOAT_NAN or BINARY_DOUBLE_NAN. 所有NaN值都强制为BINARY_FLOAT_NAN或BINARY_DOUBLE_NAN。
  • Non-default rounding modes are not supported. 不支持非默认舍入模式。
  • Non-default exception handling mode are not supported. 不支持非默认异常处理模式。

Why not store them as two column values? 为什么不将它们存储为两列值? For each number store the mantissa in a NUMBER column and the exponent in BINARY_FLOAT or BINARY_DOUBLE column. 对于每个数字存储,NUMBER列中的尾数和BINARY_FLOAT或BINARY_DOUBLE列中的指数。 That gives you max. 这给你最大。 precision (not accuracy) and magnitude range. 精度(不精确)和幅度范围。 When you need to use the number, retrieve the two values from the table and "re-combine" them into one number using your JAVA/C code. 当您需要使用该数字时,从表中检索这两个值,并使用您的JAVA / C代码将它们“重新组合”成一个数字。

Like the PLS_INTEGER mentioned previously, the BINARY_FLOAT and BINARY_DOUBLE types in Oracle 10g use machine arithmetic and require less storage space, both of which make them more efficient than the NUMBER type 与前面提到的PLS_INTEGER一样,Oracle 10g中的BINARY_FLOAT和BINARY_DOUBLE类型使用机器算法并且需要更少的存储空间,这两者都使它们比NUMBER类型更有效

http://www.dba-oracle.com/plsql/t_plsql_binary_float.htm http://www.dba-oracle.com/plsql/t_plsql_binary_float.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM