Float Equal Problem

Understand limitations of floating point representations.Never check for equality with ==. Instead, check if the difference is less than an epsilon value.

public class Line{
    static double epsilon=0.000001;
    public double slope;
    public double yintersect;

    public Line(double s, double y){
            slope=s;
            yintersect=y;
    }

    public boolean intersect(Line line2){
            return Math.abs(slope-line2.slope)>epsilon||
            Math.abs(yintersect-line2.yintersect)<epsilon;
    }
}                

  

posted @ 2014-08-04 13:13  Phoebe815  阅读(137)  评论(0编辑  收藏  举报