Download presentation
Presentation is loading. Please wait.
Published byReginald Atkins Modified over 9 years ago
1
Numeric Weirdness
2
Weirdness
3
Overflow Each data type has a limited range – Depends on platform/compiler Going past boundary wraps around
4
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
5
Floating Point Floating point numbers are ALWAYS approximate 0.1010 2 = 0.5 + 0.125 = 0.625 10 2382382 224224 212212 201201 2 -1 0.5 2 -2 0.25 2 -3 0.125 2 -4 0.0625 01010
6
Floating Point Floating point numbers are ALWAYS approximate 0.1010 2 = 0.5 + 0.125 = 0.625 10 0.1001 2 = 0.5 + 0.0625 = 0.5625 10 Where is 0.6? 2382382 224224 212212 201201 2 -1 0.5 2 -2 0.25 2 -3 0.125 2 -4 0.0625 01010
7
32 Bit Floating Point IEEE specifies conventions for floating points double representation
8
Data Types Floating Point Types NameSizeRangeSignificant Digits float32 bit +/- 3.4028235 x 10 38 ~7 double64 bits +/- 1.7976931348623157 x 10 308 ~15 long double 80 bits +/ - 1.18 x 10 4932 ~19
9
Too many choices!!!! Don't panic When in doubt: – Whole numbers int – Decimal numbers double
10
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
11
^ ^ is not exponentiation Binary XOR 3 = 0011 2 = 0010 XOR = 0001
12
Powers pow function – In library Must include!!! – Input: base, exponent – Output: answer – always a decimal value (double)
13
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.