Share
Embed
Floats are Friends: Making the Most of IEEE 754.00000000002
David Wolever
May 04, 2019
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
136 slides
-
Floats are Friends: Making the Most of IEEE 754.000000002 David
Wolever @wolever Excited to be here today talking to you about floating point numbers - What brought you here today? What are you hoping to learn? Tweet at me, I’d love to hear! - Folks in the front: can you take a picture? - Say this up front: I’ve taken some artistic license with the title; 754 can be exactly represented in floating point, please accept my apology -
@wolever Floats are Friends They aren’t the best They also
aren’t the worst But we are definitely stuck with them Over this talk, I’d like to tell you a bit more about what floats are, how they work, and why they do the weird things they do. And hopefully by the end … well, I don’t expect you to like them. But at least understand them. -
@wolever Why do Floats Exist? need to take a step
further back and look at how numbers are represented in computers -
@wolever Whole Numbers (Integers) Pretty easy one-to-one mapping between binary
numbers and decimal integers (you might notice negative numbers are missing here … -
@wolever Whole Numbers (Integers) Pretty easy 0 0 0 0
0 0 0 one-to-one mapping between binary numbers and decimal integers (you might notice negative numbers are missing here … -
@wolever Whole Numbers (Integers) Pretty easy 0 0 0 0
0 0 0 1 0 0 0 0 0 1 one-to-one mapping between binary numbers and decimal integers (you might notice negative numbers are missing here … -
@wolever Whole Numbers (Integers) Pretty easy 0 0 0 0
0 0 0 1 0 0 0 0 0 1 2 0 0 0 0 1 0 one-to-one mapping between binary numbers and decimal integers (you might notice negative numbers are missing here … -
@wolever Whole Numbers (Integers) Pretty easy 0 0 0 0
0 0 0 1 0 0 0 0 0 1 42 1 0 1 0 1 0 2 0 0 0 0 1 3 0 0 0 0 1 1 ⋮ 0 one-to-one mapping between binary numbers and decimal integers (you might notice negative numbers are missing here … -
@wolever Whole Numbers (Integers) Two’s Complement … a representation called
two’s complement is used to handle negative number. It’s a thing of beauty, and I’d really suggest looking it up. But alas, it’s outside the scope of this talk. -
@wolever Whole Numbers (Integers) Work Pretty Well INT_MIN (32 bit):
−2,147,483,648 INT_MAX (32 bit): +2,147,483,647 2 billion 9 quadrillion -
@wolever Whole Numbers (Integers) Work Pretty Well INT_MIN (32 bit):
−2,147,483,648 INT_MAX (32 bit): +2,147,483,647 LONG_MIN (64 bit): −9,223,372,036,854,775,808 LONG_MAX (64 bit): +9,223,372,036,854,775,807 2 billion 9 quadrillion -
@wolever Fractional Numbers (Reals) A Bit More Difficult 0 0
0 0 0 . 0.125 0 0 0 1 . -
@wolever Fractional Numbers (Reals) A Bit More Difficult 0 0
0 0 0 . 0.125 0 0 0 1 . 0.25 0 0 1 0 . -
@wolever Fractional Numbers (Reals) A Bit More Difficult 0 0
0 0 0 . 0.125 0 0 0 1 . 0.25 0 0 1 0 . 0.375 0 0 1 1 . 0.5 0 1 0 0 . 0.875 0 1 1 1 . ⋮ -
@wolever Fractional Numbers (Reals) A Bit More Difficult FIXED (16,
16) smallest: 1.5 ⋅ 10−5 ≈ 2−16 FIXED (16, 16) largest: 131,071.999985 ≈ 217 − 2−16 -
@wolever Fractional Numbers (Reals) A Bit More Difficult FIXED (16,
16) smallest: 1.5 ⋅ 10−5 ≈ 2−16 FIXED (16, 16) largest: 131,071.999985 ≈ 217 − 2−16 FIXED (32, 32) smallest: 2.3 ⋅ 10−10 = 2−32 FIXED (32, 32) largest: 4,294,967,296 ≈ 232 − 2−32 -
@wolever Fractional Numbers (Reals) A Bit More Difficult FIXED (16,
16) smallest: 1.5 ⋅ 10−5 ≈ 2−16 FIXED (16, 16) largest: 131,071.999985 ≈ 217 − 2−16 FIXED (32, 32) smallest: 2.3 ⋅ 10−10 = 2−32 FIXED (32, 32) largest: 4,294,967,296 ≈ 232 − 2−32 (ignoring negative numbers) -
@wolever Fractional Numbers (Reals) A Bit More Difficult In the
real world, it’s not uncommon to deal with numbers outside this range But in this naive system where we’re fixing the position of the decimal place - a fixed point number, if you will - begins to run into problems, because it can’t represent the range of numbers we’ll encounter. … to move that decimal place around - to float it. Almost like you’ve implemented a floating point. -
@wolever Fractional Numbers (Reals) A Bit More Difficult Pluto: 7.5e12
M (7.5 billion kilometres) Water molecule: 2.8e-10 M (0.28 nanometers) In the real world, it’s not uncommon to deal with numbers outside this range But in this naive system where we’re fixing the position of the decimal place - a fixed point number, if you will - begins to run into problems, because it can’t represent the range of numbers we’ll encounter. … to move that decimal place around - to float it. Almost like you’ve implemented a floating point. -
@wolever Fractional Numbers (Reals) A Bit More Difficult Pluto: 7.5e12
M (7.5 billion kilometres) Water molecule: 2.8e-10 M (0.28 nanometers) >>> distance_to_pluto = number(7.5, scale=12) >>> size_of_water = number(2.8, scale=-10) In the real world, it’s not uncommon to deal with numbers outside this range But in this naive system where we’re fixing the position of the decimal place - a fixed point number, if you will - begins to run into problems, because it can’t represent the range of numbers we’ll encounter. … to move that decimal place around - to float it. Almost like you’ve implemented a floating point. -
@wolever Floating Point Numbers ± E E E E F
F F F F F F … if you’ve ever seen scientific notation, this is exactly the same thing; this is base 2 scientific notation. -
@wolever Floating Point Numbers ± E E E E F
F F F F F F Sign (+ or -) … if you’ve ever seen scientific notation, this is exactly the same thing; this is base 2 scientific notation. -
@wolever Floating Point Numbers ± E E E E F
F F F F F F Sign (+ or -) Exponent … if you’ve ever seen scientific notation, this is exactly the same thing; this is base 2 scientific notation. -
@wolever Floating Point Numbers ± E E E E F
F F F F F F Sign (+ or -) Exponent Fraction (also called mantissa) … if you’ve ever seen scientific notation, this is exactly the same thing; this is base 2 scientific notation. -
@wolever Floating Point Numbers ± E E E E F
F F F F F F Sign (+ or -) Exponent Fraction … if you’ve ever seen scientific notation, this is exactly the same thing; this is base 2 scientific notation. -
@wolever Floating Point Numbers ± E E E E F
F F F F F F Sign (+ or -) Exponent Fraction (also called mantissa) … if you’ve ever seen scientific notation, this is exactly the same thing; this is base 2 scientific notation. -
@wolever Floating Point Numbers ± E E E E F
F F F F F F Sign (+ or -) Exponent (if you’re trying to sound fancy) Fraction (also called mantissa) … if you’ve ever seen scientific notation, this is exactly the same thing; this is base 2 scientific notation. -
@wolever Floating Point Numbers ± E E E E F
F F F F F F Sign (+ or -) Exponent (if you’re trying to sound fancy) frac × 2exp value = sign × Fraction (also called mantissa) … if you’ve ever seen scientific notation, this is exactly the same thing; this is base 2 scientific notation. -
@wolever Floating Point Numbers 0 1 0 0 0 0
0 1 0.5 1 × 23−4 -
@wolever Floating Point Numbers 0 1 0 0 0 0
0 1 0.5 1 × 23−4 Exponent bias: half the exponent’s maximum value -
@wolever Floating Point Numbers 0 1 0 0 0 0
0 1 0.5 1 × 23−4 caveat: FPs are stored normalized, so the leading "1" is omitted -
@wolever Floating Point Numbers 0 1 0 0 0 0
0 1 0.5 0 1 0 1 1 1 0 1 3.25 1 × 23−4 13 × 23−5 -
@wolever Floating Point Numbers 0 1 0 0 0 0
0 1 0.5 0 1 0 1 1 1 0 1 3.25 1 × 23−4 13 × 23−5 1 0 0 0 1 0 1 1 -88 11 × 23−0 caveat: FPs are stored normalized, so the leading "1" is omitted -
@wolever Floating Point Numbers 0 1 0 0 0 0
0 1 0.5 0 1 0 1 1 1 0 1 3.25 1 × 23−4 13 × 23−5 1 0 0 0 1 0 1 1 -88 11 × 23−0 1 1 1 0 0 0 0 1 -0.0125 1 × 23−6 caveat: FPs are stored normalized, so the leading "1" is omitted -
@wolever Neat! We’ve got a number system that can scale
to represent very small numbers or very large numbers -
@wolever Floating Point Numbers exponent fraction smallest largest 32 bit
(float) 8 bits 23 bits 1.18e-38 3.4e+38 64 bit (double) 11 bits 52 bits 2.2e-308 1.8e+308 -
@wolever Floating Point Numbers 179,769,313,486,231,570,814,527,423,731,704,356,798, 070,567,525,844,996,598,917,476,803,157,260,780,028, 538,760,589,558,632,766,878,171,540,458,953,514,382, 464,234,321,326,889,464,182,768,467,546,703,537,516, 986,049,910,576,551,282,076,245,490,090,389,328,944, 075,868,508,455,133,942,304,583,236,903,222,948,165,
808,559,332,123,348,274,797,826,204,144,723,168,738, 177,180,919,299,881,250,404,026,184,124,858,368 -
@wolever Floating Point Numbers A Tradeoff Precision Magnitude How small
can we get? How big can we get? -
@wolever Floating Point Numbers A Tradeoff Precision Magnitude How small
can we get? How big can we get? We can measure the distance to Pluto (but it won’t be reliable down to the meter) -
@wolever Floating Point Numbers A Tradeoff Precision Magnitude How small
can we get? How big can we get? We can measure the distance to Pluto (but it won’t be reliable down to the meter) We can measure the size of a water molecule (but not a billion of them at the same time) -
@wolever Floating Point Numbers wat >>> 1.0 1.0 >>> 1e20
1e+20 >>> 1e20 + 1 1e+20 >>> 1e20 + 1 == 1e20 True -
@wolever Floating Point Numbers wat >>> 1.0 1.0 >>> 1e20
1e+20 >>> 1e20 + 1 1e+20 >>> 1e20 + 1 == 1e20 True -
@wolever Floating Point Numbers wat >>> 1.0 1.0 >>> 1e20
1e+20 >>> 1e20 + 1 1e+20 >>> 1e20 + 1 == 1e20 True -
@wolever Floating Point Numbers wat >>> 1.0 1.0 >>> 1e20
1e+20 >>> 1e20 + 1 1e+20 >>> 1e20 + 1 == 1e20 True -
@wolever Floating Point Numbers wat do? 1. Rule of thumb:
doubles have 15 significant digits -
@wolever Floating Point Numbers wat do? 1. Rule of thumb:
doubles have 15 significant digits 2. Precision is lost when adding or subtracting numbers with different magnitudes: -
@wolever Floating Point Numbers wat do? 1. Rule of thumb:
doubles have 15 significant digits 2. Precision is lost when adding or subtracting numbers with different magnitudes: >>> 12345 + 1e15 1000000000012345 >>> 12345 + 1e16 10000000000012344 >>> 12345 + 1e17 100000000000012352 -
@wolever Floating Point Numbers wat do? 1. Rule of thumb:
doubles have 15 significant digits 2. Precision is lost when adding or subtracting numbers with different magnitudes: >>> 12345 + 1e15 1000000000012345 >>> 12345 + 1e16 10000000000012344 >>> 12345 + 1e17 100000000000012352 -
@wolever Floating Point Numbers wat do? 1. Rule of thumb:
doubles have 15 significant digits 2. Precision is lost when adding or subtracting numbers with different magnitudes: >>> 12345 + 1e15 1000000000012345 >>> 12345 + 1e16 10000000000012344 >>> 12345 + 1e17 100000000000012352 -
@wolever Floating Point Numbers wat do? 1. Rule of thumb:
doubles have 15 significant digits 2. Precision is lost when adding or subtracting numbers with different magnitudes: >>> 12345 + 1e15 1000000000012345 >>> 12345 + 1e16 10000000000012344 >>> 12345 + 1e17 100000000000012352 (multiplication and division are fine, though!) -
@wolever Floating Point Numbers wat do? 3. Use a library
to sum floats: -
@wolever Floating Point Numbers wat do? 3. Use a library
to sum floats: >>> sum([-1e20, 1, 1e20]) 0.00000000000000000000 >>> math.fsum([-1e20, 1, 1e20]) 1.00000000000000000000 >>> np.sum([-1e20, 1, 1e20]) 0.00000000000000000000 -
@wolever Floating Point Numbers wat do? 3. Use a library
to sum floats: >>> sum([-1e20, 1, 1e20]) 0.00000000000000000000 >>> math.fsum([-1e20, 1, 1e20]) 1.00000000000000000000 >>> np.sum([-1e20, 1, 1e20]) 0.00000000000000000000 -
@wolever Floating Point Numbers wat do? 3. Use a library
to sum floats: >>> sum([-1e20, 1, 1e20]) 0.00000000000000000000 >>> math.fsum([-1e20, 1, 1e20]) 1.00000000000000000000 >>> np.sum([-1e20, 1, 1e20]) 0.00000000000000000000 -
@wolever Floating Point Numbers wat do? 3. Use a library
to sum floats: >>> sum([-1e20, 1, 1e20]) 0.00000000000000000000 >>> math.fsum([-1e20, 1, 1e20]) 1.00000000000000000000 >>> np.sum([-1e20, 1, 1e20]) 0.00000000000000000000 See also: accupy -
@wolever Floating Point Numbers A Tradeoff Every real number can’t
be represented Some are infinite: π, e, etc Some can’t be expressed in binary: 0.1 -
@wolever Floating Point Numbers wat >>> 0.1 0.10000000000000000555 >>> "%0.20f"
%(0.1, ) 0.10000000000000000555 Note: floating point values will be shown to 20 decimal places: -
@wolever Floating Point Numbers A Tradeoff 0.1 3.1415926… 0.100000005 3.1416
0.5 1.0 (the difference between a real number and the nearest number that can be represented is called "relative error") -
@wolever Floating Point Numbers wat >>> 0.1 0.10000000000000000555 >>> 0.2
0.20000000000000001110 >>> 0.3 0.29999999999999998890 >>> 0.1 + 0.2 0.30000000000000004441 >>> sum([0.1] * 10) 0.99999999999999988898 >>> 0.1 * 10 1.00000000000000000000 One of the problems with this roundoff error is that it compounds -
@wolever Floating Point Numbers wat >>> 0.1 0.10000000000000000555 >>> 0.2
0.20000000000000001110 >>> 0.3 0.29999999999999998890 >>> 0.1 + 0.2 0.30000000000000004441 >>> sum([0.1] * 10) 0.99999999999999988898 >>> 0.1 * 10 1.00000000000000000000 One of the problems with this roundoff error is that it compounds -
@wolever Floating Point Numbers wat >>> 0.1 0.10000000000000000555 >>> 0.2
0.20000000000000001110 >>> 0.3 0.29999999999999998890 >>> 0.1 + 0.2 0.30000000000000004441 >>> sum([0.1] * 10) 0.99999999999999988898 >>> 0.1 * 10 1.00000000000000000000 One of the problems with this roundoff error is that it compounds -
@wolever Floating Point Numbers wat >>> 0.1 0.10000000000000000555 >>> 0.2
0.20000000000000001110 >>> 0.3 0.29999999999999998890 >>> 0.1 + 0.2 0.30000000000000004441 >>> sum([0.1] * 10) 0.99999999999999988898 >>> 0.1 * 10 1.00000000000000000000 One of the problems with this roundoff error is that it compounds -
@wolever Floating Point Numbers wat >>> 0.1 0.10000000000000000555 >>> 0.2
0.20000000000000001110 >>> 0.3 0.29999999999999998890 >>> 0.1 + 0.2 0.30000000000000004441 >>> sum([0.1] * 10) 0.99999999999999988898 >>> 0.1 * 10 1.00000000000000000000 One of the problems with this roundoff error is that it compounds -
@wolever Floating Point Numbers wat >>> 0.1 0.10000000000000000555 >>> 0.2
0.20000000000000001110 >>> 0.3 0.29999999999999998890 >>> 0.1 + 0.2 0.30000000000000004441 >>> sum([0.1] * 10) 0.99999999999999988898 >>> 0.1 * 10 1.00000000000000000000 One of the problems with this roundoff error is that it compounds -
@wolever Floating Point Numbers wat do? 1. Remember that every
operation introduces some error (nothing you can do about this) dot product of two perpendicular vectors -
@wolever Floating Point Numbers wat do? 1. Remember that every
operation introduces some error (nothing you can do about this) 2. Be careful when comparing floats (especially to 0.0) dot product of two perpendicular vectors -
@wolever Floating Point Numbers wat do? 1. Remember that every
operation introduces some error (nothing you can do about this) 2. Be careful when comparing floats (especially to 0.0) >>> np.isclose(0.1 + 0.2 - 0.3, 0.0) True >>> def isclose(a, b, epsilon=1e-8): ... return abs(a - b) < epsilon >>> isclose(0.1 + 0.2, 0.3) True dot product of two perpendicular vectors -
@wolever Floating Point Numbers wat do? 3. Round floats to
the precision you need before displaying them: >>> "%0.2f" %(0.1, ) '0.10' >>> "%0.2f" %(0.1 + 0.2, ) '0.30' >>> "%0.2f" %(sum([0.1] * 10), ) '1.00' -
@wolever the weird parts inf / -inf 01 1 1
0 0 0 0 ± >>> inf = float('inf') >>> inf > 1e308 True >>> inf > inf False -
@wolever the weird parts inf / -inf >>> 1e308 +
1e308 inf >>> -1e308 - 1e308 -inf Result of overflowing a large number: -
@wolever the weird parts inf / -inf >>> np.array([1.0]) /
np.array([0.0]) RuntimeWarning: divide by zero encountered in divide array([inf]) >>> 1.0 / 0.0 … ZeroDivisionError: float division by zero Result of dividing by zero (sometimes): -
@wolever the weird parts inf / -inf >>> np.array([1.0]) /
np.array([0.0]) RuntimeWarning: divide by zero encountered in divide array([inf]) >>> 1.0 / 0.0 … ZeroDivisionError: float division by zero Result of dividing by zero (sometimes): -
@wolever the weird parts -0 00 0 0 0 0
0 0 ± >>> float('-0') -0.0 >>> -1e-323 / 10 -0.0 Result of underflowing a small number: -
@wolever the weird parts -0 "Useful" to know the sign
of inf when dividing by 0: >>> np.array([1.0, 1.0]) / ... np.array([float('0'), float('-0')]) array([ inf, -inf]) -
@wolever the weird parts -0 Otherwise behaves like 0: >>>
float('-0') == float('0') True >>> float('-0') / 42.0 -0.0 -
@wolever the weird parts nan 0 Not A Number Nan
is a number that - like it says right there on the box - is Not A Number -
@wolever the weird parts nan >>> float('inf') / float('inf') nan
Result of mathematically undefined operations: 01 1 1 0 0 0 1 ± -
@wolever the weird parts nan >>> float('inf') / float('inf') nan
Result of mathematically undefined operations: 01 1 1 0 0 0 1 ± >>> math.sqrt(-1) ValueError: math domain error Although Python is more helpful: -
@wolever the weird parts nan >>> nan = float('nan') >>>
nan == nan False >>> 1 > nan False >>> 1 < nan False >>> 1 + nan nan Wild, breaks everything: -
@wolever the weird parts nan >>> nan = float('nan') >>>
nan == nan False >>> 1 > nan False >>> 1 < nan False >>> 1 + nan nan Wild, breaks everything: -
@wolever the weird parts nan >>> nan = float('nan') >>>
nan == nan False >>> 1 > nan False >>> 1 < nan False >>> 1 + nan nan Wild, breaks everything: -
@wolever the weird parts nan >>> nan = float('nan') >>>
nan == nan False >>> 1 > nan False >>> 1 < nan False >>> 1 + nan nan Wild, breaks everything: -
@wolever the weird parts nan >>> a = np.array([1.0, 0.0,
3.0]) >>> b = np.array([5.0, 0.0, 7.0]) >>> np.nanmean(a / b) 0.3142857142857143 Useful if you want to ignore invalid values: -
@wolever the weird parts nan >>> math.isnan(nan) True >>> nan
!= nan True Check for nan with isnan or x != x: -
@wolever the weird parts nan Pop quiz: how many nans
are there? 252 -
@wolever the weird parts nan Pop quiz: how many nans
are there? 252 01 1 1 X X X X ± -
@wolever the weird parts nan Why not us all those
nans as pointers? * The top 16-bits denote the type of the encoded JSValue: * * Pointer { 0000:PPPP:PPPP:PPPP * / 0001:****:****:**** * Double { ... * \ FFFE:****:****:**** * Integer { FFFF:0000:IIII:IIII (from WebKit’s JSCJSValue.h) -
@wolever the weird parts nan JsObj JsObj_add(JsObj a, JsObj b)
{ if (JS_IS_DOUBLE(a) && JS_IS_DOUBLE(b)) return a + b if (JS_IS_STRING_REF(a) && JS_IS_STRING_REF(b)) return JsString_concat(a, b) ... } -
@wolever the weird parts nan JsObj JsObj_add(JsObj a, JsObj b)
{ if (JS_IS_DOUBLE(a) && JS_IS_DOUBLE(b)) return a + b if (JS_IS_STRING_REF(a) && JS_IS_STRING_REF(b)) return JsString_concat(a, b) ... } -
@wolever the weird parts nan JsObj JsObj_add(JsObj a, JsObj b)
{ if (JS_IS_DOUBLE(a) && JS_IS_DOUBLE(b)) return a + b if (JS_IS_STRING_REF(a) && JS_IS_STRING_REF(b)) return JsString_concat(a, b) ... } -
@wolever I want to offer you some hope we’ve talked
about how precision can be lost when numbers of different magnitudes are added or subtracted And we’ve talked about the problems that come up with trying to convert between binary and decimal fractions -
@wolever decimal Exact representations of decimal numbers The "nearest number"
rounding will still happen, but it will be more sensible -
@wolever decimal Exact representations of decimal numbers The "nearest number"
rounding will still happen, but it will be more sensible Precision still needs to be specified… -
@wolever decimal Exact representations of decimal numbers The "nearest number"
rounding will still happen, but it will be more sensible Precision still needs to be specified… … but the default is 28 decimal places -
@wolever decimal >>> from decimal import Decimal >>> d =
Decimal('0.1') >>> d + d + d + d + d + d + d + d + d + d Decimal('1.0') >>> pi = Decimal(math.pi) >>> pi Decimal('3.141592653589793115997963…') -
@wolever decimal >>> from decimal import Decimal >>> d =
Decimal('0.1') >>> d + d + d + d + d + d + d + d + d + d Decimal('1.0') >>> pi = Decimal(math.pi) >>> pi Decimal('3.141592653589793115997963…') -
@wolever decimal >>> from decimal import Decimal >>> d =
Decimal('0.1') >>> d + d + d + d + d + d + d + d + d + d Decimal('1.0') >>> pi = Decimal(math.pi) >>> pi Decimal('3.141592653589793115997963…') -
@wolever decimal >>> from decimal import Decimal >>> d =
Decimal('0.1') >>> d + d + d + d + d + d + d + d + d + d Decimal('1.0') >>> pi = Decimal(math.pi) >>> pi Decimal('3.141592653589793115997963…') -
@wolever decimal In [1]: d = Decimal('42') In [2]: %timeit
d * d 100,000 loops, best of 3: 7.28 µs per loop In [3]: f = 42.0 In [4]: %timeit f * f 10,000,000 loops, best of 3: 44.6 ns per loop -
@wolever decimal In [1]: d = Decimal('42') In [2]: %timeit
d * d 100,000 loops, best of 3: 7.28 µs per loop In [3]: f = 42.0 In [4]: %timeit f * f 10,000,000 loops, best of 3: 44.6 ns per loop -
@wolever decimal In [1]: d = Decimal('42') In [2]: %timeit
d * d 100,000 loops, best of 3: 7.28 µs per loop In [3]: f = 42.0 In [4]: %timeit f * f 10,000,000 loops, best of 3: 44.6 ns per loop -
@wolever decimal >>> from pympler.asizeof import asizeof >>> asizeof(42.0) 24
>>> asizeof(1e308) 24 >>> asizeof(Decimal('42')) 168 >>> asizeof(Decimal('1e308')) 192 -
@wolever decimal >>> from pympler.asizeof import asizeof >>> asizeof(42.0) 24
>>> asizeof(1e308) 24 >>> asizeof(Decimal('42')) 168 >>> asizeof(Decimal('1e308')) 192 -
@wolever decimal >>> from pympler.asizeof import asizeof >>> asizeof(42.0) 24
>>> asizeof(1e308) 24 >>> asizeof(Decimal('42')) 168 >>> asizeof(Decimal('1e308')) 192 -
@wolever decimal >>> from pympler.asizeof import asizeof >>> asizeof(42.0) 24
>>> asizeof(1e308) 24 >>> asizeof(Decimal('42')) 168 >>> asizeof(Decimal('1e308')) 192 -
Selected References • "What Every Computer Scientist Should Know About
Floating-Point Arithmetic": http://docs.sun.com/source/806-3568/ncg_goldberg.html (note: very math and theory heavy; not especially useful) • "Points on Floats": https://matthew-brett.github.io/teaching/ floating_point.html#floating-point (much more approachable) • "Float Precision–From Zero to 100+ Digits": https:// randomascii.wordpress.com/2012/03/08/float-precisionfrom-zero- to-100-digits-2/ (a good series of blog posts on floats and precision) • John von Neumann’s thoughts on floats: https://library.ias.edu/files/ Prelim_Disc_Logical_Design.pdf (section 5.3; page 18)