Pay no attention to that man behind the curtain.♭We look at a simple Ox program that poses a puzzle about how computers do math. The answer to the puzzle must wait for a later chapter, but the puzzle may motivate you to learn about how computers works before trusting them with your own research.
--The Wizard of Oz
08-If6was9.ox
1: #include "oxstd.h"
2: main() {
3: decl x;
4: x = 0.25;
5: print( "Is 2.5=10*(0.25)? ");
6: // 0 1 2 3 4 5 6 7 8 9 check that there are 10 x's
7: if (2.5== x+x+x+x+x+x+x+x+x+x)
8: println("Yes");
9: else
10: println("No");
11: x = 0.1;
12: print( "Is 1.0=10*(0.10)? ");
13: if (1.0== x+x+x+x+x+x+x+x+x+x )
14: println("Yes");
15: else
16: println("No");
17: }
x
. Yesor
Nobased on the comparison of the two expressions.
Is 2.5=10*(0.25)? Yes Is 1.0=10*(0.10)? No
… If you can explain the output you might read the next few chapters quickly. If you cannot explain the output, then as a student of economics you might thinking,
Perhaps I cannot trust Ox to do arithmetic.After all, if you cannot rely on adding x to itself 10 times always being the same as 10x, for all values x, then how can we rely on it to, say, find a consumption bundle at which marginal utility equals the slope of the budget line?
The Pentium Bug
The famous case of a real problem with computer arithmetic is the Intel Pentium Bug where about once every 9 billion divisions were incorrect.. scalar x = 0.25 . di 2.5==x+x+x+x+x+x+x+x+x+x 1 . sca x = 0.1 . di 1.0==x+x+x+x+x+x+x+x+x+x 0
1
and 0
are equivalent to yesand
no.
Symbolic Math
Mathematica and Maple are designed to carry out symbolic operations, so this question would not apply to them, at least when they are in symbolic mode.)09-If6was9reprise.ox
1: #include "oxstd.h"
2: main() {
3: decl x,x10;
4: println("\n At ? type a decimal number x and 10x then hit ENTER.\n",
5: "Example:\n? 2 20\n\nEnter 0 0 to exit");
6: do {
7: scan("\n? %f %f",&x,&x10);
8: if (x==0) break;
9: print("Is ",x10," = 10(",x,")?\n",
10: x10==x+x+x+x+x+x+x+x+x+x ? "Yes!"
11: : "No!",
12: "\n---------------");
13: } while (TRUE);
14: }
15:
08-If6was9.ox
and in fewer lines. It uses an iterative statement (do { } while()
) to repeat a statement (or block of statements) more than once, which eliminates the need to have almost duplicate lines. And it uses the conditional statement A ? B : C
to check a condition (A) and return different values (B and C) depending on whether the condition is true or false.