Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to set double precision?

Posted on 2008-06-21
5
Medium Priority
?
6,996 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 500 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

916 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