File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes dividing floating point numbers? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "dividing floating point numbers?" Watch "dividing floating point numbers?" New topic

dividing floating point numbers?

Tom Clement

Joined: Mar 21, 2004
Posts: 26
I've made it over many hurdles on this one, but since it's math
intensive and I'm a graphical interface person, I'm having to bone up
on my math.
Let's say I'm calculating a negative exponent by dividing with BigDecimal.
for(int i = 1; i > power; i--)
test= test.divide(Dholder,5000,BigDecimal.ROUND_HALF_UP);
This works fine, as long as there is no floating point on the power.
But in some cases the power is something like, 223.0754233
I've tried separating the two, and using the right side as a percentage.
e,g, (newPower = Dholder * rightSide, where Dholder is the base of the equation, then dividing one last time by the newPower)
But when I test the result against a calculator, it's still not right.
Although, it's close. Anybody know the correct formula for this?
Thank you in advance,
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
For division, you could divide the mantissas, then just subtract the two exponents. For Multiplication, multiply the mantissas, then add the exponents.
Tom Blough

Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
Tom Clement

Joined: Mar 21, 2004
Posts: 26
Ok, concrete example:
power = -5.300000190734863
base = 20
20 ^-5 = 3.125000E-7 using BigDecimal divide().
How do I calculate the .300000190734863
to arrive at correct answer of
I'm using BigDecimal because of the size of some of the calculations.
Oh, and I've tested the program on whole numbers and it works fine when tested against the calculator, it's the handling of the fractional part that's killing me.
Thank you all, very much...... Tom
I agree. Here's the link:
subject: dividing floating point numbers?
It's not a secret anymore!