Oracle rounding in special circumstances

Source: Internet
Author: User
For the strange data, the value of select is obviously 0.0140625, but it is not rounded down by round (vv, 6. It should be calculated as follows: 0.014063, but the actual result is: 0.0140

For the strange data, the value of select is obviously 0.0140625, but it is not rounded down by round (vv, 6. It should be calculated as follows: 0.014063, but the actual result is: 0.0140

For the strange data, the select statement is clearly 0.0140625, but it is not rounded out by round (vv, 6. It should be calculated as follows: 0.014063, but the actual result is: 0.014062. It violates the rounding principle.

Case:

Drop table tt;
Create table tt
Select a, v, exp (sum (ln (1 + v) over (order by rownum)-1 x
From (select a, lag (a, 1, a) over (order by r desc)/a-1 v
From (select 1.2980 a, 2 r
From dual
Union all
Select 1.2800 a, 1 r from dual ))

SQL> select a, v, round (v, 6) round_v, x, round (x, 6) round_x from tt;

A v ROUND_V x ROUND_x
--------------------------------------------------
1.298 0 0 0 0
1.28 0.0140625 0.014063 0.0140625 0.014062

After carefully observing the above results, we can find that the field v and field x are "same", both of which are 0.0140625 ,. However, the same round 6 results for the two fields are different. The value of round_v is 0.014063, that is, rounding. The round_x corresponding to x is 0.014062, with no rounding.

After further dumping, we found that the internal data storage is different:
V dump result:
Typ = 2 Len = 5: 192,2, 41,63, 51
Dump result of x:
Typ = 2 Len = 20: 50,100,100,100,100,100,100,100,100,100,100,100,100,100
The storage of the same data varies greatly.

Now, let's dig deeper into the dump results of x:
Returns an index of 192: 192-193 =-1.
Then we can get the digits:
(2-1) * 100 ^ (-1-0) +
(41-1) * 100 ^ (-1-1) +
(63-1) * 100 ^ (-1-2) +
(50-1) * 100 ^ (-1-3) +
(100-1) * 100 ^ (-1-4)
= 0.0140624999

Now, you know why.
Because the storage in Oracle is 0.0140624999, that is, the seventh digit after the decimal point is 4, rather than 5, it is not "in" When rounding ". The translation result of v is 0.0140625, so it is "in.
I don't know whether the problem is displayed or a bug handled by Oracle. There are two reasons for this problem:
1. Use of the exp ln function. If you use addition, subtraction, multiplication, division, the result is correct.
2. Data difference occurs only when x. xxxxx49999 is used. The resulting problems are still obvious.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.