Upload
reginald-atkins
View
214
Download
1
Embed Size (px)
Citation preview
Numeric Weirdness
Weirdness
Overflow
• Each data type has a limited range– Depends on platform/compiler
• Going past boundary wraps around
Data Types
• Integral Types
Name Size Range
short 16 bits –215 (-32,768) to 215 – 1 (32,767)
unsigned short 16 bits 0 to 216 – 1 (65535)
int 32 bits
unsigned int 32 bits
long 32 bits In Windows, often 64 bits in Linux
long long 64 bits −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned long long 64 bits 0 to 18,446,744,073,709,551,615
Floating Point
• Floating point numbers are ALWAYS approximate
0.10102 = 0.5 + 0.125 = 0.62510
23
822
421
220
12-1
0.52-2
0.252-3
0.1252-4
0.0625
0 1 0 1 0
Floating Point
• Floating point numbers are ALWAYS approximate
0.10102 = 0.5 + 0.125 = 0.62510
0.10012 = 0.5 + 0.0625 = 0.562510
• Where is 0.6?
23
822
421
220
12-1
0.52-2
0.252-3
0.1252-4
0.0625
0 1 0 1 0
32 Bit Floating Point
• IEEE specifies conventions for floating points
double representation
Data Types
• Floating Point Types
Name Size Range Significant Digits
float 32 bit
+/- 3.4028235 x 1038 ~7
double 64 bits
+/- 1.7976931348623157 x 10308 ~15
long double
80 bits
+/ - 1.18 x 104932 ~19
Too many choices!!!!
• Don't panic• When in doubt:– Whole numbersint
– Decimal numbersdouble
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
1.5
(1 / 2) + 1(0) + 1
1
^
• ^ is not exponentiation
• Binary XOR
3 = 0011
2 = 0010
XOR = 0001
Powers
• pow function– In <cmath> 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