Floating Point Continued
- Exercise: Fahrenheit to Celsius
- C = 5/9(F-32)
- Why the parenthesis?, What happens without them?
- My code is here. NOTE:
THIS CODE USES STUFF WE TALK ABOUT LATER. IT IS A FINAL EXAMPLE
OF THE CODE, NOT THE WORK IN CLASS
- Coercion : Automatic conversion
- Bad deal
- FLOAT -> INT (drop the decimal part)
- int = float + 0.5 does rounding for us.
- INT -> FLOAT (add a .0)
- Single type expressions are evaluated in the type of the
expression and then cast.
- A Mixed Type Expression has multiple types, and
all operators are converted to float, the operation is performed
and the result is a float.
- Some code examples.
- Casting: programmer conversion
- It is better if you convert yourself
- two styles:
- float (var or expression)
- (float) var or expression
- Output manipulators
- You know about endl
- setw(n)
- #include <iomanip>
- Sets the width of numeric and string output.
- Is a mininal width suggestion. (things will take up more space
if they need to)
- Only good for the next variable output.
- setprecision(n)
- For the life of the program, or until another setprecision is
called.
- Digits to the right of the decimal place.
- showpoint
- Always show the decimal point in floating point numbers
- We need cout.setf(ios::showpoint);
- fixed
- Dont print in scientific notation.
- We need to use cout.setf(ios::fixed, ios::floatfield);