When bigdecimal and when just double ??

When bigdecimal and when just double ??

Post by etantoni » Fri, 20 Feb 2009 20:14:44

I need to do simple multiply,
for example 58 multiply by a double 1,4536366 .
The result is different respect to what I've with windows calculator.
Using BigDecimal the program becames too slow so I need to know when I
could use just double to make a multiply, or a add , or a subtract, or
a divide.


When bigdecimal and when just double ??

Post by John B. Ma » Fri, 20 Feb 2009 20:37:26

In article


Why don't you write a short example showing the result you get and the
result you expected:

< http://www.yqcomputer.com/ ;

Here's a helpful article on the topic:

< http://www.yqcomputer.com/ ;

John B. Matthews
trashgod at gmail dot com
< http://www.yqcomputer.com/ ;


When bigdecimal and when just double ??

Post by Patricia S » Sat, 21 Feb 2009 00:39:05


What is the purpose of the multiply? The choice of BigDecimal vs. double
depends on big picture issues.

For example, in some contexts exactness, or failing that controlled
rounding, is particularly important for numbers that have exact decimal
representations. Think about accounting. That is where BigDecimal is
really needed.

Double is a binary fraction system, so decimal fractions have no special
status. 1.4536366 can only be approximated in double, not represented
exactly. However, numbers can be approximated to over 15 decimal digits,
good enough for a lot of engineering and scientific calculations. It is
a very compact representation, supported in hardware on many processors,
making it extremely efficient.


When bigdecimal and when just double ??

Post by Patricia S » Sat, 21 Feb 2009 05:31:21


It is certainly not "in error" because it is acting in accordance with
its published behavior: "Extended Precision, a feature of Calculator,
means that all operations are accurate to at least 32 digits. Calculator
also stores rational numbers as fractions to retain accuracy. For
example, 1/3 is stored as 1/3, rather than .333." (Windows XP Calculator
Help - calculations - extended precision).

It is no more in error than is BigDecimal for producing a different
answer from IEEE 754 64 bit binary. Both are supposed, according to
their specifications, to produce the infinitely precise result for that
case, and both do so.

Computer arithmetic often involves trade-offs between various desirable
properties, such as range, accuracy for particular cases, automatic
scaling, performance, and compactness. Two programs with different
criteria, and different specifications, can produce different results
for the same calculation without either of them being "in error".


When bigdecimal and when just double ??

Post by Roedy Gree » Sun, 22 Feb 2009 11:24:10

On Thu, 19 Feb 2009 03:14:44 -0800 (PST), etantonio
< XXXX@XXXXX.COM > wrote, quoted or indirectly quoted someone
who said :

If you want a precise answer use int or long.

If the answer is too fat to fit in a long use BigInteger or BigDecimal

If the answer is a measurement of a physical quantity, use a double or
float depending on the accuracy required. It equivalent to scientific
notation, allowing very large or very small numbers with only about 15
significant digits.

See http://www.yqcomputer.com/
various types.

in your particular case 58 * 1,4536366 = 843,109,228

The biggest possible int is 2,147,483,647, so you can handle this with
ordinary ints.

Roedy Green Canadian Mind Products

One path leads to despair and utter hopelessness. The other,
to total extinction. Let us pray we have the wisdom to choose correctly.
~ Woody Allen .

When bigdecimal and when just double ??

Post by Lew » Sun, 22 Feb 2009 11:41:36

etantonio wrote, quoted or indirectly quoted someone

Given the ".it" suffix on the email address, it is likely that the OP was
using the comma as the radix point, making the answer slightly larger than 84.
It is highly unlikely that the answer would have diverged from that given by
the Windows calculator otherwise.


When bigdecimal and when just double ??

Post by aaron.jame » Mon, 02 Mar 2009 16:32:19

Actually Java only complies to its own standards, and not IEEE
Read: www.cs.berkeley.edu/~wkahan/JAVAhurt.pdf

Also the JVM is free to implement floating point differently on
different platforms using hardware that may not be available on all
cpus to optimize floating point operations. The only way to get
completely consistent results on ALL platforms is to use the strictfp
modifier on the class or method.