@@ -69,37 +69,57 @@ operation.
6969 The result will be of the type of the LHS.
7070
7171*Multiplication *
72- Supported, but yields strange results.
72+ Supported, but yields results which may not be intuitive. The math is
73+ performed at the byte-level (ignoring prefix units). The result is in the
74+ unit of the LHS. Technically speaking, if the LHS of the equation is a byte
75+ unit, then the result should be squared. You are advised to call the
76+ :py:meth: `best_prefix ` method on the result to get a useful value back.
7377
7478.. code-block :: python
75- :linenos:
76- :emphasize- lines: 6 ,9
7779
78- In [10 ]: first = MiB(5 )
80+ >> > bitmath.kB(3 ).bytes, bitmath.MiB(5 ).bytes
81+ (3000.0 , 5242880.0 )
82+ >> > bitmath.best_prefix(3000 * 5242880 )
83+ GiB(14.6484375 )
7984
80- In [11 ]: second = kB(2 )
85+ >> > bitmath.Byte(3000 ) * bitmath.MiB(5 )
86+ B(15728640000.0 )
8187
82- In [ 12 ]: first * second
83- Out[ 12 ]: MiB( 10000.0 )
88+ >> > (bitmath.Byte( 3000 ) * bitmath.MiB( 5 )).best_prefix()
89+ GiB( 14.6484375 )
8490
85- In [ 13 ]: (first * second).best_prefix()
86- Out[ 13 ]: GiB( 9.765625 )
91+ The final result represents how many “GiB-sized” chunks of bytes are in that
92+ total. It's weird, but it works.
8793
88- As we can see on lines **6 ** and **9 **, multiplying even two
89- relatively small quantities together (``MiB(5) `` and ``kB(2) ``) yields
90- quite large results.
94+ If the LHS is larger than a byte prefix unit then you get a result matching the
95+ unit of the LHS of the equation.
9196
92- Internally, this is implemented as:
93-
94- .. math ::
97+ .. code-block :: python
9598
96- (5 \cdot 2 ^{20 }) \cdot (2 \cdot 10 ^{3 }) = 10 ,485 ,760 ,000 B
99+ >> > bitmath.MiB(5 ) * bitmath.kB(3 )
100+ MiB(15000.0 )
101+ # LHS was MiB, result is MiB
97102
98- 10 ,485 ,760 ,000 B \cdot \dfrac {1 MiB}{1 ,048 ,576 B} = 10 ,000 MiB
103+ # And if we wrap it with best_prefix() we get the earlier result back
104+ >> > (bitmath.MiB(5 ) * bitmath.kB(3 )).best_prefix()
105+ GiB(14.6484375 )
99106
100107 *Division *
101108 The result will be a number type due to unit cancellation.
102109
110+ .. code-block :: python
111+
112+ >> > bitmath.kB(3 ) / bitmath.MiB(5 )
113+ 0.00057220458984375
114+
115+ >> > bitmath.kB(3 ).bytes / bitmath.MiB(5 ).bytes
116+ 0.00057220458984375
117+
118+ Above you can see that the math is performed on the bytes of each operand. As
119+ noted above, the units cancel out. This is the opposite of the multiplication
120+ case where the units square together if you do not coerce them into a larger
121+ prefix unit.
122+
103123.. _appendix_math_mixed_types :
104124
105125Mixed Types: Addition and Subtraction
@@ -123,7 +143,7 @@ function works correctly with iterables of bitmath objects, since
123143 >> > sum ([bitmath.Byte(1 ), bitmath.MiB(1 ), bitmath.GiB(1 )])
124144 Byte(1074790401.0 )
125145
126- For all non-zero numeric operands the behaviour (returning a number)
146+ For all non-zero numeric operands the behavior (returning a number)
127147applies.
128148
129149**Discussion: ** Why do ``100 - KiB(90) `` and ``KiB(100) - 90 `` both
@@ -251,7 +271,25 @@ yourself what you would expect to get if you did this:
251271
252272.. math ::
253273
254- \dfrac {100 }{kB(33 )} = x
274+ \dfrac {100 }{kB(33 )}
275+
276+ Unless you're representing rates that doesn't mean much at all. The units of
277+ operands when expressing rates is going to be context sensitive and can be very
278+ non-intuitive without additional knowledge. For example:
279+
280+ .. code-block :: python
281+
282+ >> > 100 / bitmath.kB(33 )
283+ 3.0303030303030303
284+
285+ This is functionally equivalent to writing:
286+
287+ .. math ::
288+
289+ \dfrac {1 }{kB(33 )} \cdot 100
290+
291+ This might mean something to you, but we can't express that as a prefix unit. We
292+ let you do it, but it is up to you to determine the significance of the result.
255293
256294
257295
@@ -326,6 +364,6 @@ Footnotes
326364 <https://www.programiz.com/python-programming/precedence-associativity> `_
327365
328366 .. [#datamodel ] `Python Datamodel Customization Methods
329- <https://docs.python.org/2.7 /reference/datamodel.html#basic-customization> `_
367+ <https://docs.python.org/3 /reference/datamodel.html#basic-customization> `_
330368
331369 .. [#significance ] https://en.wikipedia.org/wiki/Significance_arithmetic
0 commit comments