Modular arithmetic for double value: m mod n - Java java.lang

Java examples for java.lang:Math Operation

Description

Modular arithmetic for double value: m mod n

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        double m = 2.45678;
        double n = 2.45678;
        System.out.println(mod(m, n));
    }/*  ww  w. ja  va 2  s  .co m*/

    /**
     * Modular arithmetic: m mod n
     * @param m
     * @param n
     * @return m mod n
     */
    public static double mod(double m, double n) {
        double result = m % n;
        return result < 0 ? result + n : result;
    }
}

Related Tutorials