This revision made January 05, 2013 23:04, by Anatole

# JSR 354 - Aspects regarding Precision

Basically it is required to support very large numbers and a very large number of decimal places. There is quite some debate on how this can be realized efficiently. Candidates identified are

• `BigDecimal`
• single `long`
• two `long`s (pounds & fraction)
• new `Decimal` class (probably wrapping two longs)
• `double` should be ruled out as inaccurate
• `Number` should be ruled out as being abstract and non-precise

We have decided on using a Decimal class, that will encapsulate 2 longs - one long for the integer part and the other for the fractional part. Arithmetic needs to take this into account.

### Possible Issue

1000/3 = 333.33, but 333.33*3 = 999.99 which is one penny short. Since this would not work in an accounting application, need to consider an api where the penny is accounted for, Several options:

• return an array where the first element is the quotient, and the second element is the excpetion: [333.33, 333.34]
• Or represent the result and remainder [333.33, .01]

Both of these approaches would require a third number to represent the remainder or exception. In both approaches, the remainder or exception needs to be included in further calculations.

So 100/3 = [33, 34]. Now let's say we want to divide that by 4, so the 33 term produces [8,9] and the 34 term produces [8,10], so how do you capture all of those remainders?

Another issue. To show it clearly, assume 0 decimal precision, so - 34/4 = [8, 10] using exception approach, or [8, 2] using remainder approach. In either case, "multiplying" back yields the correct result: 8+8+8+10 = 34. But shouldn't that be better represented as 8+8+9+9? so how do you represent that.

Another approach - retain the fractional representation and work with the formatter to iterate through the results, adding the missing penny to the last one. Then all arithmetic would be fractional arithmetic.

Difference compared to previous revision
<span><span style="text-decoration:line-through;color:red">'''</span><span style="text-decoration:underline;color:green">=<img align="right" src="http://asset-1.java.net/attachments/images/project/javamoney.png?20121214.f926c51"/> <br/>JSR 354 - Aspects regarding </span>Precision<span style="text-decoration:line-through;color:red">'''</span><span style="text-decoration:underline;color:green"><br/><br/><br/></span> <span style="text-decoration:line-through;color:red">*</span><span style="text-decoration:underline;color:green">Basically</span> <span style="text-decoration:line-through;color:red">S</span><span style="text-decoration:underline;color:green">it is req</span>u<span style="text-decoration:underline;color:green">ired to su</span>upport <span style="text-decoration:line-through;color:red">a </span>pport very large number<span style="text-decoration:underline;color:green">s</span>very large number and a very large number of decimal places<span style="text-decoration:line-through;color:red"> *</span> and a very large number of decimal places<span style="text-decoration:underline;color:green">.</span> <span style="text-decoration:underline;color:green">There is quite some debate on how this can be realized efficiently. </span>Candidates <span style="text-decoration:underline;color:green">identified </span>are ** <span style="text-decoration:underline;color:green"><code></span>BigDecimal<span style="text-decoration:underline;color:green"></code></span> ** single <span style="text-decoration:underline;color:green"><code></span>long<span style="text-decoration:underline;color:green"></code></span> ** two <span style="text-decoration:underline;color:green"><code></span>long<span style="text-decoration:underline;color:green"></code></span>s (pounds & fraction) ** new <span style="text-decoration:underline;color:green"><code></span>Decimal<span style="text-decoration:underline;color:green"></code></span> class (probably wrapping two longs) * <span style="text-decoration:underline;color:green"><co</span>d<span style="text-decoration:underline;color:green">e>d</span>ouble<span style="text-decoration:underline;color:green"></code></span> should be ruled out as inaccurate * <span style="text-decoration:underline;color:green"><code></span>Number<span style="text-decoration:underline;color:green"></code></span> should be ruled out as being abstract and non-precise We have decided on using a Decimal class, that will encapsulate 2 longs - one long for the integer part and the other for the fractional part. Arithmetic needs to take this into account. <span style="text-decoration:underline;color:green"> ==Additional Notes ===Possible </span> Candidates are ** BigDecimal ** single long ** two longs (pounds & fraction) ... We have decided on using a Decimal class, that will encapsulate 2 longs - one long for the integer part and the other for the fractional part. Arithmetic needs to take this into account. Issue<span style="text-decoration:line-through;color:red"> - </span>Issue<span style="text-decoration:underline;color:green"> </span>1000/3 = 333.33, but 333.33*3 = 999.99 which is one penny short. Since this would not work in an accounting application, need to consider an api where the penny is accounted for, Several options<span style="text-decoration:underline;color:green">: *</span>1000/3 = 333.33, but 333.33*3 = 999.99 which is one penny short. Since this would not work in an accounting application, need to consider an api where the penny is accounted for, Several options <span style="text-decoration:line-through;color:red">- </span> return an array where the first element is the quotient, and the second element is the excpetion: [333.33, 333.34] <span style="text-decoration:underline;color:green">* </span>Or represent the result and remainder [333.33, .01] <span style="text-decoration:underline;color:green"> </span>return an array where the first element is the quotient, and the second element is the excpetion: [333.33, 333.34] Or represent the result and remainder [333.33, .01] Both of these approaches would require a third <span style="text-decoration:line-through;color:red">lo</span>n<span style="text-decoration:line-through;color:red">g</span>Both of these approaches would require a third n<span style="text-decoration:underline;color:green">umber</span> to represent the remainder or exception.<span style="text-decoration:line-through;color:red"> </span><span style="text-decoration:underline;color:green"> </span>In both approaches, the remainder or exception needs to be included in further calculations. So 100/3 = [33, 34]. Now let's say we want to divide that by 4, so the 33 term produces [8,9] and the 34 term produces [8,10], so how do you capture all of those remainders? <span>