[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Double precision and iostream library

Posted on 1997-02-26
1
Medium Priority
?
537 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 200 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

865 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