1

I have been learning CPP lately, and I came across this question:

Consider the below program:

#include <iostream>
using namespace std;
class Hall
{
 public:
 double cost;
};
int main()
{
 Hall hall;
 hall.cost=10000.50;
 cout<<hall.cost;
 return 0;
}
What will be the output of above coding? 

The answer to this question is 10000.5, however, I couldn't understand why is it so ? In my opinion, answer should be either 10000.500000 as double has precision upto 6 decimal digits or it should print 10000.50 as it is the value assigned to hall.cost.

Please explain.

giri
  • 11
  • 1
  • 1
    "double has precision upto 6 decimal digits" who the deuce told you that? `10000.5` is one of the few numbers that can be represented exactly as a `double`. – Bathsheba Mar 19 '21 at 17:22
  • But why is it that 10000.5 is one of the few numbers that can be represented exactly as a double? @Bathsheba – giri Mar 19 '21 at 17:26
  • What is the concept behind it? – giri Mar 19 '21 at 17:26
  • "10000.5 is one of the few numbers that can be represented exactly as a double" --> there are about 2^64 such values. Still a drop in the infinity bucket. – chux - Reinstate Monica Mar 19 '21 at 17:26
  • This is a good starting point: https://en.wikipedia.org/wiki/IEEE_754. It's remarkably clever. – Bathsheba Mar 19 '21 at 17:27
  • 1
    `double` represent _exactly_ values in the form of sign*53-bit integer*2^expo. expo about +/-1024. – chux - Reinstate Monica Mar 19 '21 at 17:28
  • so @Bathsheba I didn't get your point, what relation does expressing 100000.5 in IEEE-754 form have with precision of double (sorry I am a novice) – giri Mar 19 '21 at 17:28
  • Subject to other limiting factors (such as the number of bits in the type), only dyadic rationals can be represented exactly in floating point. And 10000.5 is a dyadic rational. Actually @chux-ReinstateMonica puts it better with his formula. – Bathsheba Mar 19 '21 at 17:28
  • Once you've read the article, you'll realise that you can round trip a 15 significant figure decimal number via a double. See https://stackoverflow.com/questions/59120620/how-are-flt-dig-dbl-dig-and-ldbl-dig-determined-in-c – Bathsheba Mar 19 '21 at 17:29
  • On a sidenote, I tried executing this program and got 10000.2 as the answer. Can you explain? @Bathsheba https://ide.codingblocks.com/s/443538 – giri Mar 19 '21 at 17:33
  • It's not about the precision of `double`, it's how you print it. `10000.5`, `10000.50` and `10000.50000` are equal and they have the same binary representation, so how should it be printed? `std::cout << ...` will not print unnecessary zeros unless explicitly told to. – Lukas-T Mar 19 '21 at 17:33
  • Isn't x in this case a dyadic rational? – giri Mar 19 '21 at 17:34
  • https://ide.codingblocks.com/s/443538 Please explain this @churill – giri Mar 19 '21 at 17:36
  • @giri: On your "explain this", `cout` has a default 6 significant figures for output. Silly old C++! – Bathsheba Mar 19 '21 at 17:38
  • So, you agree with 10000.2 or 10000.25 @Bathsheba – giri Mar 19 '21 at 17:40
  • The number is exactly 10000.25 (also a dyadic rational), but the default formatting for std::cout will output 10000.2. The duplicate explains how to widen the output. – Bathsheba Mar 19 '21 at 17:42
  • If you need to work with the actual maximum precision of double (in decimal digits), you can use std::numeric_limits ([example](https://stackoverflow.com/a/50970282/4641116)) to programmatically access that value. – Eljay Mar 19 '21 at 18:00
  • "In my opinion, answer should be either 10000.500000 as double has precision upto 6 decimal digits or it should print 10000.50" . `double` stores a _value_. 10000.5, 10000.500000, 10000.50 have the same value. A `double` does not retain information about the precession of the value assigned to it. A `double` can be thought of as having a fixed 53 _binary_ digits of precision. That is like about 15-17 significant decimal digits. – chux - Reinstate Monica Mar 19 '21 at 20:35

0 Answers0