Oracle浮点数与数字 [英] Oracle Floats vs Number

查看:301
本文介绍了Oracle浮点数与数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在看到冲突的引用神谕文档。在数据库中FLOAT和NUMBER类型的小数点之间有什么区别吗?



我记得C等人的一个浮点数有精度限制,一个int不具有。例如,对于'浮动',0.1(基数10)近似为0.110011001100110011001101(基数2),其等于0.100000001490116119384765625(基数10)。但是,对于int,5(Base 10)恰好是101(Base 2)。

这就是为什么以下不会像C中预期的那样终止: p>

  float i; 
i = 0;
for(i = 0; i!= 10;)
{
i + = 0.1
}

但是我看到已将FLOAT定义为NUMBER。据我所知,Oracle的NUMBER类型的实现不会遇到与C的float类似的问题。

那么,真正的故事是什么呢?甲骨文已经偏离了我所期望的漂浮/ FLOAT的规范吗?



(我相信这是一个不同寻常的飓风为什么我会用他们为,但我知道我会有问题,如果0.1 * 10出来1.00000000000000001)

解决方案
BINARY_FLOAT 使用IEEE 754浮点表示法在内部存储数据,如C语言和许多其他语言。当你从数据库中获取它们,并且通常以主机语言将它们存储在一个IEEE 754数据类型中时,它能够复制这个值而不改变它。



FLOAT 数据类型是ANSI SQL NUMERIC数据类型的同义词,在Oracle中称为NUMBER。这是一个精确的数字,是一种缩放的十进制数据类型,它不具有IEEE 754的舍入行为。但是,如果从数据库中提取这些值并将其放入C或Java浮点数,则在此步骤中可能会失去精度。

I'm seeing conflicting references in Oracles documentation. Is there any difference between how decimals are stored in a FLOAT and a NUMBER types in the database?

As I recall from C, et al, a float has accuracy limitations that an int doesn't have. R.g., For 'float's, 0.1(Base 10) is approximated as 0.110011001100110011001101(Base 2) which equals roughtly something like 0.100000001490116119384765625 (Base 10). However, for 'int's, 5(Base 10) is exactly 101(Base 2).

Which is why the following won't terminate as expected in C:

float i;
i = 0;
for (i=0; i != 10; )
{
    i += 0.1
}

However I see elsewhere in Oracle's documentation that FLOAT has been defined as a NUMBER. And as I understand it, Oracle's implementation of the NUMBER type does not run into the same problem as C's float.

So, what's the real story here? Has Oracle deviated from the norm of what I expect to happen with floats/FLOATs?

(I'm sure it's a bee-fart-in-a-hurricane of difference for what I'll be using them for, but I know I'm going to have questions if 0.1*10 comes out to 1.00000000000000001)

解决方案

Oracle's BINARY_FLOAT stores the data internally using IEEE 754 floating-point representation, like C and many other languages do. When you fetch them from the database, and typically store them in an IEEE 754 data type in the host language, it's able to copy the value without transforming it.

Whereas Oracle's FLOAT data type is a synonym for the ANSI SQL NUMERIC data type, called NUMBER in Oracle. This is an exact numeric, a scaled decimal data type that doesn't have the rounding behavior of IEEE 754. But if you fetch these values from the database and put them into a C or Java float, you can lose precision during this step.

这篇关于Oracle浮点数与数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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