Using BigInteger and BigDecimal [WAS: Converting floats to Strings and back]  
Author Message
Oliver Wong





PostPosted: 2005-11-23 5:03:00 Top

java-programmer, Using BigInteger and BigDecimal [WAS: Converting floats to Strings and back]
"Alun Harford" <email***@***.com> wrote in message
news:dlvmk8$j8e$email***@***.com...
>
> Unless you want speed (ie. the arithmetic is hard and you've analysed the
> result of using floating-point to do it), or you want to demonstrate some
> of
> the nasty things that happen with floating point, use BigDecimal.
> a) It requires significantly less use of your brain, which generally
> reduces
> the number of bugs.
> b) Your users shouldn't have to think about the limitations of floating
> point without a very good reason.

I've experimented with using BigInteger instead of int in my code with
mixed results. Code that was integer-math intensive (e.g. finding prime
numbers) typically ran 5 to 7 times slower, which is pretty bad, but not
"noticeable" for typical user applications. I'll probably continue this
practice because for most of my apps, the bottleneck is not the
integer-math, and the extra flexibility is nice.

My question is: is there a "best" way to store BigInteger and BigDecimal
values in databases (particularly in SQL)? The two most obvious solutions to
me is to store them as BLOBs or as strings. I'd probably favor the latter,
because although it uses more storage space and processing time (e.g. to
parse the string back into a BigInteger value), it'll probably be easier to
inspect the DB to make sure all the values are correct during debugging.

- Oliver


 
Chris Uppal





PostPosted: 2005-11-23 18:59:00 Top

java-programmer >> Using BigInteger and BigDecimal [WAS: Converting floats to Strings and back] Oliver Wong wrote:

> My question is: is there a "best" way to store BigInteger and
> BigDecimal values in databases (particularly in SQL)? The two most
> obvious solutions to me is to store them as BLOBs or as strings. I'd
> probably favor the latter, because [...]

Also, other applications that read the data (not Java) will be able to work
with it. E.g. report generators.

Since one of the biggest (arguably the only) reason to keep data in SQL-style
databases is so that the data can be application-independent, it makes sense to
keep data in application-independent formats where possible.

-- chris