GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Big Decimal why use it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Big Decimal why use it?" Watch "Big Decimal why use it?" New topic
Author

Big Decimal why use it?

Marco Vanoli
Ranch Hand

Joined: Jan 12, 2005
Posts: 99
Can somebody gimme some info about this Type (on java references i do not understand well what it is..)?

..or if you can give me just a link..

tnk bye


bye, <br />Marco
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Well, as you know, doubles and floats are a specific number of bits wide. That means you can only cram so much information into them. 4.2 * 12.09 is no problem. But how about 34998729873948792879387948793879234.019872987349871234908798734928734928734987 * 0.00000000000000000000000000000023498729384729834792834792837492837492834729843273928?

Well, you get the idea! (Use BigDecimal.)


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If you add, subtract, multiply, divide *money* BigDecimal will give you the answers you expect. Floats and Doubles will be full of unpleasant surprises.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343



"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Marco Vanoli
Ranch Hand

Joined: Jan 12, 2005
Posts: 99
Originally posted by marc weber:

the result is :

0.30000000000000004
0.3
Press any key to continue...

ok and now con you quickly explain why, when use them?
and there is maybe a tutorial that explain use of Bigint?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Marco Vanoli:
...con you quickly explain why, when use them? ...

Basically, if you're okay with small errors due to lack of precision -- like 0.30000000000000004 instead of 0.3 -- then doubles are fine. But whenever you need precise results, then use BigDecimal.
Originally posted by Marco Vanoli:
...there is maybe a tutorial that explain use of Bigint?

The API for BigInteger states...
BigInteger provides analogues to all of Java's primitive integer operators, and all relevant methods from java.lang.Math. Additionally, BigInteger provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.

[ July 23, 2005: Message edited by: marc weber ]
Marco Vanoli
Ranch Hand

Joined: Jan 12, 2005
Posts: 99
ok i found some intresting =)


class BigProblem2 {

public static void main(String[] args) {

// BigNums.java

System.out.println("Here's Long.MAX_VALUE: " + Long.MAX_VALUE);

BigInteger bInt = new BigInteger("3419229223372036854775807");

System.out.println("Here's a bigger number: " + bInt);

System.out.println("Here it is as a double: " + bInt.doubleValue( ));
}

}


import java.math.*;


Here's Long.MAX_VALUE: 9223372036854775807
Here's a bigger number: 3419229223372036854775807
Here it is as a double: 3.419229223372037E24
Press any key to continue...


tnks for reply marc
[ July 23, 2005: Message edited by: Marco Vanoli ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Marco Vanoli:
...Here's Long.MAX_VALUE: 9223372036854775807
Here's a bigger number: 3419229223372036854775807
Here it is as a double: 3.419229223372037E24...

Yeah, I recall using BigInteger for handling very large numbers when I was writing the "ultimate" factoring program. (Doesn't everyone write one of these?) My program used BigInteger until the remainder was "small" enough to use primitives, when it switched over to long. I never actually verified whether this switch was more efficient.
 
GeeCON Prague 2014
 
subject: Big Decimal why use it?