Java has -0.0 and 0.0? It is so confusing!


  • 1
    H

    when I remove if(p1.y==p2.y) slope=0; I will get the wrong answer, because 0.0 and -0.0 are two different keys in the hashmap.

    public int maxPoints(Point[] points) {
            if(points.length==0) return 0;
            int result=0;
            for(int i=0;i<points.length;i++)
            {
                Point p1=points[i];
                int max=0;
                int same=1;// the number of points that is same as p1.(p1 is same as p1)
                HashMap<Double,Integer> map=new HashMap<Double,Integer>();
                for(int j=i+1;j<points.length;j++)
                {
                    Point p2=points[j];
                    if(p1.x!=p2.x||p1.y!=p2.y)//p1 and p2 are not the same point
                    {
                        double slope=Double.MAX_VALUE;
                        if(p1.x!=p2.x)
                        {
                            if(p1.y==p2.y) slope=0;// I cannot believe Java have -0.0 and 0.0, and they are not the same key!!!!
                            else slope=((p2.y-p1.y)*1.0)/(p2.x-p1.x);
                           
                        }
                        if(map.containsKey(slope))
                        {
                            map.put(slope,map.get(slope)+1);
                        }
                        else
                        {
                            map.put(slope,1);
                        }
                        max=Math.max(max,map.get(slope));
                    }
                    else// p2 is same as p1
                    {
                        same++;
                    }
                }
                result=Math.max(result,max+same);
            }
            return result;
        }

  • 0
    Z

    yes, I meet the same situation. My code:

    public int maxPoints(Point[] points) {
    	int max=0;
    	for (int i=0; i<points.length; i++) {
    		Map<Double, Integer> slope = new HashMap<Double, Integer>();
    		int duplicate = 1;
    		int vertical = 0;
    		int maxLocal = 0;
        	for (int j=i+1; j<points.length; j++) {
        		if(points[i].x==points[j].x&&points[i].y==points[j].y) 
        			duplicate++;
        		else if (points[i].x==points[j].x) {
        			vertical++;
        		} else {
        			double slo = 1.0*(points[j].y-points[i].y)/(points[j].x-points[i].x);
        			if (slope.get(slo)==null)
        				slope.put(slo, 1);
        			else {
        				int count = slope.get(slo);
        				slope.put(slo, count+1);
        			}
        			if (slope.get(slo)>maxLocal)
        				maxLocal = slope.get(slo);
        		}
        	}
        	int tmp = duplicate+Math.max(maxLocal, vertical);
        	if (tmp>max)
        		max = tmp;
    	}
        return max;
    }
    

    It failed when handle "{new Point(2,3), new Point(3,3), new Point(-5,3)};"
    because 1 is considered 0.0 and 2 are considered -0.0

    I don't know why java exists float -0.0. Is it necessary?


  • 0
    Y

    After

    slope=((p2.y-p1.y)*1.0)/(p2.x-p1.x);
    

    add

    slope += 0.0;
    

    can solve your problem.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.