• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why does Jave incorrectly calculate?  RSS feed

 
Dean Janse Van Rensburg
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dean Janse Van Rensburg
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Henry,
Is it then impossible to do theses calculations with a computer?
 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
long is also limited in its range. For arbitrarily big numbers, use the java.math.BigInteger clas.
 
Dean Janse Van Rensburg
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you I'll look in to BigInteger thank you. Is there a limit to BigInteger ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Dean Janse Van Rensburg
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!