Numeric Weirdness
Weirdness
Overflow Each data type has a limited range – Depends on platform/compiler Going past boundary wraps around
Data Types Integral Types NameSizeRange short16 bits–2 15 (-32,768) to 2 15 – 1 (32,767) unsigned short16 bits0 to 2 16 – 1 (65535) int32 bits unsigned int32 bits long32 bitsIn Windows, often 64 bits in Linux long 64 bits−9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 unsigned long long64 bits0 to 18,446,744,073,709,551,615
Floating Point Floating point numbers are ALWAYS approximate = =
Floating Point Floating point numbers are ALWAYS approximate = = = = Where is 0.6?
32 Bit Floating Point IEEE specifies conventions for floating points double representation
Data Types Floating Point Types NameSizeRangeSignificant Digits float32 bit +/ x ~7 double64 bits +/ x ~15 long double 80 bits +/ x ~19
Too many choices!!!! Don't panic When in doubt: – Whole numbers int – Decimal numbers double
Order matters Expressions evaluated in PEMDAS order Type rule: – Two ints : int answer – At least one decimal : decimal answer (1.0 / 2) + 1 (0.5) (1 / 2) + 1 (0) + 1 1
^ ^ is not exponentiation Binary XOR 3 = = 0010 XOR = 0001
Powers pow function – In library Must include!!! – Input: base, exponent – Output: answer – always a decimal value (double)
Powers double x = pow(4, 3); //x = 64 double x = pow(2.5, 2); //x = 5.25 double x = pow(9, 0.5); //x = 3.0