Forums Register Login

Why does Jave incorrectly calculate?

+Pie Number of slices to send: Send
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.



Results
+Pie Number of slices to send: Send
 

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.

Henry
+Pie Number of slices to send: Send
Thank you Henry,
Is it then impossible to do theses calculations with a computer?
+Pie Number of slices to send: Send
 

Dean Janse Van Rensburg wrote:Thank you Henry,
Is it then impossible to do theses calculations with a computer?


You have to use long date type for big numbers int is limited between about -2 billion and about +2 billion
+Pie Number of slices to send: Send
long is also limited in its range. For arbitrarily big numbers, use the java.math.BigInteger clas.
+Pie Number of slices to send: Send
Thank you I'll look in to BigInteger thank you. Is there a limit to BigInteger ?
+Pie Number of slices to send: Send
 

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...

Henry

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
power shouldn't be hard...assuming you have an integer-type for the exponent. Read the API on the BigInteger class carefully.


square/cube root might be a little harder.
Could you hold this puppy for a sec? I need to adjust this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 885 times.
Similar Threads
for loop demo
Dsplaying the alphabet with a loop?
HELP~~
Math.Random() query
toUpper, toLower for chars
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:58:36.