view flys-artifacts/src/main/java/de/intevation/flys/utils/DoubleUtil.java @ 1666:09c1292cf36d

Bugfix: #351 Modified determination of single values specified by min/max values and an interval. flys-artifacts/trunk@2874 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 30 Sep 2011 14:29:26 +0000
parents 913b52064449
children da872168a899
line wrap: on
line source
package de.intevation.flys.utils;

import java.util.Arrays;


public class DoubleUtil
{
    public static final double DEFAULT_STEP_PRECISION = 1e6;

    private DoubleUtil() {
    }

    public static final double [] explode(double from, double to, double step) {
        return explode(from, to, step, DEFAULT_STEP_PRECISION);
    }

    public static final double round(double x, double precision) {
        return Math.round(x * precision)/precision;
    }

    public static final double round(double x) {
        return Math.round(x * DEFAULT_STEP_PRECISION)/DEFAULT_STEP_PRECISION;
    }

    public static final double [] explode(
        double from,
        double to,
        double step,
        double precision
    ) {
        double lower = from;

        double diff = to - from;
        double tmp  = diff / step;
        int    num = (int)Math.abs(Math.ceil(tmp)) + 1;

        double [] values = new double[num];

        if (from > to) {
            step = -step;
        }

        double max = Math.max(from, to);

        for (int idx = 0; idx < num; idx++) {
            if (lower > max) {
                return Arrays.copyOfRange(values, 0, idx);
            }

            values[idx] = round(lower, precision);
            lower      += step;
        }

        return values;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org