Precision of Double->Wrong calculations

  • Thread starter Thread starter Th.Krebs
  • Start date Start date
T

Th.Krebs

Hello all,

we just got a little problem with the correctness of VB.Net.

What would u expect as the result of
4.35 * 100.0

435.0 u think ?

well, VB.Net tells us, that the result of this
calculation will be:

4.35*100.0 434.99999999999994 Double

Who is right here, and how can we avoid these kind of miscalculations.
Using Decimal instead of Double?
Thanks for reading and answering this post.
Thomas
 
Th.Krebs said:
What would u expect as the result of
4.35 * 100.0

435.0 u think ?

well, VB.Net tells us, that the result of this
calculation will be:

4.35*100.0 434.99999999999994 Double

Who is right here, and how can we avoid these kind of miscalculations.
Using Decimal instead of Double?

For the reasons, see:

<URL:http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html>
<URL:http://www.math.grin.edu/~stone/courses/fundamentals/IEEE-reals.html>
<URL:http://support.microsoft.com/?scid=kb;[LN];42980>

MSDN on 'Decimal':

---
The 'Decimal' value type represents 'Decimal' numbers ranging from positive
79,228,162,514,264,337,593,543,950,335 to negative
79,228,162,514,264,337,593,543,950,335. The 'Decimal' value type is
appropriate for financial calculations requiring large numbers of
significant integral and fractional digits and no round-off errors.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top