• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why does Jave incorrectly calculate?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Henry,
Is it then impossible to do theses calculations with a computer?
 
Ranch Hand
Posts: 202
Android PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you I'll look in to BigInteger thank you. Is there a limit to BigInteger ?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic