Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# dividing floating point numbers?

Greenhorn
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?
Tom

Ranch Hand
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 Clement
Greenhorn
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
1.2721571e-7
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

 It is sorta covered in the JavaRanch Style Guide.