Floating point accuracy

Hải Phong

I am writing a method which calculates the equation of a 2D Line in the form a*x+b*y=1

//Given two points, find the equation of the line by solving two linear equations and then test the result. (For simplicity, assume that delta !=0 here)

private boolean solveAndRetry(float x1,float y1, float x2,float y2) {
        float delta = x1 * y2 - x2 * y1;
        float deltaA = y2 - y1;
        float deltaB = x1 - x2;

        float a = deltaA / delta;
        float b = deltaB / delta;
        float c = 1;

        //test
        if (a * x2 + b * y2 == c) {
        System.out.println("ok");
            return true;
        }
        else {
            System.out.println(a * x2 + b * y2-c);
            return false;
        }
    }

When I ran it, I was expecting there would be all "ok"s, but that's not the case and I don't know why

public static void main(String[] args) {
        for (float x = 0; x < 10; x += 0.01f) {
            solveAndRetry(1, -1, x, 2);
        }
    }

Here are some lines in the result

ok
ok
ok
ok
ok
ok
ok
ok
-5.9604645E-8
ok
-5.9604645E-8
ok
ok
ok
1.1920929E-7
ok
ok
-5.9604645E-8
ok
Henry

A float has an accuracy of 6 to 7 decimal digits. Since rounding errors cannot be avoided, your results are as good as it can get.

Normally, you would never compare floating point numbers for equality. Instead of x == y always use a comparison with an interval:

Math.abs(x - y) < eps

for a suitably chosen eps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Find floating point accuracy

Managing floating point accuracy

Floating point accuracy with different languages

Elixir floating point division accuracy

floating point accuracy in MIPS assembly

Groovy vs Java - difference in floating point accuracy

how set numpy floating point accuracy?

Difference between double and float in floating point accuracy

comparing accuracy for calculating floating point with x*0.1 and x/10.0

Computing floating point accuracy (K&R 2-1)

How to increase accuracy of floating point second derivative calculation?

How to change the accuracy of a floating point number in a cell in QTableWidget?

Best possible accuracy for single precision floating point division

Strange issue with floating point accuracy on ARM64

Accuracy of floating point calculations: is something converting rational -> double-float?

'Find' function working incorrectly, have tried floating point accuracy resolution

Preserving Accuracy When Formatting a Floating Point Number to Display With Two-point Precison (Java)

How do I print a floating-point value for later scanning with perfect accuracy?

The calculation accuracy of floating-point numbers (float, double) in Java (IEEE 754)

Is floating point arithmetic stable?

Converting fractions to floating point

Floating point overflow and inexactness

Floating point to binary conversion

Floating point division in Bash

Floating point comparison in shell

Floating point precision golang

precision of floating point in tensorflow

Floating point performance in Linux

Alternative to floating point as key