Java Number Range Create range(double a, double b, double step)

Here you can find the source of range(double a, double b, double step)

Description

Generates an array going for a to b with steps of size step.

License

Open Source License

Parameter

Parameter Description
step size of steps, must be positive.
a first value of array.
b last value of array.

Exception

Parameter Description
IllegalArgumentException if step is negative of if a=b.

Declaration

public static double[] range(double a, double b, double step) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//www .  ja  v a  2s  .co  m
     * Generates an array going for a to b
     * with steps of size step. If it can't
     * get to the value b in a finite number
     * of steps, it gets as close as possible
     * (a can be larger or smaller than b)
     *
     * @param step size of steps, must be positive.
     * @param a    first value of array.
     * @param b    last value of array.
     * @throws IllegalArgumentException if step is negative of if a=b.
     */
    public static double[] range(double a, double b, double step) {
        if (step <= 0.0) {
            throw new IllegalArgumentException("The argument step should be positive: " + step + " < 0");
        }
        if (a == b) {
            double[] ans = new double[1];
            ans[0] = a;
            return (ans);
        }
        int sizeOfArray = (new Double(Math.abs(a - b) / step)).intValue() + 1;
        double[] ans = new double[sizeOfArray];
        ans[0] = a;
        if (a > b) {
            step = -step;
        }
        for (int k = 1; k < sizeOfArray; k++) {
            ans[k] = ans[k - 1] + step;
        }
        return (ans);
    }

    /**
     * Generates an array going for a to b
     * inclusively with steps of size 1. a can be
     * smaller or larger than b.
     */
    public static double[] range(double a, double b) {
        return (range(a, b, 1.0));
    }

    /**
     * returns the difference between the max and the min element
     */
    public static double range(double[] population) {
        double min = Double.POSITIVE_INFINITY;
        double max = Double.NEGATIVE_INFINITY;
        for (int i = 0; i < population.length; i++) {
            if (population[i] < min)
                min = population[i];
            if (population[i] > max)
                max = population[i];
        }
        return max - min;
    }

    /**
     * Generates an array going for 0 to b
     * with steps of size 1. 0 can be
     * smaller or larger than b.
     */
    public static double[] range(double b) {
        return (range(0, b));
    }

    /**
     * Generates an array going for a to b
     * with steps of size 1. a can be
     * smaller or larger than b.
     */
    public static int[] range(int a, int b) {
        return (range(a, b, 1));
    }

    /**
     * Generates an array going for 0 to b
     * with steps of size 1. 0 can be
     * smaller or larger than b.
     */
    public static int[] range(int b) {
        return (range(0, b));
    }

    /**
     * Generates an array going for a to b
     * with steps of size step. If it can't
     * get to the value b in a finite number
     * of steps, it gets as close as possible
     * (a can be larger or smaller than b)
     *
     * @param step size of steps, must be positive
     * @param a    first value of array
     * @param b    last value of array
     * @throws IllegalArgumentException if step is
     *                                  negative or if a=b.
     */
    public static int[] range(int a, int b, int step) {
        if (step <= 0) {
            throw new IllegalArgumentException("The argument step should be positive: " + step + " < 0");
        }
        if (a == b) {
            int[] ans = new int[1];
            ans[0] = a;
            return (ans);
        }
        int sizeOfArray = (new Double(Math.abs(a - b) / step)).intValue();
        int[] ans = new int[sizeOfArray];
        ans[0] = a;
        if (a > b) {
            step = -step;
        }
        for (int k = 1; k < sizeOfArray; k++) {
            ans[k] = ans[k - 1] + step;
        }
        return (ans);
    }

    /**
     * Takes the absolute value of each component of an array.
     */
    public static double[] abs(double[] v) {
        double[] ans = new double[v.length];
        for (int i = 0; i < v.length; i++) {
            ans[i] = Math.abs(v[i]);
        }
        return (ans);
    }

    /**
     * Takes the absolute value of each component of an array.
     */
    public static double[][] abs(double[][] v) {
        double[][] ans = new double[v.length][];
        for (int i = 0; i < v.length; i++) {
            ans[i] = abs(v[i]);
        }
        return (ans);
    }

    /**
     * Takes the absolute value of each component of an array.
     */
    public static int[] abs(int[] v) {
        int[] ans = new int[v.length];
        for (int i = 0; i < v.length; i++) {
            ans[i] = Math.abs(v[i]);
        }
        return (ans);
    }

    /**
     * Takes the absolute value of each component of an array.
     */
    public static int[][] abs(int[][] v) {
        int[][] ans = new int[v.length][];
        for (int i = 0; i < v.length; i++) {
            ans[i] = abs(v[i]);
        }
        return (ans);
    }
}

Related

  1. range(double value1, double value2)
  2. range(double[] data, int to, int stride, int numElementsEachStride)
  3. range(double[] min, double[] max, double[] min2, double[] max2)
  4. range(double[] vals)