Numeric Data Types
- Integers
- Floating Point
- These are real numbers (with a decimal point in the number)
- float, double, long double
- Sizes vary based on machine
- litterals
- {+|-}{digit*}{.}{digit*}{E{+|-}digit*}
- 1.234565
- 1.0E5
- 1.0E-5
- Floating point numbers are limited in size.
- Consider 1/3 in decimal = .3333333333333...
- .1 in base 2 is .000101001010010100...
- Declaring constants and variables
- Just like before
- const int MAXSTUDENTS = 10;
- const int PI = 3.14159265;
- int x;
- float temprature;
- Assignment Statements
- x = 5;
- temprature = 33.94;
- Simple Expressions
- Two more strange operators
- auto increment and auto decrement
- prefix and postfix
- a = a + 1
- a++
- ++a
- Rules of precedence
- From Left to right, at the same level
- Unary +,-
- * / %
- + -
- When in doubt use ()
- Conversion, Type Casting
- Coercion
- You should type cast or convert your variables
- In eather case, floats are truncated, not rounded.
- IntValue = int (FloatValue + 0.5); // this will round
The two example programs from class.
Problem:
Write a program that will turn a measurement in inches to Miles, yards, feet
and inches
Unit | conversion |
1 Mile | 63360 inches |
1 Yard | 36 inches |
1 Foot | 12 inches |