55
CSE202: Lecture 8 The Ohio State University 1 Formatting Numbers for Output

CSE202: Lecture 8The Ohio State University1 Formatting Numbers for Output

Embed Size (px)

Citation preview

CSE202: Lecture 8 The Ohio State University 1

Formatting Numbers for Output

CSE202: Lecture 8 The Ohio State University 2

Formatting Numbers for Output

• Number formatters are to be used in conjunction with cout

• For example,

double x = 6;cout << x << endl;

• The above outputs 6, but x is declared a floating point number, so why isn’t 6.0000… displayed?

CSE202: Lecture 8 The Ohio State University 3

Formatting Numbers for Output (2)

• The decimal point is truncated for display, so we must force it to be shown. Observe the following code:

double x = 6;cout.setf(ios::fixed);cout << x << endl;

• Notice the cout.setf(ios::fixed) command. It says “force cout to display the decimal point and 6 significant digits afterward.”

• Now 6.000000 will be displayed.

CSE202: Lecture 8 The Ohio State University 4

outputFormat1// format output#include <iostream>using namespace std;

int main(){ double x; cout << "Enter x: "; cin >> x;

cout << "x = " << x << endl; // general format, default cout.setf(ios::fixed); // use fixed decimal notation cout << "x = " << x << endl; cout.unsetf(ios::fixed); // unset fixed decimal notation flag cout.setf(ios::scientific); // use scientific notation cout << "x = " << x << endl; return 0;}

CSE202: Lecture 8 The Ohio State University 5

> outputFormat1.exeEnter x: 123.45x = 123.45x = 123.450000x = 1.234500e+02

> outputFormat1.exeEnter x: 1e15x = 1e+15x = 1000000000000000.000000x = 1.000000e+15

cout << "x = " << x << endl; // general format, default

cout.setf(ios::fixed); // use fixed decimal notation

cout << "x = " << x << endl;

cout.unsetf(ios::fixed); // unset fixed decimal notation flag

cout.setf(ios::scientific); // use scientific notation

cout << "x = " << x << endl;

CSE202: Lecture 8 The Ohio State University 6

> outputFormat1.exe

Enter x: 1e-15

x = 1e-15

x = 0.000000

x = 1.000000e-15

>

cout << "x = " << x << endl; // general format, default

cout.setf(ios::fixed); // use fixed decimal notation

cout << "x = " << x << endl;

cout.unsetf(ios::fixed); // unset fixed decimal notation flag

cout.setf(ios::scientific); // use scientific notation

cout << "x = " << x << endl;

CSE202: Lecture 8 The Ohio State University 7

Setting Precision (1)

• Remember that fixed shows 6 decimal places by default. But…– if our program dealt with printing out dollar

amounts, do we really need to show 6 decimal places?

– Another example: if our program dealt with some rigorous scientific simulation, is 6 decimal places enough?

CSE202: Lecture 8 The Ohio State University 8

Setting Precision (2)

• To set the precision, we use cout.precision(n) formatter, where n is the number of decimal places to be displayed.

double x = 6;cout.precision(3);cout.setf(ios::fixed);cout << x << endl;

• Now 6.000 is displayed.

CSE202: Lecture 8 The Ohio State University 9

examplePrecision// example of setting precision…int main(){ cout.setf(ios::fixed); cout << "pi/100 = " << M_PI/100.0 << endl;

cout.precision(3); cout << "pi/100 = " << M_PI/100.0 << endl;

cout.precision(12); cout << "pi/100 = " << M_PI/100.0 << endl;

return 0;}

CSE202: Lecture 8 The Ohio State University 10

> examplePrecision.exepi/100 = 0.031416pi/100 = 0.031pi/100 = 0.031415926536

>

… cout.setf(ios::fixed); cout << "pi/100 = " << M_PI/100.0 << endl;

cout.precision(3); cout << "pi/100 = " << M_PI/100.0 << endl;

cout.precision(12); cout << "pi/100 = " << M_PI/100.0 << endl;…

CSE202: Lecture 8 The Ohio State University 11

examplePrecision2// example of setting precision…int main(){ cout.setf(ios::scientific); cout << "pi/100 = " << M_PI/100.0 << endl;

cout.precision(3); cout << "pi/100 = " << M_PI/100.0 << endl;

cout.precision(12); cout << "pi/100 = " << M_PI/100.0 << endl;

return 0;}

CSE202: Lecture 8 The Ohio State University 12

> examplePrecision2.exepi/100 = 3.141593e-02pi/100 = 3.142e-02pi/100 = 3.141592653590e-02

>

… cout.setf(ios::scientific); cout << "pi/100 = " << M_PI/100.0 << endl;

cout.precision(3); cout << "pi/100 = " << M_PI/100.0 << endl;

cout.precision(12); cout << "pi/100 = " << M_PI/100.0 << endl;…

CSE202: Lecture 8 The Ohio State University 13

examplePrecision3// example of setting precision…int main(){ cout.precision(30);

cout.setf(ios::fixed); cout << "pi/100 = " << M_PI/100.0 << endl; return 0;}

> examplePrecision3.exepi/100 = 0.031415926535897934

>

CSE202: Lecture 8 The Ohio State University 14

examplePrecision4// example of setting precision…int main(){ cout.precision(30);

cout.setf(ios::scientific); cout << "pi/100 = " << M_PI/100.0 << endl; return 0;}

> examplePrecision4.exepi/100 = 3.141592653589793394e-02

>

CSE202: Lecture 8 The Ohio State University 15

Double Precision

• IEEE Standard for Floating-Point Arithmetic:

Double precision numbers have 16 significant digits.

CSE202: Lecture 8 The Ohio State University 16

outputFormat2// format output#include <iostream>using namespace std;

int main(){ double x;

cout << "Enter x: "; cin >> x;

cout.precision(12); cout << "x = " << x << endl; // general format, default cout.setf(ios::fixed); // use fixed decimal notation cout << "x = " << x << endl; cout.unsetf(ios::fixed); // unset fixed decimal notation flag cout.setf(ios::scientific); // use scientific notation cout << "x = " << x << endl; return 0;}

CSE202: Lecture 8 The Ohio State University 17

> outputFormat2.exeEnter x: 123.45x = 123.45x = 123.450000000000x = 1.234500000000e+02

> outputFormat2.exeEnter x: 1e15x = 1e+15x = 1000000000000000.000000000000x = 1.000000000000e+15

cout.precision(12);

cout << "x = " << x << endl; // general format, default

cout.setf(ios::fixed); // use fixed decimal notation

cout << "x = " << x << endl;

cout.unsetf(ios::fixed); // unset fixed decimal notation flag

cout.setf(ios::scientific); // use scientific notation

cout << "x = " << x << endl;

CSE202: Lecture 8 The Ohio State University 18

> outputFormat2.exeEnter x: 123456789e-17x = 1.23456789e-09x = 0.000000001235x = 1.234567890000e-09

>

cout.precision(12);

cout << "x = " << x << endl; // general format, default

cout.setf(ios::fixed); // use fixed decimal notation

cout << "x = " << x << endl;

cout.unsetf(ios::fixed); // unset fixed decimal notation flag

cout.setf(ios::scientific); // use scientific notation

cout << "x = " << x << endl;

CSE202: Lecture 8 The Ohio State University 19

Fixed vs. Scientific• cout.precision(12);cout.setf(ios::fixed);cout << “x = “ << x << endl;

Outputs 12 digits after the decimal place.

• cout.precision(12);cout.setf(ios::scientific);cout << “x = “ << x << endl;

Outputs 13 significant digits.

CSE202: Lecture 8 The Ohio State University 20

Setting Output Width

tempTable

CSE202: Lecture 8 The Ohio State University 21

> tempTable.exefahrenheit celsius kelvin -40 -40.00 233.15 -20 -28.89 244.26 0 -17.78 255.37 20 -6.67 266.48 40 4.44 277.59 60 15.56 288.71>

CSE202: Lecture 8 The Ohio State University 22

tempTableBad.cpp// Output with no formatting#include <iostream>using namespace std;

int main(){ cout << "fahrenheit celsius kelvin" << endl; for (int fahrenheit = -40; fahrenheit <= 60; fahrenheit += 20) { double celsius = (fahrenheit - 32.0) * (5.0/9.0); double kelvin = celsius + 273.15;

cout << " " << fahrenheit << " " << celsius << " " << kelvin << endl; }

return 0;}

tempTableBad

CSE202: Lecture 8 The Ohio State University 23

> tempTableBad.exefahrenheit celsius kelvin -40 -40 233.15 -20 -28.8889 244.261 0 -17.7778 255.372 20 -6.66667 266.483 40 4.44444 277.594 60 15.5556 288.706 >

cout << "fahrenheit celsius kelvin" << endl; for (int fahrenheit = -40; fahrenheit <= 60; fahrenheit += 20) { double celsius = (fahrenheit - 32.0) * (5.0/9.0); double kelvin = celsius + 273.15;

cout << " " << fahrenheit << " " << celsius << " " << kelvin << endl; }

CSE202: Lecture 8 The Ohio State University 24

tempTableBad2.cpp. . .int main(){ cout.setf(ios::fixed); cout.precision(2);

cout << "fahrenheit celsius kelvin" << endl; for (int fahrenheit = -40; fahrenheit <= 60; fahrenheit += 20) { double celsius = (fahrenheit - 32.0) * (5.0/9.0); double kelvin = celsius + 273.15;

cout << " " << fahrenheit << " " << celsius << " " << kelvin << endl; }

return 0;}

tempTableBad2

CSE202: Lecture 8 The Ohio State University 25

> tempTableBad2.exefahrenheit celsius kelvin -40 -40.00 233.15 -20 -28.89 244.26 0 -17.78 255.37 20 -6.67 266.48 40 4.44 277.59 60 15.56 288.71>

cout << "fahrenheit celsius kelvin" << endl; for (int fahrenheit = -40; fahrenheit <= 60; fahrenheit += 20) { double celsius = (fahrenheit - 32.0) * (5.0/9.0); double kelvin = celsius + 273.15;

cout << " " << fahrenheit << " " << celsius << " " << kelvin << endl; }

Width Problem

CSE202: Lecture 8 The Ohio State University 26

> tempTableBad2.exefahrenheit celsius kelvin -40 -40.00 233.15 -20 -28.89 244.26 0 -17.78 255.37 20 -6.67 266.48 40 4.44 277.59 60 15.56 288.71>

cout << " " << fahrenheit << " " << celsius << " " << kelvin << endl;

Problem: Need to fix the width of each output field.

CSE202: Lecture 8 The Ohio State University 27

#include <iomanip>

• Output can also be format using routines in iomanip.

• To use of these functions, include this file in your program header:#include <iomanip>

CSE202: Lecture 8 The Ohio State University 28

setw• The formatter setw(n) sets the width of the output to at least n

characters. String/value is right justified.• Example:

cout << setw(10)<< “Age” << setw(10) << “Weight” << endl;cout << setw(10) << 4 << setw(10) << 32 << endl;cout << setw(10) << 22 << setw(10) << 130.5 << endl;cout << setw(10) << 101 << setw(10) << 120 << endl;

• Output is: Age Weight 4 32 22 130.5 101 120

CSE202: Lecture 8 The Ohio State University 29

tempTable.cpp#include <iostream>#include <iomanip> // Note: Must include iomanip...int main(){ cout.setf(ios::fixed); cout.precision(2);

cout << "fahrenheit celsius kelvin" << endl; for (int fahrenheit = -40; fahrenheit <= 60; fahrenheit += 20) { double celsius = (fahrenheit - 32.0) * (5.0/9.0); double kelvin = celsius + 273.15;

cout << setw(6) << fahrenheit << setw(15) << celsius << setw(10) << kelvin << endl; }...

tempTable

CSE202: Lecture 8 The Ohio State University 30

> tempTable.exefahrenheit celsius kelvin -40 -40.00 233.15 -20 -28.89 244.26 0 -17.78 255.37 20 -6.67 266.48 40 4.44 277.59 60 15.56 288.71>

cout << setw(6) << fahrenheit << setw(15) << celsius << setw(10) << kelvin << endl;

trigTable

CSE202: Lecture 8 The Ohio State University 31

> trigTable.exe degrees radians sin cos -180 -3.14 -0.00 -1.00 -150 -2.62 -0.50 -0.87 -120 -2.09 -0.87 -0.50 -90 -1.57 -1.00 0.00 -60 -1.05 -0.87 0.50 -30 -0.52 -0.50 0.87 0 0.00 0.00 1.00 30 0.52 0.50 0.87 60 1.05 0.87 0.50 90 1.57 1.00 0.00 120 2.09 0.87 -0.50 150 2.62 0.50 -0.87 180 3.14 0.00 -1.00>

CSE202: Lecture 8 The Ohio State University 32

trigTable.cpp...#include <iomanip> // NOTE: Must include iomanip... cout.setf(ios::fixed); cout.precision(2);

cout << setw(8) << "degrees" << setw(8) << "radians" << setw(8) << "sin" << setw(8) << "cos" << endl;

for (int degrees = -180; degrees <= 180; degrees += 30) { double radians = degrees * M_PI/180.0;

cout << setw(8) << degrees << setw(8) << radians << setw(8) << sin(radians) << setw(8) << cos(radians) << endl; }...

trigTable

CSE202: Lecture 8 The Ohio State University 33

> trigTable.exe degrees radians sin cos -180 -3.14 -0.00 -1.00 -150 -2.62 -0.50 -0.87 -120 -2.09 -0.87 -0.50 -90 -1.57 -1.00 0.00 -60 -1.05 -0.87 0.50 -30 -0.52 -0.50 0.87 0 0.00 0.00 1.00 30 0.52 0.50 0.87 60 1.05 0.87 0.50 90 1.57 1.00 0.00 120 2.09 0.87 -0.50 150 2.62 0.50 -0.87 180 3.14 0.00 -1.00>

cout << setw(8) << "degrees" << setw(8) << "radians" << setw(8) << "sin" << setw(8) << "cos" << endl;

expTable

CSE202: Lecture 8 The Ohio State University 34

> expTable.exeEnter number of values: 10 x e^x 2^x 1 2.72 2.00 2 7.39 4.00 3 20.09 8.00 4 54.60 16.00 5 148.41 32.00 6 403.43 64.00 7 1096.63 128.00 8 2980.96 256.00 9 8103.08 512.00 10 22026.47 1024.00>

CSE202: Lecture 8 The Ohio State University 35

expTable.cpp...#include <iomanip> // NOTE: Must include iomanip... cout << "Enter number of values: "; cin >> n;

cout.setf(ios::fixed); cout.precision(2);

cout << setw(5) << "x" << setw(10) << "e^x" << setw(10) << "2^x" << endl;

for (int i = 1; i <= n; i++) { double x = i; cout << setw(5) << i << setw(10) << exp(x) << setw(10) << pow(2.0,x) << endl; }...

expTable

CSE202: Lecture 8 The Ohio State University 36

> expTable.exeEnter number of values: 10 x e^x 2^x 1 2.72 2.00 2 7.39 4.00 3 20.09 8.00 4 54.60 16.00 5 148.41 32.00 6 403.43 64.00 7 1096.63 128.00 8 2980.96 256.00 9 8103.08 512.00 10 22026.47 1024.00>

cout << setw(5) << i << setw(10) << exp(x) << setw(10) << pow(2.0,x) << endl;

expTable

CSE202: Lecture 8 The Ohio State University 37

> expTable.exeEnter number of values: 15 x e^x 2^x 1 2.72 2.00 2 7.39 4.00... 11 59874.14 2048.00 12 162754.79 4096.00 13 442413.39 8192.00 141202604.28 16384.00 153269017.37 32768.00>

cout << setw(5) << i << setw(10) << exp(x) << setw(10) << pow(2.0,x) << endl;

expTable

CSE202: Lecture 8 The Ohio State University 38

> expTable.exeEnter number of values: 25 x e^x 2^x 1 2.72 2.00 2 7.39 4.00... 13 442413.39 8192.00 141202604.28 16384.00 153269017.37 32768.00... 239744803446.258388608.00 2426489122129.8416777216.00 2572004899337.3933554432.00 >

cout << setw(5) << i << setw(10) << exp(x) << setw(10) << pow(2.0,x) << endl;

CSE202: Lecture 8 The Ohio State University 39

expTable2.cpp... cout << "Enter number of values: "; cin >> n;

cout.setf(ios::fixed); cout.precision(2);

cout << setw(5) << "x" << " " << setw(10) << "e^x" << " " << setw(10) << "2^x" << endl;

for (int i = 1; i <= n; i++) { double x = i; // Add spaces between fields. cout << setw(5) << i << " " << setw(10) << exp(x) << " " << setw(10) << pow(2.0,x) << endl; }...

expTable2

CSE202: Lecture 8 The Ohio State University 40

> expTable2.exeEnter number of values: 10 x e^x 2^x 1 2.72 2.00 2 7.39 4.00 3 20.09 8.00 4 54.60 16.00 5 148.41 32.00 6 403.43 64.00 7 1096.63 128.00 8 2980.96 256.00 9 8103.08 512.00 10 22026.47 1024.00>

// Add spaces between fields. cout << setw(5) << i << " " << setw(10) << exp(x) << " " << setw(10) << pow(2.0,x) << endl;

expTable2

CSE202: Lecture 8 The Ohio State University 41

> expTable2.exeEnter number of values: 15 x e^x 2^x 1 2.72 2.00 2 7.39 4.00... 10 22026.47 1024.00 11 59874.14 2048.00 12 162754.79 4096.00 13 442413.39 8192.00 14 1202604.28 16384.00 15 3269017.37 32768.00>

// Add spaces between fields. cout << setw(5) << i << " " << setw(10) << exp(x) << " " << setw(10) << pow(2.0,x) << endl;

expTable2

CSE202: Lecture 8 The Ohio State University 42

> expTable2.exeEnter number of values: 25 x e^x 2^x 1 2.72 2.00 2 7.39 4.00... 20 485165195.41 1048576.00 21 1318815734.48 2097152.00 22 3584912846.13 4194304.00 23 9744803446.25 8388608.00 24 26489122129.84 16777216.00 25 72004899337.39 33554432.00>

// Add spaces between fields. cout << setw(5) << i << " " << setw(10) << exp(x) << " " << setw(10) << pow(2.0,x) << endl;

CSE202: Lecture 8 The Ohio State University 43

expTable3.cpp... cout << "Enter number of values: "; cin >> n;

// DO NOT SET FIXED OUTPUT FORMAT // cout.setf(ios::fixed); cout.precision(2);

cout << setw(5) << "x" << setw(10) << "e^x" << setw(10) << "2^x" << endl;

for (int i = 1; i <= n; i++) { double x = i; cout << setw(5) << i << setw(10) << exp(x) << setw(10) << pow(2.0,x) << endl; }...

expTable3

CSE202: Lecture 8 The Ohio State University 44

> expTable3.exeEnter number of values: 25 1 2.7 2 2 7.4 4... 20 4.9e+08 1e+06 21 1.3e+09 2.1e+06 22 3.6e+09 4.2e+06 23 9.7e+09 8.4e+06 24 2.6e+10 1.7e+07 25 7.2e+10 3.4e+07>

cout << setw(5) << i << setw(10) << exp(x) << setw(10) << pow(2.0,x) << endl;

expTable3

CSE202: Lecture 8 The Ohio State University 45

> expTable3.exeEnter number of values: 10 x e^x 2^x 1 2.7 2 2 7.4 4 3 20 8 4 55 16 5 1.5e+02 32 6 4e+02 64 7 1.1e+03 1.3e+02 8 3e+03 2.6e+02 9 8.1e+03 5.1e+02 10 2.2e+04 1e+03 >

cout << setw(5) << i << setw(10) << exp(x) << setw(10) << pow(2.0,x) << endl;

CSE202: Lecture 8 The Ohio State University 46

Other Formatters• Iomanip contains many other formatters, including:

fixed Force show a decimal point with 6 trailing decimal places

scientific Outputs floating point number in scientific notation

hex Outputs integer in hexadecimal (base16)

oct Outputs integer in octal (base 8)

showpos Force positive numbers to be shown with a leading ‘+’ symbol

setprecision(n) Set floating point precision to n places

setw(n) Set the field width to n characters

CSE202: Lecture 8 The Ohio State University 47

outputFormat3// format output#include <iostream>#include <iomanip>using namespace std;

int main(){ double x;

cout << "Enter x: "; cin >> x;

// general format, default, 12 significant digits cout << "x = " << setprecision(12) << x << endl; // fixed decimal notation, 12 digits after decimal point cout << "x = " << fixed << setprecision(12) << x << endl; // scientific notation, 12 digits after decimal point cout << "x = " << scientific << setprecision(12) << x << endl; return 0;}

CSE202: Lecture 8 The Ohio State University 48

// general format, default, 12 significant digits

cout << "x = " << setprecision(12) << x << endl;

// fixed decimal notation, 12 digits after decimal point

cout << "x = " << fixed << setprecision(12) << x << endl;

// scientific notation, 12 digits after decimal point

cout << "x = " << scientific << setprecision(12) << x << endl;

> outputFormat3.exeEnter x: 123.45x = 123.45x = 123.450000000000x = 1.234500000000e+02

> outputFormat3.exeEnter x: 123456789e-17x = 1.23456789e-09x = 0.000000001235x = 1.234567890000e-09

CSE202: Lecture 8 The Ohio State University 49

Rounding Output// example of output rounding

#include <iostream>#include <iomanip>using namespace std;

int main(){ double x = 1.0/7.0;

cout << "x = " << setprecision(12) << x << endl; cout << "x = " << setprecision(7) << x << endl; cout << "x = " << setprecision(5) << x << endl; return 0;}

• C++ rounds output.

CSE202: Lecture 8 The Ohio State University 50

double x = 1.0/7.0;

cout << "x = " << setprecision(12) << x << endl;

cout << "x = " << setprecision(7) << x << endl;

cout << "x = " << setprecision(5) << x << endl;

> roundOutput.exex = 0.142857142857x = 0.1428571x = 0.14286

>

expTable

CSE202: Lecture 8 The Ohio State University 51

> expTable.exeEnter number of values: 10 x e^x 2^x 1 2.72 2.00 2 7.39 4.00 3 20.09 8.00 4 54.60 16.00 5 148.41 32.00 6 403.43 64.00 7 1096.63 128.00 8 2980.96 256.00 9 8103.08 512.00 10 22026.47 1024.00>

expTable4

CSE202: Lecture 8 The Ohio State University 52

> expTable4.exeEnter number of values: 10 x e^x 2^x 1 2.72 2 2 7.39 4 3 20.09 8 4 54.60 16 5 148.41 32 6 403.43 64 7 1096.63 128 8 2980.96 256 9 8103.08 512 10 22026.47 1024>

CSE202: Lecture 8 The Ohio State University 53

expTable4.cpp... cout << "Enter number of values: "; cin >> n;

cout.setf(ios::fixed);

cout << setw(5) << "x" << " " << setw(10) << "e^x" << " " << setw(8) << "2^x" << endl;

for (int i = 1; i <= n; i++) { double x = i; // Use precision 2 for e^x and precision 0 (integer) for 2^x. cout << setw(5) << i << " " << setw(10) << setprecision(2) << exp(x) << " " << setw(8) << setprecision(0) << pow(2.0,x) << endl; }...

expTable

CSE202: Lecture 8 The Ohio State University 54

> expTable4.exeEnter number of values: 10 x e^x 2^x 1 2.72 2 2 7.39 4 3 20.09 8 4 54.60 16 5 148.41 32 6 403.43 64 7 1096.63 128 8 2980.96 256 9 8103.08 512 10 22026.47 1024>

// Use precision 2 for e^x and precision 0 (integer) for 2^x. cout << setw(5) << i << " " << setw(10) << setprecision(2) << exp(x) << " " << setw(8) << setprecision(0) << pow(2.0,x) << endl;

Summary

• cout.setf(ios::fixed); // use fixed decimal notation• cout.setf(ios::scientific); // use scientific notation• cout.precision(k); // print k digits after decimal

iomanip:• #include <iomanip> // include IO manipulators (formatters)• cout << setw(k) << ...; // set next field width to k• cout << setprecision(k) << ...; // print k digits after decimal• cout << fixed << ...; // switch to fixed decimal notation• cout << scientific << ...; // switch to scientific notation

CSE202: Lecture 8 The Ohio State University 55