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 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. Thesetprecision()manipulator specifies how many decimal places should appear after the decimal point when you’re outputting a floating-point number. Thefixedandscientificmanipulators 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 eitherscientificorfixed, 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#includefor
The first line is easy: you use the manipulators like you usedsetw(), 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 fromsetw()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 The Table 2-11. abs(arg) fabs(arg) ceil(arg) floor(arg) exp(arg) log(arg) log10(arg) pow(arg1, arg2) DescriptionReturns the absolute value of arg as the same type asarg, whereargcan be of any floating-point type. There are versions of theabs()function declared in the Returns the absolute value ofargas the same type as the argument. The argument can beint,long,float,double, orlong double.Returns a floating-point value of the same type asargthat is the smallest integer greater than or equal toarg, soceil(2.5)produces the value 3.0.argcan be of any floating-point type. Returns a floating-point value of the same type asargthat is the largest integer less than or equal toargso the value returned byfloor(2.5)will be 2.0.argcan be of any floating-point type. Returns the value ofearg as the same type asarg.argcan be of any floating-point type. Thelogfunction returns the natural logarithm (to base e) ofargas the same type asarg.argcan be any floating-point type. Thelog10function returns the logarithm to base 10 ofargas the same type asarg.argcan be any floating-point type. Thepowfunction returns the value ofarg1raised to the powerarg1,which is arg1arg2. Thus the result ofpow(2, 3)will be 8, and the result ofpow(1.5, 3)will be 3.375. The arguments can be both of typeintor any floating-point type. The second argument,arg2, may also be of typeintwitharg1of typeint, orlong, or any floating-point type. The value returned will be of the same type asarg1. Table 2-12 shows the trigonometric functions that you have available in the
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 degrees 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
You can use this value inangleand the value ofdistanceto 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);
blog comments powered by Disqus | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
|
|