https://rentry.org/PPP2_p384

// Code derived from Stroustrup's PPP2 book
// § 11.2.3 Floating-point output
//  -and beginning on p 384

#include <iostream>

using namespace std;

int main()
{
  cout << 1234.56789 << "\t\t(defaultfloat)\n"  // \t\t to line up columns
       << fixed << 1234.56789 << "\t(fixed)\n"
       << scientific << 1234.56789 << "\t(scientific)\n";

  //---
  cout << '\n';

  cout << 1234.56789 << '\t' << fixed << 1234.56789 << '\t'  //
       << scientific << 1234.56789 << '\n';
  cout << 1234.56789 << '\n';  // floating format “sticks”

  //---
  cout << '\n';

  cout << defaultfloat << 1234.56789 << '\t'  // the default format for
                                              // floating-point output
       << fixed << 1234.56789 << '\t'         //
       << scientific << 1234.56789 << '\n';
}

build & run:

g++ -std=c++20 -O2 -Wall -pedantic ./ch_11/main_p384.cpp && ./a.out

PrevUpNext

Edit
Pub: 09 Apr 2023 05:02 UTC
Edit: 03 May 2023 10:24 UTC
Views: 341