我是靠谱客的博主 激动老虎,最近开发中收集的这篇文章主要介绍oracle只舍不入,Oracle SQL舍入不当行为,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

我遇到了使用Oracle SQL进行binary_double舍入的奇怪行为. binary_double值应该根据

documentation舍入一半,但是在使用以下查询进行测试时,似乎存在一些不一致.下面的所有查询应分别给出相同的最后一位数,即0.x00008和0.x00006(舍入为6位)或0.x0008和0.x0006(舍入为5位),其中x为(0,1, 2,3,4,5,6,7,8,9).问题是他们没有.任何有助于理解为什么舍入结果取​​决于分离点之后的第一个数字和/或原始数字中的位数的帮助都是值得赞赏的.

select 1,(round( cast (0.0000075 as binary_double ) ,6)), (round( cast (0.0000065 as binary_double ) ,6)) from dual

union

select 2,(round( cast (0.1000075 as binary_double ) ,6)), (round( cast (0.1000065 as binary_double ) ,6)) from dual

union

select 3,(round( cast (0.2000075 as binary_double ) ,6)), (round( cast (0.2000065 as binary_double ) ,6)) from dual

union

select 4,(round( cast (0.3000075 as binary_double ) ,6)), (round( cast (0.3000065 as binary_double ) ,6)) from dual

union

select 5,(round( cast (0.4000075 as binary_double ) ,6)), (round( cast (0.4000065 as binary_double ) ,6)) from dual

union

select 6,(round( cast (0.5000075 as binary_double ) ,6)), (round( cast (0.5000065 as binary_double ) ,6)) from dual

union

select 7,(round( cast (0.6000075 as binary_double ) ,6)), (round( cast (0.6000065 as binary_double ) ,6)) from dual

union

select 8,(round( cast (0.7000075 as binary_double ) ,6)), (round( cast (0.7000065 as binary_double ) ,6)) from dual

union

select 9,(round( cast (0.8000075 as binary_double ) ,6)), (round( cast (0.8000065 as binary_double ) ,6)) from dual

union

select 10,(round( cast (0.9000075 as binary_double ) ,6)), (round( cast (0.9000065 as binary_double ) ,6)) from dual

union

select 11,(round( cast (0.000075 as binary_double ) ,5)), (round( cast (0.000065 as binary_double ) ,5)) from dual

union

select 12,(round( cast (0.100075 as binary_double ) ,5)), (round( cast (0.100065 as binary_double ) ,5)) from dual

union

select 13,(round( cast (0.200075 as binary_double ) ,5)), (round( cast (0.200065 as binary_double ) ,5)) from dual

union

select 14,(round( cast (0.300075 as binary_double ) ,5)), (round( cast (0.300065 as binary_double ) ,5)) from dual

union

select 15,(round( cast (0.400075 as binary_double ) ,5)), (round( cast (0.400065 as binary_double ) ,5)) from dual

union

select 16,(round( cast (0.500075 as binary_double ) ,5)), (round( cast (0.500065 as binary_double ) ,5)) from dual

union

select 17,(round( cast (0.600075 as binary_double ) ,5)), (round( cast (0.600065 as binary_double ) ,5)) from dual

union

select 18,(round( cast (0.700075 as binary_double ) ,5)), (round( cast (0.700065 as binary_double ) ,5)) from dual

union

select 19,(round( cast (0.800075 as binary_double ) ,5)), (round( cast (0.800065 as binary_double ) ,5)) from dual

union

select 20,(round( cast (0.900075 as binary_double ) ,5)), (round( cast (0.900065 as binary_double ) ,5)) from dual;

底线是这个:

为什么在以下查询中,两个值之间存在差异:

SELECT (round( CAST (0.0000065 AS BINARY_DOUBLE ) ,6)), (round( cast (0.1000065 as binary_double ) ,6)) FROM dual;

按照@zerkms的建议,我将convert的数字转换成二进制格式,然后得到:

0.0000065 -> 6.49999999999999959998360846147E-6

0.1000065 -> 1.00006499999999998173905169097E-1

查询将最多舍入为6位数.令人惊讶的是,对我来说,我看到四舍五入导致:

0.0000065 -> 0.000006 (execute the query above to see this)

0.1000065 -> 0.100007 (execute the query above to see this)

这是为什么?我可以理解,如果我尝试转向> 12位数,其中二进制表示中的一系列数字开始不同,但是如何在这样的早期阶段看到差异?

最后

以上就是激动老虎为你收集整理的oracle只舍不入,Oracle SQL舍入不当行为的全部内容,希望文章能够帮你解决oracle只舍不入,Oracle SQL舍入不当行为所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部