Text
I recently came upon a very cool Python module: uncertainties (PyPI entry.)
What this package does is allow you to speak about numbers while taking into account any possible errors that these numbers contain.
For example, let’s say you are buying a few dozen bottles of beer for a party. You go to the grocery store, where you are happy to see a variety of different beers of different styles, light, dark, lager, stout, etc. You take a variety of beer bottles to your shopping cart, so now your cart is full of different kinds of beer bottles, which have different prices. (You take single bottles because the six-packs are unfortunately stored outside of the refrigerator.)
Now, you want to know what the total price of the beer collection is going to be, because you have a limited beer budget for this party. And just to annoy you, the beers have prices like $1.85. (You secretly rejoice in the fact that marketers have yet to discover irrational and/or transcendental numbers.) How can you quickly approximate the total price?
You look at the cart and see that the price for most of your beers is around $1.50. Some are $1.85, some are $1.15, some are $0.90, some are $1.70, so you can say they are $1.50, give or take $0.50.
Now you want to know how many bottles you have. You take a quick glance and see you have about 30 bottles, give or take 5 bottles.
Now, how would you calculate how much the whole cart would cost? uncertainties to the rescue:
>>> from uncertainties import ufloat
>>> price_of_beer = ufloat('1.50 +/- 0.50')
>>> number_of_bottles = ufloat('30 +/- 5')
>>> total_price = price_of_beer * number_of_bottles
>>> total_price
45.0+/-16.770509831248422
So the total price is $45, give or take $17. Thanks uncertainties!
(P.S., I am getting rumors that the uncertainties module can also be used in serious calculations in statistics; I have yet to confirm this.)