0

im trying to program a little java program, that finds null points in a given function f(x).

This is my idea:

I have X1 and X2 (User input) to define an area where to search for the null point. For example -5 and 5.

private static double Naehern(double X, double X2) {
    double Y = -1;
    double Step = 0.01;

    while(Y < -0.00001 || Y > 0.00001) {
        X = X + Step;
        Y = f(X);

        if(X >= X2) {
            break;
        }
    }

    return X;
}

static double f(double x) {
    return x * x;
}

I am trying to find a Point X, between X1 and X2, where our null point is with an accuracy of about 5. So, in my while() i am starting at X1 and keep checking for X1 while adding +0,01 to it. At some point it should find a null point.

Problems in my current idea:

When starting with -5 for X1 and 5 for X2, the following happens:

X: -4,99
X: -4,98
X: -4,9700000000000000001

How can i fix this by still using doubles?

Secondly i am getting this number at the end of the program: -6.230779781013496E-14. This is the found null point at X = -6.230779781013496E-14. How can I convert this number into something like a normal-readable double with 5 characters after the "."?

Bobulous
  • 12,967
  • 4
  • 37
  • 68
  • 1
    floats and doubles are always just approximations of values. They are useful for scientific and engineering computations, but for math and finance not so much. If you know that you are going to be doing calculations that you wish to be exact and with a known fixed precision, then that is a great time to consider using BigDecimal instead of double. – scottb Nov 24 '13 at 16:12
  • I second the advice of scottb. Where numerical precision is important you really need to learn how to use BigDecimal. – Bobulous Nov 24 '13 at 16:34

2 Answers2

0

a) you can't, unless you use a step width that is exactly representable in binary, like 1/128 instead of 1/100 (which is not).

b) You can use printf("%.5f", ...), though in your case, the output would be just 0.00000.

Ingo
  • 36,037
  • 5
  • 53
  • 100
  • Yes, i know how to format it, i could also use Math.round(X*10000) / 10000, but in that case it would only resume in 0.0. My problem is: HOW can i stop the program from calculating those super small numbers? I just want something like X = 0.00034 and not something like 0.0000000000000000000000000000000000000000253. How? I tried that by using while(Y < -0.00001 || Y > 0.00001). – user2999582 Nov 24 '13 at 16:41
  • @user2999582 You **can't** stop it. This is how IEEE floating point works. Use BigDecimal. – Radiodef Nov 24 '13 at 16:49
  • @user2999582 Radiodef is right in principle, but observe what I said in point a). If you choose numbers that have exact representation, also sums and differences of such numbers will be exact. So, if you start out with 5.0 and step with 0.0078125 (1 / 128) you will not encounter those "super small numbers". – Ingo Nov 25 '13 at 10:06
0

You can use System.out.printf, String.format or NumberFormat class to format numbers.

Floats and doubles are not exact values. If you want higher precision, there are better ways than float/double. Yes, BigDecimal, I am looking at you.

Silviu Burcea
  • 5,103
  • 1
  • 29
  • 43