处理Pro * C中的浮动数字

时间:2013-10-14 11:12:35

标签: c oracle oracle-pro-c

我有一个带有NUMBER类型列的Oracle表,其中包含一系列浮点数。使用Pro * C将其读入C变量的正确方法是什么 我尝试过以下方法:

EXEC SQL BEGIN DECLARE SECTION;
static  float   o_start_x;
EXEC SQL END DECLARE SECTION;

EXEC SQL SELECT start_x
FROM  my_table
INTO  :o_start_x;

更常见的是,这不是很好,但是一些浮点数,特别是真正接近0的浮点数(例如1.4E-43)会导致以下错误:

ORA-01426: numeric overflow;

是否有正确/安全的方法来读取这样的值,或者是否有一种方法让oracle足够安全地转换类型,从而导致精度损失?

1 个答案:

答案 0 :(得分:2)

float允许有限的精度 - double有更多,通常是15位数。

警告:例如,在使用资金时,浮点存在问题。示例:.10无法在IEEE-754浮点内部数据表示中准确表示。一个常见的解决方法是让oracle使用BCD算法,避免浮点问题,然后将最终结果读成双精度。

FLT_DIG
This is the number of decimal digits of precision for the float data type. Technically, if p and b are the precision and base (respectively) for the representation, then the decimal precision q is the maximum number of decimal digits such that any floating point number with q base 10 digits can be rounded to a floating point number with p base b digits and back again, without change to the q decimal digits.

FLT_DIG通常是精度最小值的六位数,DBL_DIG:15。

只要你避免做大量的数学运算并用C代码进行比较,除非你知道如何处理我提到的问题和其他问题,否则很容易获得最终结果。

EXEC SQL BEGIN DECLARE SECTION;
static  double   o_start_x;
EXEC SQL END DECLARE SECTION;

EXEC SQL SELECT start_x
FROM  my_table
INTO  :o_start_x;

如果此数字很大,您可能必须使用字符串。 NUMBER的限制是32位精度,超过了常见C数据类型的精度限制。 Pro * C不支持bignum数据类型,AFAIK。