Finding the gold ratio up to exactly 20 decimal places

The golden ratio is a special relationship that can be related to the ratio between the different sizes of parts of a living organism.

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

Using any type of data, I could not store such a value. doubleis the best, but I cannot use 20 decimal places using it.

So, I thought, can I find the last digits of the relationship after calculating it, and then save it in some variable and continue to do so. In the end, I could make it complete by combining the decimal places one by one.

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

Nevertheless, I have problems, because even during the calculation, it does not use such an exact variable.

Can anyone help?

+3
source share
3 answers

java.math.BigDecimal is your way

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

represents the formula you published and will return 1.6180339887498949025257388711906969547271728515625

That is, up to 49 decimal places

+4
source

You can use java.math.BigDecimalas indicated by @Philipp. but the next problem you will encounter is that java does not provide the sqrt function for BigDecimals. So here is your way out. I found this code somewhere that can help you calculate sqrt BigDecimals 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;

}
0
source

:

    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));

: 1.61803398874989484820458683436564162637092283739506413642905463708132618450008521904237568378448486328125

, .

0
source

All Articles