13
Numeric Weirdness

Numeric Weirdness. Weirdness Overflow Each data type has a limited range – Depends on platform/compiler Going past boundary wraps around

Embed Size (px)

Citation preview

Page 1: Numeric Weirdness. Weirdness Overflow Each data type has a limited range – Depends on platform/compiler Going past boundary wraps around

Numeric Weirdness

Page 2: Numeric Weirdness. Weirdness Overflow Each data type has a limited range – Depends on platform/compiler Going past boundary wraps around

Weirdness

Page 3: Numeric Weirdness. Weirdness Overflow Each data type has a limited range – Depends on platform/compiler Going past boundary wraps around

Overflow

• Each data type has a limited range– Depends on platform/compiler

• Going past boundary wraps around

Page 4: 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

Page 5: Numeric Weirdness. Weirdness Overflow Each data type has a limited range – Depends on platform/compiler Going past boundary wraps around

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

Page 6: Numeric Weirdness. Weirdness Overflow Each data type has a limited range – Depends on platform/compiler Going past boundary wraps around

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

Page 7: Numeric Weirdness. Weirdness Overflow Each data type has a limited range – Depends on platform/compiler Going past boundary wraps around

32 Bit Floating Point

• IEEE specifies conventions for floating points

double representation

Page 8: Numeric Weirdness. Weirdness Overflow Each data type has a limited range – Depends on platform/compiler Going past boundary wraps around

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

Page 9: Numeric Weirdness. Weirdness Overflow Each data type has a limited range – Depends on platform/compiler Going past boundary wraps around

Too many choices!!!!

• Don't panic• When in doubt:– Whole numbersint

– Decimal numbersdouble

Page 10: Numeric Weirdness. Weirdness Overflow Each data type has a limited range – Depends on platform/compiler Going past boundary wraps around

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

Page 11: Numeric Weirdness. Weirdness Overflow Each data type has a limited range – Depends on platform/compiler Going past boundary wraps around

^

• ^ is not exponentiation

• Binary XOR

3 = 0011

2 = 0010

XOR = 0001

Page 12: Numeric Weirdness. Weirdness Overflow Each data type has a limited range – Depends on platform/compiler Going past boundary wraps around

Powers

• pow function– In <cmath> library• Must include!!!

– Input:base, exponent

– Output:answer – always a decimal value (double)

Page 13: Numeric Weirdness. Weirdness Overflow Each data type has a limited range – Depends on platform/compiler Going past boundary wraps around

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