2

Here's is an example:

std::cout << 1000000.0 << "\n";
std::cout << std::fixed << 1000000.0 << "\n";
std::cout << std::fixed << std::noshowpoint << 1000000.0 << "\n";

It will print me this:

1e+06
1000000.000000
1000000.000000

But I want this:

1000000

How I can do it with IO manipulators without setprecision()? Why without? Because I want to print:

std::cout << 1000000.0 << " " << 1111111.1 << " " << 1234567.89;

and get:

1000000 1111111.1 1234567.89

without calculating precision for every number

Givikap120
  • 71
  • 6
  • you can use printf function for it. Just need to use appropriate format specifiers. – Ankit Kumar Maurya Jun 18 '22 at 16:59
  • The last two numbers in your example are not representable in binary exactly, so how would the program know how many decimal digits to print out if you do not specify the precision manually? – Alex Sveshnikov Jun 18 '22 at 17:02
  • `std::cout << static_cast(1000000.0) << '\n';` – Ted Lyngmo Jun 18 '22 at 17:04
  • @AlexSveshnikov so how does `cout` know what to print if I do this `cout<<12.34`? Stupid comment IMO. – Givikap120 Jun 18 '22 at 17:05
  • @Givikap120 Please keep it civil. – Ted Lyngmo Jun 18 '22 at 17:06
  • @Givikap120 ok, then what do you want to be printed in case of 12.34001? – Alex Sveshnikov Jun 18 '22 at 17:12
  • @AlexSveshnikov 12.34 – Givikap120 Jun 18 '22 at 17:15
  • Why 12.34? And what in case of 12.341? Do I understand you correctly that you want to keep only *at most* 2 digits after the point, but show only 1 or even 0 of them if these digits are zeros? – Alex Sveshnikov Jun 18 '22 at 17:20
  • @AlexSveshnikov in the case of 12.341 it would print 12.341. I want to use the default "general" format but without using the scientific format in case of large numbers. – Givikap120 Jun 18 '22 at 17:26
  • `std::cout << std::fixed << std::setprecision(0) << 1000000.0 << "\n";` – Eljay Jun 18 '22 at 17:27
  • Welcome to ugly land. This just isn't something floating-point does well because [the number doesn't necessarily end exactly where you think it does](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) and telepathic computers haven't been invented yet. – user4581301 Jun 18 '22 at 17:34
  • @Givikap120 Your question is still not not clear enough. Try `cout << 1.001` and then `cout << 1000.001`. First time I get printed `1.001`, but the second time - just `1000`. So the precision is changing even without going to scientific format. – Alex Sveshnikov Jun 18 '22 at 17:35
  • @AlexSveshnikov ok i already figured out what scientific is used when digits count is higher than precision, so in my case I need this `std::cout << setprecision(16) << 1000000.0 << " " << 1111111.1 << " " << 1234567.89;` because 16 is the highest precision what is not adding wrong numbers to out – Givikap120 Jun 18 '22 at 17:44
  • You will find cases where that fails as well, unfortunately. – user4581301 Jun 18 '22 at 17:49
  • 1
    The `setprecision(16)` solution contradicts your answer to my question what should be printed in case of `12.34001`. You answered `12.34`, but now it will print `12.34001`. – Alex Sveshnikov Jun 18 '22 at 17:51
  • @user4581301 this is obvious because computer math isn't perfect. At least it works for the absolute majority of double numbers. When you use a 16-digit number it will most likely fail in dozens of other situations because this is too close to overflowing. – Givikap120 Jun 18 '22 at 17:54
  • @AlexSveshnikov this is because I thought that scientific formatting is used for human-readable output, but now I know what this is the consequence of the lack of digits to represent numbers in this precision. My logic was "the same output for numbers < 1000000 and non-scientific for numbers >= 1000000". I am sorry for this. – Givikap120 Jun 18 '22 at 17:59
  • @Givikap120 ok, just 1 more thing - the setprecision(16) does not guarantee that you will never get the scientific output. For example, with setprecision(16) the cout << 0.00001 still produces 1e-05. So, basically the root of your problems lies in my first comment :) – Alex Sveshnikov Jun 18 '22 at 18:04
  • A pedantic clarification: This isn't a math problem. It is a representation problem. Sometimes the distinction is important. – user4581301 Jun 18 '22 at 18:04
  • @AlexSveshnikov ok that is a new problem: how to turn off the scientific format now for a very small numbers? – Givikap120 Jun 18 '22 at 18:10
  • @Givikap120 what you *probably* want is std::fixed, but strip out the ending zeros. Only you can decide if it is what you want or not. – Alex Sveshnikov Jun 18 '22 at 18:13
  • `std::cout << std::fixed << std::setprecision(1) << 1000000.3 << "\n";` – Eljay Jun 18 '22 at 20:29

1 Answers1

1

The scientific output is used when the digits count of the number is higher than precision. For example, the default precision is equal to 6, but the number 1234567 has 7 digits, so it will be printed as 1.23457+e06, cutting off the last digit. So to get 1234567 printed normally we need at least precision = 7. The highest precision that is not adding the "wrong" numbers is 15, so you need to use std::setprecision(15).

code: std::cout << std::setprecision(15) << 1000000.0 << " " << 1111111.1 << " " << 1234567.89;

out: 1000000 1111111.1 1234567.89

Givikap120
  • 71
  • 6