For double inaccuracies, first I show them this code:

 

  double x = 5;

for (int  i = 1;  i <= 10; i++)

{

x +=  .1;

System.out.println(x);

}

 

/*  OUTPUT

5.1

5.199999999999999

5.299999999999999

5.399999999999999

5.499999999999998

5.599999999999998

5.6999999999999975

5.799999999999997

5.899999999999997

5.9999999999999964

*/

 

Then I explain how to convert numbers such as .1, .2, .3, ...   into

binary, using the following algorithm (say for .3):

 

1) Multiply .3 by 2.  This equals 0.6

2) Take the whole number part (0 in this case) and place it to the  right

of a decimal point.

3) Take the decimal part (.6 in this case) and multiply it by 2. This

equals 1.2.

4) Take the whole number part (1 in this case) and place it to the right of

 the 0 in step 2.

5) Take the decimal part (.2 in this case) and multiply it by 2. This

equals 0.4

etc.

Continue this until you get a repeating decimal.  For .3, you will get

.01001 with the "1001" repeating.

This shows that .3 in binary is a repeating decimal value, so stored in

finite memory leads to inaccuracies.

 

Evelyn Torres-Rangel

 

 

In a message dated 10/4/2010 9:02:34 A.M. Pacific Daylight Time,

morrison@ncssm.edu writes:

 

>  From: Jeremy Woodward [jeremy.woodward@mac.com]

> Sent: Tuesday,  September 28, 2010 11:55 PM

> To: AP Computer Science

> Subject:  [ap-compsci] Binary Numbers and Double inaccuracies...  How do

you teach  this?

>

> Just curious,

>

> Do any of you teach  converting negative integers to binary?

>

> Also what is the best  way to explain this?

>

> Why does 0.1 + 0.1 + 0.1  + 0.1 +0.1  + 0.1 + 0.1  + 0.1 + 0.1 + 0.1 =

0.999999999999999999999?

>

>  Thanks

>

>  Jeremy