ajwang / groovypptest

Automatically exported from code.google.com/p/groovypptest
0 stars 0 forks source link

implicit bigdecimal to double conversions may cause loss of precision #244

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. example function:
public BigDecimal payment(BigDecimal amount,BigDecimal rate, int p)
{
    BigDecimal tax = 0.13;
    BigDecimal taxedRate = (1+tax) * rate;                            
    BigDecimal payment = taxedRate * ((1+taxedRate)**p);  //i(1+i)^n
    payment = payment / ((((1+taxedRate)**p))-1); //(i(1+i)^n)/((1+i)^n-1) 
    payment = payment * amount;    
    return payment;
}
2. use the function

What is the expected output? What do you see instead?
Example values: payment(new BigDecimal("300"),new BigDecimal("0.045"),12);
Correct output(dynamic compilation):
34.01007900970739461499192657356616109609603881835937500
using @Typed:
34.01007900970739400

What version of the product are you using? On what operating system?
groovy++ 0.2.6, Linux version 2.6.32-3-amd64, Java: 1.6.0_20

Please provide any additional information below.
Using static compilation produces the following code(DECOMPILED):
public BigDecimal payment(BigDecimal amount, BigDecimal rate, int p)
  {
    BigDecimal tax = new BigDecimal("0.13");
    BigDecimal taxedRate = new
BigDecimal(DefaultGroovyPPMethods.plus(DefaultGroovyPPMethods.box(1),
tax).toString()).multiply(rate);
    BigDecimal payment = new
BigDecimal(DefaultGroovyPPMethods.box(taxedRate.doubleValue() *
DefaultTypeTransformation.doubleUnbox(DefaultGroovyMethods.power(new
BigDecimal(DefaultGroovyPPMethods.plus(DefaultGroovyPPMethods.box(1),
taxedRate).toString()), DefaultGroovyPPMethods.box(p)))).toString());
    payment = new
BigDecimal(DefaultGroovyPPMethods.box(payment.doubleValue() /
(DefaultTypeTransformation.doubleUnbox(DefaultGroovyMethods.power(new
BigDecimal(DefaultGroovyPPMethods.plus(DefaultGroovyPPMethods.box(1),
taxedRate).toString()), DefaultGroovyPPMethods.box(p))) - 1)).toString());
    payment = payment.multiply(amount);
    return payment;
}

The precision is lost because of the double conversions. Implicit
conversions of bigdecimal to doubles shouldn't happen...

Original issue reported on code.google.com by jrodrig...@gmail.com on 6 Jun 2010 at 3:59

GoogleCodeExporter commented 8 years ago

Original comment by alex.tka...@gmail.com on 6 Jun 2010 at 5:27

GoogleCodeExporter commented 8 years ago

Original comment by alex.tka...@gmail.com on 6 Jun 2010 at 9:42

GoogleCodeExporter commented 8 years ago
This issue and test seems to be broken after update to 1.7.5

@Roshan, of you have time to have a look it will be very helpful

Original comment by alex.tka...@gmail.com on 20 Sep 2010 at 3:50