Oracle JDBC:双重下溢
Oracle JDBC: underflow in double
尝试将双精度插入 Oracle table 的 DOUBLE PRECISION 列时,当双精度超出范围时出现异常(在我的例子中:太小了),但只有在使用 PreparedStatement
时(如果我使用普通的 Statement
,它会将 double 舍入为 0)。
Table:
create table test
(
VALUE DOUBLE PRECISION
);
Java代码:
double d = 1e-234;
// using Statement works, the number gets rounded and 0 is inserted
try (Statement stmt = conn.createStatement()) {
stmt.executeUpdate("insert into test values (" + d + ")");
}
// using PreparedStatement fails, throws an IllegalArgumentException: Underflow
try (PreparedStatement stmt = conn.prepareStatement("insert into test values (?)")) {
stmt.setDouble(1, d);
stmt.executeUpdate();
}
- 在 insert/update 语句中使用它们之前,我是否必须检查并舍入所有双精度数?
- 我能以某种方式让值自动四舍五入吗?
感谢任何insights/hints。
这是预料之中的,因为双精度最终会转换为数字,而数字只支持范围-1E-130 .. 10E125
。
SQL> select to_char(to_number(1.0e-234)) from dual;
T
-
0
SQL> select to_char(to_number(1e-130)) from dual;
TO_CHAR(TO_NUMBER(1E-130))
----------------------------------------
1.000000000000000000000000000000000E-130
FLOAT:
A subtype of the NUMBER datatype having precision p. A FLOAT value is represented internally as NUMBER. The precision p can range from 1 to 126 binary digits. A FLOAT value requires from 1 to 22 bytes. More...
来自AskTom
Doesn't show that floats aren't numbers, they are just a number with a different precision then number in itself.
DOUBLE PRECISION
= FLOAT(126)
如您在评论中所述。
使用 BINARY_DOUBLE
数据类型与 Java Double
具有相同的精度。
参考:
尝试将双精度插入 Oracle table 的 DOUBLE PRECISION 列时,当双精度超出范围时出现异常(在我的例子中:太小了),但只有在使用 PreparedStatement
时(如果我使用普通的 Statement
,它会将 double 舍入为 0)。
Table:
create table test
(
VALUE DOUBLE PRECISION
);
Java代码:
double d = 1e-234;
// using Statement works, the number gets rounded and 0 is inserted
try (Statement stmt = conn.createStatement()) {
stmt.executeUpdate("insert into test values (" + d + ")");
}
// using PreparedStatement fails, throws an IllegalArgumentException: Underflow
try (PreparedStatement stmt = conn.prepareStatement("insert into test values (?)")) {
stmt.setDouble(1, d);
stmt.executeUpdate();
}
- 在 insert/update 语句中使用它们之前,我是否必须检查并舍入所有双精度数?
- 我能以某种方式让值自动四舍五入吗?
感谢任何insights/hints。
这是预料之中的,因为双精度最终会转换为数字,而数字只支持范围-1E-130 .. 10E125
。
SQL> select to_char(to_number(1.0e-234)) from dual;
T
-
0
SQL> select to_char(to_number(1e-130)) from dual;
TO_CHAR(TO_NUMBER(1E-130))
----------------------------------------
1.000000000000000000000000000000000E-130
FLOAT:
A subtype of the NUMBER datatype having precision p. A FLOAT value is represented internally as NUMBER. The precision p can range from 1 to 126 binary digits. A FLOAT value requires from 1 to 22 bytes. More...
来自AskTom
Doesn't show that floats aren't numbers, they are just a number with a different precision then number in itself.
DOUBLE PRECISION
= FLOAT(126)
如您在评论中所述。
使用 BINARY_DOUBLE
数据类型与 Java Double
具有相同的精度。
参考: