Example usage for java.lang Math nextAfter

List of usage examples for java.lang Math nextAfter

Introduction

In this page you can find the example usage for java.lang Math nextAfter.

Prototype

public static float nextAfter(float start, double direction) 

Source Link

Document

Returns the floating-point number adjacent to the first argument in the direction of the second argument.

Usage

From source file:FloastPointDemo.java

public static void main(String[] args) {
    // Returns the lesser adjacent of a double
    double lesserAdjacent = Math.nextAfter(123.0, 120.0);
    System.out.println("Math.nextAfter (123.0, 120.0) = " + lesserAdjacent);
}

From source file:Main.java

public static void main(String[] args) {
    double x = 123456.7;
    double y = -123.45;

    // print the next number for x towards y
    System.out.println("Math.nextAfter(" + x + "," + y + ")=" + Math.nextAfter(x, y));

    // print the next number for y towards x
    System.out.println("Math.nextAfter(" + y + "," + x + ")=" + Math.nextAfter(y, x));

}

From source file:Main.java

public static void main(String[] args) {

    float x = 12345.123f;
    double y = 123.12345;

    // print the next number for x towards y
    System.out.println("Math.nextAfter(" + x + "," + y + ")=" + Math.nextAfter(x, y));

}

From source file:org.asoem.greyfish.core.space.WalledPointSpace.java

/**
 * Checks if the line {@code xo, yo, xd, yd} crosses an edge of the {@code tile} or any adjacent tile in the
 * direction of movement which has a wall present. If such a crossing is found, than the point closest to this
 * crossing is returned, {@code null}, otherwise.
 *
 * @param tile the tile to check for a collision
 * @param xo   Movement line x origin/*from   ww  w  .  j av a  2  s . c  o m*/
 * @param yo   Movement line y origin
 * @param xd   Movement line x destination
 * @param yd   Movement line x destination
 * @return the location on the line closest to the point of a collision with a wall or {@code null} if none could be
 * found
 */
@Nullable
private Point2D collision(final WalledTile tile, final double xo, final double yo, final double xd,
        final double yd) {
    assert tile != null;

    if (tile.covers(xd, yd)) {
        return null;
    }

    TileDirection follow1 = null;
    TileDirection follow2 = null;

    if (yd < yo) { // north
        final ImmutablePoint2D intersection = intersection(tile.getX(), tile.getY(),
                Math.nextAfter(tile.getX() + 1.0, -Double.MIN_VALUE), tile.getY(), xo, yo, xd, yd);

        if (intersection != null) {
            if (tile.hasWall(TileDirection.NORTH)) {
                return intersection;
            } else {
                follow1 = TileDirection.NORTH;
            }
        }
    }

    if (xd > xo) { // east
        final ImmutablePoint2D intersection = intersection(Math.nextAfter(tile.getX() + 1.0, -Double.MIN_VALUE),
                tile.getY(), Math.nextAfter(tile.getX() + 1.0, -Double.MIN_VALUE),
                Math.nextAfter(tile.getY() + 1.0, -Double.MIN_VALUE), xo, yo, xd, yd);

        if (intersection != null) {
            if (tile.hasWall(TileDirection.EAST)) {
                return intersection;
            } else {
                if (follow1 == null) {
                    follow1 = TileDirection.EAST;
                } else {
                    follow2 = TileDirection.EAST;
                }
            }
        }
    }

    if (yd > yo) { // south
        final ImmutablePoint2D intersection = intersection(tile.getX(),
                Math.nextAfter(tile.getY() + 1.0, -Double.MIN_VALUE),
                Math.nextAfter(tile.getX() + 1.0, -Double.MIN_VALUE),
                Math.nextAfter(tile.getY() + 1.0, -Double.MIN_VALUE), xo, yo, xd, yd);

        if (intersection != null) {
            if (tile.hasWall(TileDirection.SOUTH)) {
                return intersection;
            } else {
                if (follow1 == null) {
                    follow1 = TileDirection.SOUTH;
                } else {
                    follow2 = TileDirection.SOUTH;
                }
            }
        }
    }

    if (xd < xo) { // west
        final ImmutablePoint2D intersection = intersection(tile.getX(),
                Math.nextAfter(tile.getY() + 1.0, -Double.MIN_VALUE), tile.getX(), tile.getY(), xo, yo, xd, yd);

        if (intersection != null) {
            if (tile.hasWall(TileDirection.WEST)) {
                return intersection;
            } else {
                if (follow1 == null) {
                    follow1 = TileDirection.WEST;
                } else {
                    follow2 = TileDirection.WEST;
                }
            }
        }
    }

    if (follow1 != null && hasAdjacentTile(tile, follow1)) {
        final Point2D collision = collision(getAdjacentTile(tile, follow1), xo, yo, xd, yd);
        if (collision != null) {
            return collision;
        } else if (follow2 != null && hasAdjacentTile(tile, follow2)) {
            final Point2D collision1 = collision(getAdjacentTile(tile, follow2), xo, yo, xd, yd);
            if (collision1 != null) {
                return collision1;
            }
        }
    }

    return null;
}

From source file:org.streaminer.stream.histogram.spdt.Histogram.java

/**
 * Returns a <code>SumResult</code> object which contains the
 * density estimate at the point <code>p</code> along
 * with the density for the targets./*  w  w w . j a  va2  s.  c  om*/
 *
 * @param p the density estimate point
 */
public SumResult<T> extendedDensity(double p) {
    T emptyTarget = (T) _bins.first().getTarget().init();
    double countDensity;
    T targetDensity;

    Bin<T> exact = _bins.get(p);
    if (p < _minimum || p > _maximum) {
        countDensity = 0;
        targetDensity = (T) emptyTarget.clone();
    } else if (p == _minimum && p == _maximum) {
        countDensity = Double.POSITIVE_INFINITY;
        targetDensity = emptyTarget;
    } else if (exact != null) {
        double higher = Math.nextAfter(p, Double.POSITIVE_INFINITY);
        double lower = Math.nextAfter(p, Double.NEGATIVE_INFINITY);

        SumResult<T> lowerResult = extendedDensity(lower);
        SumResult<T> higherResult = extendedDensity(higher);
        countDensity = (lowerResult.getCount() + higherResult.getCount()) / 2;
        targetDensity = (T) lowerResult.getTargetSum().clone().sum(higherResult.getTargetSum()).mult(0.5);
    } else {
        Bin<T> lowerBin = _bins.lower(p);
        if (lowerBin == null) {
            lowerBin = new Bin(_minimum, 0, emptyTarget.clone());
        }

        Bin<T> higherBin = _bins.higher(p);
        if (higherBin == null) {
            higherBin = new Bin(_maximum, 0, emptyTarget.clone());
        }

        double bDiff = p - lowerBin.getMean();
        double pDiff = higherBin.getMean() - lowerBin.getMean();
        double bpRatio = bDiff / pDiff;

        NumericTarget countTarget = (NumericTarget) computeDensity(bpRatio, lowerBin.getMean(),
                higherBin.getMean(), new NumericTarget(lowerBin.getCount()),
                new NumericTarget(higherBin.getCount()));
        countDensity = countTarget.getSum();

        targetDensity = (T) computeDensity(bpRatio, lowerBin.getMean(), higherBin.getMean(),
                lowerBin.getTarget(), higherBin.getTarget());
    }

    return new SumResult<T>(countDensity, targetDensity);
}

From source file:com.bigml.histogram.Histogram.java

/**
 * Returns a <code>SumResult</code> object which contains the
 * density estimate at the point <code>p</code> along
 * with the density for the targets.//from  w  w w .j  av a 2  s .c o  m
 *
 * @param p the density estimate point
 */
@SuppressWarnings("unchecked")
public SumResult<T> extendedDensity(double p) {
    T emptyTarget = (T) _bins.first().getTarget().init();
    double countDensity;
    T targetDensity;

    Bin<T> exact = _bins.get(p);
    if (p < _minimum || p > _maximum) {
        countDensity = 0;
        targetDensity = (T) emptyTarget.clone();
    } else if (p == _minimum && p == _maximum) {
        countDensity = Double.POSITIVE_INFINITY;
        targetDensity = emptyTarget;
    } else if (exact != null) {
        double higher = Math.nextAfter(p, Double.POSITIVE_INFINITY);
        double lower = Math.nextAfter(p, Double.NEGATIVE_INFINITY);

        SumResult<T> lowerResult = extendedDensity(lower);
        SumResult<T> higherResult = extendedDensity(higher);
        countDensity = (lowerResult.getCount() + higherResult.getCount()) / 2;
        targetDensity = (T) lowerResult.getTargetSum().clone().sum(higherResult.getTargetSum()).mult(0.5);
    } else {
        Bin<T> lowerBin = _bins.lower(p);
        if (lowerBin == null) {
            lowerBin = new Bin<T>(_minimum, 0, (T) emptyTarget.clone());
        }

        Bin<T> higherBin = _bins.higher(p);
        if (higherBin == null) {
            higherBin = new Bin<T>(_maximum, 0, (T) emptyTarget.clone());
        }

        double bDiff = p - lowerBin.getMean();
        double pDiff = higherBin.getMean() - lowerBin.getMean();
        double bpRatio = bDiff / pDiff;

        NumericTarget countTarget = (NumericTarget) computeDensity(bpRatio, lowerBin.getMean(),
                higherBin.getMean(), new NumericTarget(lowerBin.getCount()),
                new NumericTarget(higherBin.getCount()));
        countDensity = countTarget.getSum();

        targetDensity = (T) computeDensity(bpRatio, lowerBin.getMean(), higherBin.getMean(),
                lowerBin.getTarget(), higherBin.getTarget());
    }

    return new SumResult<T>(countDensity, targetDensity);
}

From source file:org.opendatakit.services.database.utilities.ODKDatabaseImplUtils.java

public boolean identicalValue(String localValue, String serverValue, ElementDataType dt) {

    if (localValue == null && serverValue == null) {
        return true;
    } else if (localValue == null || serverValue == null) {
        return false;
    } else if (localValue.equals(serverValue)) {
        return true;
    }//from  w  w w .j  a v  a  2 s .  c  o m

    // NOT textually identical.
    //
    // Everything must be textually identical except possibly number fields
    // which may have rounding due to different database implementations,
    // data representations, and marshaling libraries.
    //
    if (dt == ElementDataType.number) {
        // !!Important!! Double.valueOf(str) handles NaN and +/-Infinity
        Double localNumber = Double.valueOf(localValue);
        Double serverNumber = Double.valueOf(serverValue);

        if (localNumber.equals(serverNumber)) {
            // simple case -- trailing zeros or string representation mix-up
            //
            return true;
        } else if (localNumber.isInfinite() && serverNumber.isInfinite()) {
            // if they are both plus or both minus infinity, we have a match
            if (Math.signum(localNumber) == Math.signum(serverNumber)) {
                return true;
            } else {
                return false;
            }
        } else if (localNumber.isNaN() || localNumber.isInfinite() || serverNumber.isNaN()
                || serverNumber.isInfinite()) {
            // one or the other is special1
            return false;
        } else {
            double localDbl = localNumber;
            double serverDbl = serverNumber;
            if (localDbl == serverDbl) {
                return true;
            }
            // OK. We have two values like 9.80 and 9.8
            // consider them equal if they are adjacent to each other.
            double localNear = localDbl;
            int idist = 0;
            int idistMax = 128;
            for (idist = 0; idist < idistMax; ++idist) {
                localNear = Math.nextAfter(localNear, serverDbl);
                if (localNear == serverDbl) {
                    break;
                }
            }
            if (idist < idistMax) {
                return true;
            }
            return false;
        }
    } else {
        // textual identity is required!
        return false;
    }
}