Outside of the realm of graphic interfaces or databases, a major field in cs is the science of computation. This is your typical "number cruncher" that calculates pi to the 3 billionth digit. Inevitably, big numbers are involved. Or sometimes, extremely small numbers. But of course, large or small, any number can be concisely represented in scientific notation. 30000 and 0.00003 in scientific notation only differ by a sign change: 3e5 vs 3e-5. But what if you want to store 1/3 in a variable? 1/3 = 0.333333... You can't type infinite digits can you? But being an eager cs student you immediately look for a workaround. Okay, I'll just type x = 1./3. And typing 3 * x in the shell gives me 1.0. Tada, I made my program store infinite decimals.
Sadly, that is not that case. Here's an example to try. Type in the shell 1e-20 + 1 - 1. From inspection we see trivially that the answer is 1e-20, yet our program doesn't give us that answer. It gave us 0.0! How can this be? Next, try 1e-20 + (1 - 1). What do you see? Next, try lowering the number 20 until you see "proper" results.
>>> 1e-15 + 1 - 1
1.1102230246251565e-15
That certainly is strange isn't it?
No comments:
Post a Comment