Solved

Double precision and iostream library

Posted on 1997-02-26
1
519 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What does std::atomic give me? 7 125
Which IDE to use to begin C++ training? 5 63
White board coding practice 3 88
Problem with SqlConnection 4 170
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 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.

803 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