Solved

Double precision and iostream library

Posted on 1997-02-26
1
516 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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 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

10 Experts available now in Live!

Get 1:1 Help Now