4 Replies Latest reply: Apr 7, 2013 4:49 PM by rp0428

# BigDecimal and POW

Hello

In the following example, I compute new_amount_d in 2 different ways and I get 2 different values although it should be the same in theory.

Is there a way to get the same value with BigDecimal for instance ?

I'm new to Java and I'm working for financial industry...that's why I'm trying to solve this problem.

Thanks for helping.

public class num004
{
public static void main(String[] args)
{

double amount_d = 1000;
double interest_rate_d = 0.03;
double new_amount_d = amount_d;
for(int i = 0;i<365;i++)
{
new_amount_d = new_amount_d * Math.pow(1+interest_rate_d,(double)1/365);
}
System.out.println("new amount_d :"+new_amount_d);

new_amount_d = amount_d * Math.pow(1+interest_rate_d,(double) 1);
System.out.println("new amount_d :"+new_amount_d);

}
}
• ###### 1. Re: BigDecimal and POW
`````` tags around your samples when posting here...

The wrong result is cause by your double values.
native decimal numbers are not precise because of two resons:
<ol><li>there is s limmited number of bits/bytes to represent the value.
<li>binary representation used by the Computer differs from human readable decimal format in that way that some decimal fractions form entless binary fractions, wich connot be represented due to leck of bits/bytes
</ol>
Use only <tt>BigDecimal</tt> objects and their methods for calulation.

bye
TPD                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ``````
• ###### 2. Re: BigDecimal and POW
OK, according to what I red, I understood that BigDecimal is the solution.

But, How to compute power( BigDecimal, 1/365) ?

In the API, I saw only power(BigDecimal,int).
• ###### 3. Re: BigDecimal and POW
Zabo wrote:
OK, according to what I red, I understood that BigDecimal is the solution.

But, How to compute power( BigDecimal, 1/365) ?

In the API, I saw only power(BigDecimal,int).
http://en.wikipedia.org/wiki/Exponentiation#Arbitrary_integer_exponents
And since you're a lucky guy there's a lib for you:
http://krum.rz.uni-mannheim.de/jas/

bye
TPD
• ###### 4. Re: BigDecimal and POW
>
In the following example, I compute new_amount_d in 2 different ways and I get 2 different values although it should be the same in theory.
>
No - those would NEVER be the same in theory or in practice since they use two vastly different formulas.
``````new_amount_d = new_amount_d * Math.pow(1+interest_rate_d,(double)1/365);
. . .
new_amount_d = amount_d * Math.pow(1+interest_rate_d,(double) 1);``````
The second line of code uses an exponent of '1' which means it doesn't compute a 'power' at all. Anything raised to the power of '1' is just the same value. The first example uses '1/365' as the exponent and that is way different than the '1' used in the second example.
>
I'm new to Java and I'm working for financial industry...that's why I'm trying to solve this problem.
>
Then start over and tell what the actual problem is that you are trying to solve.

What are you trying to compute? What financial formula are you trying to use?

Forget your code and provide an example, in English, showing:

1. the input values
2. what you are trying to compute
3. the expected output values.

Then someone can try to help you find the best way to do that.