Exponentiation of a Java BigInteger with a value lower than 1 [duplicate] - java

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to do a fractional power on BigDecimal in Java?
I have a BigInteger A that I need to exponentiate with 1/b (b is an int).
My problem is that A supports only A.pow(int) which is not suitable for my case.
Is there any workaround for this?

Newton's method is your jam.

because BigInteger are intended for Rational numbers, instead fractional powers are Real numbers.
http://en.wikipedia.org/wiki/Real_number
The workaround is using a temporary float and then approximate to the nearest BigDecimal.

Related

Convert double to BigInteger [duplicate]

This question already has answers here:
is there anyway to convert from Double to BigInteger?
(3 answers)
Closed 5 years ago.
How do you convert a double to a BigInteger, as accurately as possible? BigInteger doesn't have a valueOf(double) method. It does have a valueOf(long) method, but converting via long, would destroy any value outside the 2^64 range, and in particular I'm trying to get large numbers like 1e100 to convert to their corresponding integer values to the sixteen or so significant figures allowed by floating point precision.
You can convert the double into a BigDecimal and then into a BigInteger:
BigInteger k = BigDecimal.valueOf(doublevalue).toBigInteger();
You could use BigDecimal.valueOf(double) and then call toBigIntegerExact() on that. Like,
BigInteger bi = BigDecimal.valueOf(Math.PI).toBigIntegerExact();

how big does an integer have to be that you need the big integer class to use a math function [duplicate]

This question already has answers here:
max value of integer
(11 answers)
Closed 7 years ago.
how big does an integer have to be that you need the big integer class to use a math function. Is there a specific rule that needs to be followed
It depends on the largest possible magnitude (size) of the result of the math function in your specific application as against the magnitude of an integer or a long. If you do not require absolute precision real or double may be your best choice - again this depends on your application requirements.

Java - 100 decimal places in variable [duplicate]

This question already has answers here:
How many significant digits do floats and doubles have in java?
(6 answers)
Closed 7 years ago.
I have a problem when it comes to calculating pi with more than 15 decimal places(i used double).
My result looks quite good except my variable is limited to 15 decimal places:
3.140592653839794
Anybody could tell me what i have to do if i want more decimal places?
Thanks and Greeting!
Use java.math.BigDecimal instead of double for arbitrary (finite) precision.

How to bring BigDecimal to fractional power? [duplicate]

This question already has answers here:
How to do a fractional power on BigDecimal in Java?
(3 answers)
Java's BigDecimal.power(BigDecimal exponent): Is there a Java library that does it? [closed]
(3 answers)
Closed 8 years ago.
BigDecimal was the choice to store numbers that have up to 5 digits after decimal point. I need to raise the BigDecimal to fractional power (up to 2 digits after decimal point).
For example I have to bring 9.09671 to power of 1.51. Do I need some conversions from/to BigDecimal? How to do it?
EDIT:
I cannot use 3rd party libraries as described in Java's BigDecimal.power(BigDecimal exponent): Is there a Java library that does it?
Is there more elegant, succint way for this case than described in How to do a fractional power on BigDecimal in Java?

Wrong double calculation [duplicate]

This question already has answers here:
How can I handle precision error with float in Java?
(9 answers)
Closed 8 years ago.
some calculations with doubles return the wrong result.
E.g.
System.out.println(""+(0.05+0.01));
output
0.060000000000000005
What can I do to correct this error?
Doubles are not made for precise calculations (see Round to 2 decimal places) - for precise calculations, use BigDecimal instead.

Categories

Resources