Here you can find the source of distUndermod(double a, double b, double mod)
public static double distUndermod(double a, double b, double mod)
//package com.java2s; public class Main { public static double distUndermod(double a, double b, double mod) { if (mod < 0) { throw new IllegalArgumentException("mod must be positive"); }//w w w . ja va 2s. c om // compute difference in each direction. double diff_x = a - b; double diff_y = b - a; // take modulo. diff_x = diff_x % mod; diff_y = diff_y % mod; // ensure +ve. if (diff_x < 0) { diff_x += mod; } if (diff_y < 0) { diff_y += mod; } return Math.min(diff_x, diff_y); } }