Not sure if I am doing something wrong but why do I get wrong results in the following code?
I tested in Eclipse Helios and Kepler and both return the same results. I also tested int, long and double also gave the same results. I don't get it.
Dean Janse Van Rensburg wrote:Not sure if I am doing something wrong but why do I get wrong results in the following code?
I tested in Eclipse Helios and Kepler and both return the same results. I also tested int, long and double also gave the same results. I don't get it.
You need to understand that the Java data types (or actually, any primitive data type, for any language) do not have infinite range, nor infinite precision. They have a range for where they are valid. They have a precision on how accurate they are based on operations. And they have defined behaviors on how they behave when you exceed the limitations of range and precision. And obviously, your example exceeds them.
Dean Janse Van Rensburg wrote:Thank you I'll look in to BigInteger thank you. Is there a limit to BigInteger ?
The purpose of the BigInteger class is to implement two-complement integers with a byte array of any size -- basically, simulate in software what the processor does in hardware. The advantage is that the integer can grow to a ridiculous size, whereas an primitive long is only eight bytes long. The disadvantage is speed -- hardware implementations are much much faster than software implementations.
So, to answer your question, I don't think that there is a design limit, but I guess there may be limits to the implementation. After all, big implementations of anything uses resources, and you can run out of them -- even memory...
You've been a great help thank you for your time Henry. Reading up on BigInteger and BigDecimal is definitely going in the right direction. Now I just need to figure out how to get the cube root and square root of a BigInteger or bigDecimal and the powers of them.