Java Double Number mod mod(double x, double y)

Here you can find the source of mod(double x, double y)

Description

mod

License

Open Source License

Declaration

public static double mod(double x, double y) 

Method Source Code

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

public class Main {
    public static double mod(double x, double y) {

        if (0. == y)
            return x;

        double m = x - y * Math.floor(x / y);

        // handle boundary cases resulted from floating-point cut off:
        if (y > 0) { // modulo range: [0..y)
            if (m >= y) // Mod(-1e-16             , 360.    ): m= 360.
                return 0;
            if (m < 0) {
                if (y + m == y)
                    return 0; // just in case...
                else
                    return y + m; // Mod(106.81415022205296 , _TWO_PI ): m= -1.421e-14
            }//from ww w  .  j a  v  a  2s.  c  om
        } else { // modulo range: (y..0]
            if (m <= y) // Mod(1e-16              , -360.   ): m= -360.
                return 0;
            if (m > 0) {
                if (y + m == y)
                    return 0; // just in case...
                else
                    return y + m; // Mod(-106.81415022205296, -_TWO_PI): m= 1.421e-14
            }
        }

        return m;
    }
}

Related

  1. mod(double number, double mod)
  2. mod(double value, double mod)
  3. mod(double x, double m)
  4. mod(double x, double y)
  5. mod(double x, double y)
  6. mod(final double a, final double b)
  7. mod2pi(double vin)
  8. mod2pi_pos(double vin)
  9. modArray(double value, int mod, int div)