3

Golden ratio is a special ratio which can be related to ratio between different dimensions of parts of a living organism.

Mathematically, it can be found by the following formula :
(1+sq.root(5))/2)

By using any kind of data-type I couldn't store such a value. double is the best, but I can't get 20 decimal places using it.

So, I thought, if I can find the last digits of the ratio after calculating it, and then store it in some variable and go on doing this. In the end I could have made it complete by joining the decimal places one after the other.

example : from 1.61083, I would extract 3,0,8,0,1,6 and join it.

Still I'm having problems since even during calculation it does not use such an accurate variable.

Can somebody help ?

tbodt
  • 16,609
  • 6
  • 58
  • 83

3 Answers3

4

java.math.BigDecimal is your way to go

BigDecimal.ONE.add(new BigDecimal(Math.sqrt(5))).divide(new BigDecimal(2))

represents the formula that you had posted and will return 1.6180339887498949025257388711906969547271728515625

That is, till 49 decimal places

Shubham
  • 310
  • 4
  • 16
Philipp Sander
  • 10,139
  • 6
  • 45
  • 78
  • Acording to this [link](http://www.goldennumber.net/phi-million-places/) my snippet is more precise :P – eltabo Feb 04 '14 at 11:12
0

You can use java.math.BigDecimal as mentioned by @Philipp. but the very next problem you will face is that java doesnot provide sqrt function for BigDecimals. So here is your way out. I found this code somewhere that can help you calculate sqrt of BigDecimals up to any precision.

public static BigDecimal sqrt(BigDecimal d, int precison) {
    BigDecimal TWO =BigDecimal.valueOf(2);
    MathContext mc = new MathContext(precison);
    BigDecimal res = BigDecimal.ONE;
    for(int i=1;i<=precison;i++) {
        res = res.add(d.divide(res,mc)).divide(TWO,mc);
    }

    return res;

}
Sanjeev
  • 9,876
  • 2
  • 22
  • 33
  • Rather than writing a method to approximate a square root, one could rely upon the fact that if x is an approximation of the golden mean, (x+1)/x will be a better approximation of the golden mean. – supercat Feb 06 '14 at 23:45
  • 1
    Sanjeev, you are wrong, sqrt functions ARE there in java. Please edit your post – Shubham Feb 07 '14 at 10:02
  • 1
    @Shubham sqrt functions are there but none of them is for BigDecimals directly and once i needed a bigdecimal sqrt up to 100 decimal places and this function came to the rescue. – Sanjeev Feb 07 '14 at 10:07
0

Using this post to calculate square root:

    BigDecimal five = new BigDecimal(5);
    BigDecimal two = new BigDecimal(2);

    BigDecimal x = new BigDecimal(Math.sqrt(five.doubleValue()));
    BigDecimal sqrt_five =  x.add(new BigDecimal(five.subtract(x.multiply(x)).doubleValue() / (x.doubleValue() * 2.0)));
    System.out.println(BigDecimal.ONE.add(sqrt_five).divide(two));

You get: 1.61803398874989484820458683436564162637092283739506413642905463708132618450008521904237568378448486328125

Hope it helps.

Community
  • 1
  • 1
eltabo
  • 3,749
  • 1
  • 21
  • 33