Why is one of the test case so annoying?


  • 2

    0_1501735549057_2aac80da-23ca-49d9-85f1-f934c1908e5d-image.png

    I've tried calculating the slopes and count them using map, but was failing a test case all the time. No matter how many digits I set for the float it just seemed that 94911150/94911151 equals 94911151/94911152.

    ???!

    0_1501735572971_119030a3-7f7e-40fb-99e0-0307d01bd99b-image.png

    It was crazy. Anyone could help me?


  • 0

    i did something like:

    double slope = 10000000000000.0 * double(points[i].y - points[j].y) / double(points[i].x - points[j].x);
    

    seems work, could pass the test case but might cause some overflow problem in the future...
    actually 10.0 is enough:

    double slope= 10.0*(points[i].y-points[j].y)/(points[i].x-points[j].x);

  • 0
    Y

    @shenjunsu That works. I was trying to use BigDecimal. Does not work out a solution.


  • 0

    @shenjunsu That's just luck. It fails other cases. For example 10000000000000.0 * 61842131 / 61842132 == 10000000000000.0 * 61842132 / 61842133 and 10.0 * 75037253 / 75037254 == 10.0 * 75037254 / 75037255.

    @KylinZenda Those two values, as computed by floats, actually are equal. They're both exactly 0.999999989463830285529866159777157008647918701171875. Try it with "{0:.70f}" and you'll see there are only zeros after that.


Log in to reply
 

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