What is the biggest data type I can use to store numbers so I do not lose precision in JAVA?
2009-10-03 16:34:14 UTC
I am trying to do calculations on numbers in java and I am losing precision because the data type they are stored as are not big enough. What should I be saving them as?
Four answers:
Mark aka jack573
2009-10-07 08:59:59 UTC
There is a class called BigInteger in the java.math package.
you aren't losing precision because your data type isnt big enough. you are losing precision because you are saving a floating point number (3.14) to an int, which drops the .14 and makes it 3 ... hence loss of precision.
to keep from losing precision you should use floats or doubles
double double1 = 3.14;
float float1 = 3.14f;
int x = (int)double1;
(x now equals 3)
anytime that you will have to be conscious of numbers after a decimal, use a double. otherwise use an int, because it looks cleaner when you print it out...
hope i helped
?
2016-11-05 12:51:22 UTC
the two a form of solutions are good. you additionally can keep them in an ArrayList, which shops objecs, Then explictly solid them once you examine them out ArrayList al = new ArrayList(); enable's say which you keep String call, and int score for each participant. participant 0's call is in loc 0, score in loc a million; participant a million is in 2 and 3, and so on Then String aName = (String) al.get(0); int aScore = (int) al.get(a million); and so on yet on your application i think of that it may be basically right to apply a participant classification with fields call and score. Then ArrayList636da1d35e805b0eae0fcd8333f9234 al = new ArrayList636da1d35e805b0eae0fcd8333f9234(); Then al.get(0); returns the article for participant 0.
sheldonlinker
2009-10-03 16:39:58 UTC
As native types, long is a 64-bit signed integer, and double is IEEE 64-bit floating-point format. Bigger than that, and you'll have to see if someone has written a class appropriate to your needs.
Other languages support arbitrarily precise numbers, for instance FIXED BINARY(n), FLOAT BINARY(n), or FLOAT DECIMAL(n) in PL/1 (where n is a number) or mobybignum in Lisp.
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.