Java Double Number mod mod(double left, double right)

Here you can find the source of mod(double left, double right)

Description

mod

License

Open Source License

Declaration

public static long mod(double left, double right) 

Method Source Code

//package com.java2s;
/*//from  w w  w .ja va 2 s. c o  m
 * #%~
 * VDM Code Generator Runtime
 * %%
 * Copyright (C) 2008 - 2014 Overture
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #~%
 */

public class Main {
    public static long mod(double left, double right) {
        validateInput(left, right);

        return (long) (left - right * (long) Math.floor(left / right));
    }

    private static void validateInput(double left, double right) {
        if (!(is_int(left) && is_int(right))) {
            throw new ArithmeticException("Operands must be integers. Got left " + left + " and right" + right);
        }

        if (right == 0L) {
            throw new ArithmeticException("Division by zero is undefined");
        }
    }

    public static boolean is_int(Object value) {
        Double doubleValue = getDoubleValue(value);

        return is_int(doubleValue);
    }

    private static boolean is_int(Double doubleValue) {
        return doubleValue != null && (doubleValue == Math.floor(doubleValue)) && !Double.isInfinite(doubleValue);
    }

    private static Double getDoubleValue(Object value) {
        if (!(value instanceof Number)) {
            return null;
        }

        Double doubleValue = ((Number) value).doubleValue();

        return doubleValue;
    }
}

Related

  1. mod(double a, double b)
  2. mod(double a, double b)
  3. Mod(double arg1, double arg2)
  4. mod(double i, final double n)
  5. mod(double m, double n)
  6. mod(double n, double d)
  7. mod(double number, double mod)
  8. mod(double value, double mod)