Posts Tagged ‘arithmetic’

Some wtf Python float arithmetic

Friday, October 8th, 2010

Prof. Dominic Lemelin is about to try to prove that 7n + 2 is a multiple of three for all n in math class at the moment, and before I tried proving it myself I decided to do a few tests in Python to make sure I wasn’t wasting my time. It was doing pretty well until it 7365 + 2 caused it to overflow, but there was some weird stuff going on in the float arithmetic. I don’t know why I was using float arithmetic, maybe it’s still too early to be programming.

>>> x = 11398895185373145
>>> x/3
3799631728457715
>>> float(x)/3
3799631728457714.5
>>> wtf()

Update: By the way, x/3 actually gives the right answer, 3799631728457715. This is a key piece of information in recognizing that I’m not just an idiot who forgot that integer division rounds in Python.

Tetration: The mathematical operator that felt kind of left out in High School.

Tuesday, September 8th, 2009

When we are taught arithmetic in Elementary School, we always cover four distinct operators. They are addition, subtraction, multiplication and division. Immediately, we begin to find connections between them. The realizations that subtraction is the inverse of addition and division is the inverse of multiplication, that multiplication is no more than repeated recursive addition, and that the whole lot of them can be reproduced as an addition-based algorithm may not have had you wide-eyed and stuttering. You might however, have experienced a warm and fuzzy feeling that everything is somehow linked together in a rather special way, perhaps similar that of a moon landing conspiracy theorist reading some good news from an extremely credible source.

In High School, where as I remember we learned about exponents and roots, these relationships are extended further. Of course, exponentiation is no more than repeated self-multiplication, and roots are the inverse of powers. However, algebraically the exploration of these new operators is rather limited. Perhaps for purposes of practicality, you would never see anything like x41 in your maths textbook. For x=13, the answer is 4.69×1045, more than any real-world application would require. So, exponents were rather limited to squares and cubes, even tesseracts if you were lucky.

Maybe that’s why we were never even told that the ladder continues. Maybe you already know what the tetration operator is, but if you do I bet that you went through the bother of finding out about it yourself. Also called the “hyper-4 operator”, it is the fourth in this ladder of complexity: simply repeated self-exponentiation. It’s common notation is similar to that of exponentiation, but with the superscript on the other side of the number, i.e. 42. This would mean “2 to the power of itself four times” or 2^(2^(2^2)). The numerical equivalent is 65536.

Edit: changed the example, thanks Jeff!

Wouldn’t this have been nice to know while sitting in a stuffy math class producing iteration upon iteration of meaningless answers to even more meaningless questions in a machine-like fashion? Technically it was math, but I’ll be damned if you were actually doing any thinking. You could have had bigger and better things to wonder about and put your mind to.

Obviously, this relatively unknown operator is not useful for many practical reasons; it’s output becomes too large after the first few integers, and not many things in the real world have any relation to it. But it is quite an interesting concept to play around with. For example you might ask yourself, what happens when we use it with negative numbers? And what is its inverse function? If 2x = 10, what is x?

And of course, now that you’ve seen 4 of these weird things, it’s blindingly obvious that there must be more out there. You can figure it out for yourself, there is a hyper-5 operator. And a hyper-6. A hyper-nine-million, in fact. Hell, you could even say that the hyper-hyper-1(x,y) operator was hyper-x(y,y) applied y times, and hyper-hyper-n was recursively repeated hyper-hyper-(n-1), the nth function generated by that good ol’ pattern from above. Or something wonderful like that.

Here’s to showing kids that there is a brighter mathematical world out there, just waiting to be discovered, instead of teaching them only the blandest of basics because we’re afraid we might confuse them a little. I say we get them to think for themselves, get them to be mystified, to wonder about things. ‘Cause really, nobody “wonders”, nobody even gives a fuck how many apples Sally will have after received a load from Dick and gave a bunch to Bill.