Solved

How to set double precision?

Posted on 2008-06-21
5
6,987 Views
Last Modified: 2013-12-16
My question is fairly simple. I just write a code that multiples to doubles say 3.13 and 3.13 and as a result I get 9.7968999999999991 instead of 9.7969. Why do I get those unwanted extra digits as 0.000099999991 etc. These extra digits propagate errors in the following calculations.

Could you suggest a solution pls!
0
Comment
Question by:Lexiks
5 Comments
 

Expert Comment

by:jrshedden
ID: 21837310
Hi,
See if your can use BCD (binary coded decimal) types instead of doubles. Doubles calculate in radix 2 (binary) which doesn't always convert to/from radix 10 (decimal) exactly.

Jim
0
 
LVL 6

Expert Comment

by:psychic_zero
ID: 21837314
It's a system bug, you can use ToString function to set the decimal number like this
double number = 9.7968999999999991;
 

Console.WriteLine(number.ToString("#0.0000"));

Open in new window

0
 

Expert Comment

by:jrshedden
ID: 21837335
Hi,
I had a chance to look it up in the C# compiler. Have a look at "Decimal Structure" in the compiler help. The example should be helpful.
Jim
0
 
LVL 7

Accepted Solution

by:
photowhiz earned 125 total points
ID: 21839343
It is not a "bug", it is a fundamental property of the way computers represent fractions.  If you want to maintain a fixed number of decimal places, use the decimal type. Note that decimal types cannot represent fractions exactly either.

For more information, read http://docs.sun.com/source/806-3568/ncg_goldberg.html
0
 

Author Comment

by:Lexiks
ID: 21840097
Yeah photowhiz thats right. I myself kept searching and found the fact that you are saying. Ok thx all...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now