Download presentation
Presentation is loading. Please wait.
Published byKatrina McCarthy Modified over 9 years ago
1
Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Floating Point Arithmetic
2
Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Abstraction Anything that is not an integer can be thought of as. e.g. 391.1356 Or can be thought of as + / e.g. 201456 13/16
3
Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Leak 1 Numbers may not be exactly precise! 1/3 != 0.33333333333333333333 6.02214129 x 10 23 is not an exact Avogadro’s constant 3.14159265358979323846264338327950288419716939937510 is not exactly π
4
Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Leak 2 On computers, fractional numbers must be represented by bits Implies base 2 Implies “binary point” 2 2121 2020.2 -1 2 -2 2 -3 2 -4 … 421.1/21/41/81/16… …101.1001…
5
Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Leak 3 Almost infinite number of ways to represent floating point numbers Implied binary point int numerator, int denominator Scientific notation with int wholenumber, int fraction, int exponent …
6
Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Standard: IEEE 754 Abstract Representation: Decimal: [+/-]. x 10 e.g. 6.022 x 10 23 Binary: [+/-]1. x 2 e.g 1.11111110000101… x 2 78 Special case for 0, +/- ∞, “Not a Number” Concrete Representation (float) SEXPMANTISSA b 31 b 30 b 29 b 28 b 27 b 26 b 25 b 24 b 23 b 22 b 21 b 20 b 19 b 18 b 17 b 16 b 15 b 14 b 13 b 12 b 11 b 10 b9b9 b8b8 b7b7 b6b6 b5b5 b4b4 b3b3 b2b2 b1b1 b0b0
7
Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 IEEE 754 Special Cases +/-0 +/-∞ +/- Not a Number (NaN) SEXPMANTISSA S0000000000000000000000000000000 SEXPMANTISSA S1111111100000000000000000000000 SEXPMANTISSA S1111111100000000100000000000000
8
Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 abstract IEEE 754 Abstract vs. Concrete CONCRETE
9
Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Example: Abstract to Concrete Abstract: 3.1416 Convert to binary: 11.00100100001111…. Normalize: 1.100100100001111… x 2 1 Concrete: S = 0 (positive) EXP = 1 + 127 = 128 = 0b1000 0000 MANTISSA = 100100100001111…. =0x40490FF9 SEXPMANTISSA 01000000010010010000111111111001 40490FF9 See Also: xmp_floatxmp_float
10
Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Example: Concrete to Abstract Concrete: 0x6703ece6 S = 0, + EXP = 0xCE = 206, exponent = 206 – 127 = 79 mantissa = 1.0000011111011… Abstract Value: + 0b1.0000011111011.. x 2 79 = 6.23 x 10 23 SEXPMANTISSA 01100111000000111110110011100110 6703ECE6
11
Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Multiplying two floating point numbers Given S 1, EXP 1, MANT 1 ; and S 2, EXP 2, MANT 2, Compute S p, EXP p, MANT p Exclusive OR (^) S1S1 S2S2 SpSp EXP 1 EXP 2 + -127 ADD 1. EXP i ADD 1. * MANT 1 MANT 2 AMANT i Normalize EXP p MANT p
12
Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Given S 1, EXP 1, MANT 1 ; and S 2, EXP 2, MANT 2, Compute S s, EXP s, MANT s If (S 1 != S 2 ) { S 1 = !S 1; return SUBTRACT(…)} Adding Two Floating Point Numbers S1S1 SsSs EXP 1 EXP 2 MAX EXP i ADD 1. MANT 1 MANT 2 SHIFT. + AMANT i Normalize EXP s MANT s
13
Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Dealing with Precision BAD float x=1.0/3.0; float y=1.0 – (2.0/3.0); if (x==y) {…. } BETTER #define EPSILON 0.0001 float x=1.0/3.0; float y=1.0 – (2.0/3.0); if (EPSILON > (x-y)) { … }
14
Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 int percent=foo(); int base=bar(); int result = base * (percent/100); int result = base * (float)(percent/100); int result = (base * percent)/100; int result = base * (percent / (float) 100); int result = base * (percent / 100.0); Implicit Casting Gotcha
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.