Question

Here we are, facing an odd issue with a sorting of two arrays: The numbers within them are key to show off the top and bottom, max and min, of these array's in order to draw them in a line.

Here are the two arrays:

double[] hor = {5.938, 5.989, 6.047, 6.105, 6.159, 6.211, 6.260, 6.314, 6.351};
double[] vert = {-6.589,-6.348, -5.991, -5.691,- 5.491, -5.203, -4.989,- 4.805, -4.650};

aAnd the function:

public void escala(double[] hor, double[] vert) {

    double minX = Double.MAX_VALUE;
    double maxX = Double.MIN_VALUE;
    for (double x : hor) {
        if (x < minX)
            minX  = x;
        if (x > maxX)
            maxX  = x;

    }
    double minY = Double.MAX_VALUE;
    double maxY = Double.MIN_VALUE;
    for (double y : vert) {
        if (y < minY)
            minY  = y;
        if (y > maxY)
            maxY  = y;
    }

    double margin=0.00;
    final double actualMaxY = maxY+margin*(maxY-minY);
    final double actualMinY = minY-margin*(maxY-minY);
    final double actualMaxX = maxX+margin*(maxX-minX);
    final double actualMinX = minX-margin*(maxX-minX);

    System.err.println("actualMinY==" + actualMinY);
    System.err.println("actualMaxY==" + actualMaxY);

    cgraf.setLimits(actualMinX, actualMaxX, actualMinY, actualMaxY, (maxX-minX)/5, (maxY-minY)/10);
    ccur.setLimits(actualMinX, actualMaxX, actualMinY, actualMaxY, (maxX-minX)/5, (maxY-minY)/10);

}

And the headache of the result is this:

  • Max value = 0.000 (Of course, 4.9E-324)
  • Min Value = -6.589

It works properly for arrays with positive numbers. But with this negative numbers array, it crashes. Any idea on how to change the code to make it able to work?

The expected result is:

  • Max value = -4.650
  • Min Value = -6.589

This function is needed to send values to a graphics class in the applet.

Était-ce utile?

La solution

The problem is that

double maxY = Double.MIN_VALUE;

is near 0, in fact it is 4.9E-324 which is equivalent to 4.9 * Math.pow(10, -324). That's why the sorting is not working. You could try

double maxY = -Double.MAX_VALUE;

instead.

Double.MIN_VALUE: A constant holding the smallest positive nonzero value of type double, 2^(-1074).

Note: You may want to change maxX too.

Edit: You may also find interesting this: Double.NEGATIVE_INFINITY.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top