Solved

Double precision and iostream library

Posted on 1997-02-26
1
520 Views
Last Modified: 2008-02-26
Hi,

the following problem:

I have a double precision number,
double x = -9.3456

Then I try to output it using the following commands:
cout.setf(ios::fixed,ios::floatfield);
cout.precision(16);
cout << x;

...and this is the result:

-9.3455999999999992

What I need to know is:

which is the number actualy stored in memory ?
what happens ?

Thanks for any help.

Roberto

0
Comment
Question by:roberto022697
1 Comment
 

Accepted Solution

by:
jordan122196 earned 100 total points
ID: 1162124
The actual number stored in memory is:
-9.3455999999999992

This is the closest number to -9.3456 that can be stored
in the bits of a double.  Numbers are stored in base 2 (one's
and zeros).  Thus you can exactly store the numbers: 8,4,0.5,
0.25, etc. but a number like 0.1 cannot be exactly stored.

In computer science we accept this small error ( called floating
point error ) as a fact of life.

jordan

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

820 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