[ 
https://issues.apache.org/jira/browse/PHOENIX-1206?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14109570#comment-14109570
 ] 

Kyle Buzsaki commented on PHOENIX-1206:
---------------------------------------

This is a problem with our DECIMAL.toBytes() and DECIMAL.toObject() 
serialization/deserialization methods. Here's a reproduction in java:

{code}
BigDecimal decimal = new BigDecimal("-0.5"); // produces -0.5
BigDecimal unit = BigDecimal.valueOf(1, 20);  // produces 1^-20
BigDecimal result = decimal.add(unit); // produces -0.49999999999999999999 (the 
correct result)

BigDecimal roundTrip = (BigDecimal) DECIMAL.toObject(DECIMAL.toBytes(result)); 
// produces -0.50000000000000000057 (incorrect result)
{code}

> Decimal arithmetic with negative numbers rounds incorrectly
> -----------------------------------------------------------
>
>                 Key: PHOENIX-1206
>                 URL: https://issues.apache.org/jira/browse/PHOENIX-1206
>             Project: Phoenix
>          Issue Type: Bug
>            Reporter: Kyle Buzsaki
>
> Our decimal type is supposed to support 38 digits of precision, but 
> arithmetic with negative numbers at any precision higher than 19 digits 
> produces incorrect results.
> I have reproduced the error below. In the first two (incorrect) queries, the 
> right hand side is 1^-20 (19 zeros, then a 1). In the last two (correct) 
> queries, the right hand side is 1^-19 (18 zeros, then a 1). 
> Here's a reproduction in sqline: 
> {code}
> 0: jdbc:phoenix:localhost> create table demo (dec DECIMAL primary key);
> No rows affected (0.264 seconds)
> 0: jdbc:phoenix:localhost> upsert into demo values(0.5);
> 1 row affected (0.006 seconds)
> 0: jdbc:phoenix:localhost> upsert into demo values(-0.5);
> 1 row affected (0.005 seconds)
> 0: jdbc:phoenix:localhost> select dec, dec + 0.00000000000000000001 from demo;
> +------------+--------------------------------+
> |    DEC     | (DEC + 0.00000000000000000001) |
> +------------+--------------------------------+
> | -0.5       | -0.50000000000000000057        |
> | 0.5        | 0.50000000000000000001         |
> +------------+--------------------------------+
> 2 rows selected (0.03 seconds)
> 0: jdbc:phoenix:localhost> select dec, dec - 0.00000000000000000001 from demo;
> +------------+--------------------------------+
> |    DEC     | (DEC - 0.00000000000000000001) |
> +------------+--------------------------------+
> | -0.5       | -0.49999999999999999999        |
> | 0.5        | 0.49999999999999999999         |
> +------------+--------------------------------+
> 2 rows selected (0.032 seconds)
> 0: jdbc:phoenix:localhost> 
> 0: jdbc:phoenix:localhost> select dec, dec + 0.0000000000000000001 from demo;
> +------------+-------------------------------+
> |    DEC     | (DEC + 0.0000000000000000001) |
> +------------+-------------------------------+
> | -0.5       | -0.4999999999999999999        |
> | 0.5        | 0.5000000000000000001         |
> +------------+-------------------------------+
> 2 rows selected (0.032 seconds)
> 0: jdbc:phoenix:localhost> select dec, dec - 0.0000000000000000001 from demo;
> +------------+-------------------------------+
> |    DEC     | (DEC - 0.0000000000000000001) |
> +------------+-------------------------------+
> | -0.5       | -0.5000000000000000001        |
> | 0.5        | 0.4999999999999999999         |
> +------------+-------------------------------+
> 2 rows selected (0.029 seconds)
> 0: jdbc:phoenix:localhost> 
> {code}



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Reply via email to