[plt-scheme] ProfessorJ, ==, and double
Quoth Carl Eastlund:
> The == operator doesn't operate on type double in ProfessorJ: Beginner
> in DrScheme v209. It works fine in ProfessorJ: Advanced, but we've
> needed it in our earliest examples in the HtDCH class. We can work
> around it with new Double(x).equals(new Double(y)), but yuck. Is
> there any reason for this being left out of the Beginner level?
Because floating point numbers shouldn't be tested for equality, due to
numeric error. Consider:
> double x = (1.0 / 6) * 10000;
> double y = 10000.0 / 6;
> x
1666.6666666666665
> y
1666.6666666666667
Now, there are situations once you know what you're doing, when you know
that numeric error can't arise, that you can make use of ==, but Java
beginners really shouldn't use those. The correct way to test equality
on doubles is
> double epsilon = 0.0001;
> Math.abs (x - y) < epsilon
true
It's really the same reason that you can't use == on objects in Beginner
ProfJ: it doesn't mean what they might intuitively expect it to mean, so
you don't want them using it until they understand its problems.
What "earliest examples" in HtDCH are you referring to?
--
-=-Don Blaheta-=-dblaheta at knox.edu-=-=-<http://faculty.knox.edu/dblaheta/>-=-
"I can remember when a good politician had to be 75 percent ability and
25 percent actor, but I can well see the day when the reverse could be
true." --Harry Truman