Basic Data Types and Calculations - Try It Out: Yet More Output Manipulators (
Page 11 of 14 )
Here’s the same code as in the previous “Try It Out” exercise, except that it uses additional manipulators to improve the appearance of the output:
// Program 2.8 Experimenting with floating point output
#include
#include
using std::setprecision; using std::fixed; using std::scientific;
using std::cout; using std::endl;
int main() { float value1 = 0.1f; float value2 = 2.1f; value1 -= 0.09f; value2 -= 2.09f;
cout << setprecision(14) << fixed;
cout << value1 - value2 << endl;
cout << setprecision(5) << scientific; cout << value1 - value2 << endl;
return 0; } // Should be 0.01
// Should be 0.01
// Change to fixed notation
// Should output zero
// Set scientific notation // Should output zero
When I run the modified program, this is the output I get:
0.00000000745058 7.45058e-009
(Continued)
This code uses three new manipulators. The
setprecision()
manipulator specifies how many decimal places should appear after the decimal point when you’re outputting a floating-point number. The
fixed
and
scientific
manipulators complement one another and choose the format in which a floating-point number should be displayed when they’re written to the stream.
By default, your C++ compiler will select either
scientific
or
fixed
, depending on the particular value you’re outputting, and you saw in the first version of this program that it performed that task admirably. The default number of decimal places isn’t defined in the standard, but five is common.
Let’s look at the changes made. Apart from the
#include
for
, just as you needed when you were using
setw()
earlier in the chapter and the additional
using
declarations, the interest is in these four lines of code:
|
cout << setprecision(14) << fixed; |
// Change to fixed notation |
| cout << value1 - value2 << endl; |
// Should output zero |
| cout << setprecision(5) << scientific; |
// Set scientific notation |
| cout << value1 - value2 << endl; |
// Should output zero |
The first line is easy: you use the manipulators like you used
setw()
, by sending them to the output stream with the insertion operator. Their effects can then clearly be seen in the first line of output: you get a floating-point value with 14 decimal places and no exponent.
Note that these manipulators differ from
setw()
in that they’re modal.In other words, they remain in effect for the stream until the end of the program, unless you set a different option. That’s the reason for the third line in the preceding code—you have to set scientific mode and a precision of 5 explicitly in order to return to “default” behavior. You can see that you’ve succeeded, though, because the second line of output is the same as the one produced by the original program.
NOTE Actually, the
header is only required here for the
setprecision()
manipulator. Both
fixed
and
scientific
are defined in
. There are more manipulators to discuss, but the rule is that the ones requiring values (such as
setw()
and
setprecision()
) are defined in
, whereas the others are defined in
.
Mathematical Functions
The
standard library header defines a range of trigonometric and numerical functions that you can use in your programs. You’ve already seen the
sqrt()
function. Table 2-11 presents some other numerical functions from this header that you may find useful.
Table 2-11.
Numerical Functions
Function
abs(arg)
fabs(arg) ceil(arg)
floor(arg)
exp(arg) log(arg) log10(arg) pow(arg1, arg2)
Description
Returns the absolute value of arg as the same type as
arg
, where
ar
g
can be of any floating-point type. There are versions of the
abs()
function declared in the
header file for arguments of typ
e
int
and type
long.
as the same type as arg , where ar g can be of any floating-point type. There are versions of the abs() function declared in the header file for arguments of typ e int and type long.
arg as the same type as arg , where ar g can be of any floating-point type. There are versions of the abs() function declared in the header file for arguments of typ e int and type long.
Returns the absolute value of arg as the same type as arg, where arg can be of any floating-point type. There are versions of the abs() function declared in the header file for arguments of type int and type long.
Returns the absolute value of
arg
as the same type as the argument.
The argument can be
int
,
long
,
float
,
double
, or
long double.
Returns a floating-point value of the same type as
arg
that is the smallest integer greater than or equal to
arg
, so
ceil(2.5)
produces the value 3.0.
arg
can be of any floating-point type.
Returns a floating-point value of the same type as
arg
that is the largest integer less than or equal to
arg
so the value returned by
floor(2.5
)
will be 2.0.
arg
can be of any floating-point type
.
Returns the value of
e
arg as the same type as
arg
.
arg
can be of any
floating-point type. The
log
function returns the natural logarithm (to base e) of
arg
as the same type as
arg
.
arg
can be any floating-point type.
The
log10
function returns the logarithm to base 10 of
arg
as the same
type as
arg
.
arg
can be any floating-point type. The
pow
function returns the value of
arg1
raised to the power
arg1,
which is arg1arg2. Thus the result of
pow(2, 3)
will be 8, and the result of
pow(1.5, 3)
will be 3.375. The arguments can be both of type
int
or any floating-point type. The second argument,
arg2
, may also be of type
int
with
arg1
of type
int
, or
long
, or any floating-point type. The value returned will be of the same type as
arg1.
Table 2-12 shows the trigonometric functions that you have available in the
header.
Table 2-12.
Trigonometric Functions
| Function |
Description |
|
cos(angle) |
Returns the cosine of the angle expressed in radians that is passed as the argument. |
|
sin(angle) |
Returns the sine of the angle expressed in radians that is passed as the |
| argument. |
|
tan(angle) |
Returns the tangent of the angle expressed in radians that is passed as the argument. |
|
cosh(angle) |
Returns the hyperbolic cosine of the angle expressed in radians that is passed as the argument. The hyperbolic cosine of a variable x is given by formula (ex-e-x)/2. |
|
sinh(angle) |
Returns the hyperbolic sine of the angle expressed in radians that is passed as the argument. The hyperbolic sine of a variable x is given by the formula (ex+e-x)/2. |
|
tanh(angle) |
Returns the hyperbolic tangent of the angle expressed in radians that is passed as the argument. The hyberbolic tangent of a variable x is given by the hyperbolic sine of x divided by the hyperbolic cosine of x. |
|
acos(arg) |
Returns the inverse cosine (arccosine) of
arg
. The argument must be between –1 and +1. The result is in radians and will be from 0 to p. |
|
asin(arg) |
Returns the inverse sine (arcsine) of the argument. The argument must be between –1 and +1. The result is in radians and will be from –p/2 to +p/2. |
|
atan(arg) |
Returns the inverse tangent (arctangent) of the argument. The result is in radians and will be from –p/2 to +p/2. |
|
atan2(arg1, arg2) |
This function requires two arguments of the same floating-point type. The function returns the inverse tangent of
arg1/arg2
. The result will be in the range from –p to +p radians and of the same type as the |
| |
arguments. |
The arguments to these functions can be of any floating-point type and the result will be returned as the same type as the argument(s).
Let’s look at some examples of how these are used. Here’s how you can calculate the sine of an angle in radians:
double angle = 1.5; // In radians double sine_value = std::sin(angle);
If the angle is in degrees, you can calculate the tangent by using a value for p to convert to radians:
float angle_deg = 60.0f; // Angle in degree
s const float pi = 1.14159f; const float pi_degrees = 180.0f; float tangent = std::tan(pi*angle_deg/pi_degrees);
If you know the height of the church steeple is 100 feet and you’re standing 50 feet from the base of the steeple, you can calculate the angle in radians of the top of the
| steeple like this: |
|
double height = 100.0; |
// Steeple height- feet |
| double distance = 50.0; |
// Distance from base |
| angle = std::atan2(height, distance); |
// Result in radians |
You can use this value in
angle
and the value of
distance
to calculate the distance from your toe to the top of the steeple:
double toe_to_tip = distance*std::cos(angle);
Of course, fans of Pythagoras of Samos could obtain the result much more easily, like this:
double toe_to_tip = std::sqrt(std::pow(distance,2) + std::pow(height, 2);