nd4j-common/src/main/java/org/nd4j/linalg/util/BigDecimalMath.java
static public BigDecimal mod2pi(BigDecimal x) {
        /* write x= 2*pi*k+r with the precision in r defined by the precision of x and not
         * compromised by the precision of 2*pi, so the ulp of 2*pi*k should match the ulp of x.
         * First getFloat a guess of k to figure out how many digits of 2*pi are needed.
         */
        int k = (int) (0.5 * x.doubleValue() / Math.PI);
        /* want to have err(2*pi*k)< err(x)=0.5*x.ulp, so err(pi) = err(x)/(4k) with two safety digits
         */


        double err2pi;


        if (k != 0) {
            err2pi = 0.25 * Math.abs(x.ulp().doubleValue() / k);
        } else {
            err2pi = 0.5 * Math.abs(x.ulp().doubleValue());
        }
        MathContext mc = new MathContext(2 + err2prec(6.283, err2pi));
        BigDecimal twopi = pi(mc).multiply(new BigDecimal(2));
        /* Delegate the actual operation to the BigDecimal class, which may return
         * a negative value of x was negative .
         */
        BigDecimal res = x.remainder(twopi);


        if (res.compareTo(BigDecimal.ZERO) < 0) {
            res = res.add(twopi);
        }
        /* The actual precision is set by the input value, its absolute value of x.ulp()/2.
         */
        mc = new MathContext(err2prec(res.doubleValue(), x.ulp().doubleValue() / 2.));


        return res.round(mc);


    }
static public BigDecimal mod2pi(BigDecimal x) {
        /* write x= 2*pi*k+r with the precision in r defined by the precision of x and not
         * compromised by the precision of 2*pi, so the ulp of 2*pi*k should match the ulp of x.
         * First getFloat a guess of k to figure out how many digits of 2*pi are needed.
         */
        int k = (int) (0.5 * x.doubleValue() / Math.PI);
        /* want to have err(2*pi*k)< err(x)=0.5*x.ulp, so err(pi) = err(x)/(4k) with two safety digits
         */


        double err2pi;


        if (k != 0) {
            err2pi = 0.25 * Math.abs(x.ulp().doubleValue() / k);
        } else {
            err2pi = 0.5 * Math.abs(x.ulp().doubleValue());
        }
        MathContext mc = new MathContext(2 + err2prec(6.283, err2pi));
        BigDecimal twopi = pi(mc).multiply(new BigDecimal(2));
        /* Delegate the actual operation to the BigDecimal class, which may return
         * a negative value of x was negative .
         */
        BigDecimal res = x.remainder(twopi);


        if (res.compareTo(BigDecimal.ZERO) < 0) {
            res = res.add(twopi);
        }
        /* The actual precision is set by the input value, its absolute value of x.ulp()/2.
         */
        mc = new MathContext(err2prec(res.doubleValue(), x.ulp().doubleValue() / 2.));


        return res.round(mc);


    }
static public BigDecimal mod2pi ( BigDecimal x ) { /* write x= 2*pi*k+r with the precision in r defined by the precision of x and not
         * compromised by the precision of 2*pi, so the ulp of 2*pi*k should match the ulp of x.
         * First getFloat a guess of k to figure out how many digits of 2*pi are needed.
         */ int k = ( int ) ( 0.5 * x . doubleValue ( ) / Math . PI ) ; /* want to have err(2*pi*k)< err(x)=0.5*x.ulp, so err(pi) = err(x)/(4k) with two safety digits
         */ double err2pi ; if ( k != 0 ) { err2pi = 0.25 * Math . abs ( x . ulp ( ) . doubleValue ( ) / k ) ; } else { err2pi = 0.5 * Math . abs ( x . ulp ( ) . doubleValue ( ) ) ; } MathContext mc = new MathContext ( 2 + err2prec ( 6.283 , err2pi ) ) ; BigDecimal twopi = pi ( mc ) . multiply ( new BigDecimal ( 2 ) ) ; /* Delegate the actual operation to the BigDecimal class, which may return
         * a negative value of x was negative .
         */ BigDecimal res = x . remainder ( twopi ) ; if ( res . compareTo ( BigDecimal . ZERO ) < 0 ) { res = res . add ( twopi ) ; } /* The actual precision is set by the input value, its absolute value of x.ulp()/2.
         */ mc = new MathContext ( err2prec ( res . doubleValue ( ) , x . ulp ( ) . doubleValue ( ) / 2. ) ) ; return res . round ( mc ) ; }
Reduce value to the interval [0,2*Pi].

@param x the original value
@return the value modulo 2*pi in the interval from 0 to 2*pi.
Reduce value to the interval [ 0 2 * Pi ] .