Because decimal-based floating point numbers have similar problems,
but with different numbers.
For example 1.0/3.0 == 0.3333...
without end.
All floating point schemes have similar problems.
Since binary has so many other advantages,
most computers use it.
Electronic calculators sometimes use hardware decimal-based
calculations since they always
present their results to humans.
Also, mainframe computers usually include hardware decimal-based
instructions for use in financial calculations.
(The rest of this chapter may be safely skipped. You may wish to do this if you have been ignoring graphics.)
Let us graph the sine of x
, for x
in the range
of 0.0 to 2 PI radians.
Here is a start on doing this.
The value of PI is part of the class Math
.
You get it with Math.PI
.
To get accurate results,
let us use an increment of a negative power of two.
Each iteration of the loop
computes two points on the
curve sin(x)
and connects them with a short line.
If there are many short lines the result looks like a smooth curve.
The parameters for drawLine()
must be integer values, however.
This is a problem.
We want to graph sin(x) for 0 <= x <= 2*Math.PI
.
Say that the drawing area (of the JPanel
is 600 pixels wide.)
What horizontal coordinate of the panel will correspond to x=0?
What horizontal coordinate of the panel will correspond to x=2*Math.PI?