Here you can find the source of modSubtract(long a, long b, int m)
Parameter | Description |
---|---|
a | first value |
b | second value |
m | a modulus |
public static int modSubtract(long a, long b, int m)
//package com.java2s; //License from project: Open Source License public class Main { /**/*www. j a va 2 s .c o m*/ * Some methods return this value as indication that the answer doesn't exist or is undefined.<br> * For example, when mod = 0 in modular arithmetics, or when a number isn't a perfect power and * a function should return its base. * <p>This constant equals to {@code Integer.MIN_VALUE} because {@code Integer.MIN_VALUE} * can't be a base of any perfect power that fits in long. Proof:<ul> * <li>Base of perfect square is a positive number.</li> * <li>Integer.MIN_VALUE<sup>3</sup> = ((-2)<sup>31</sup>)<sup>3</sup> = (-2)<sup>93</sup> < (-2)<sup>63</sup> = Long.MIN_VALUE.</li> * <li>Higher powers would give higher values.</li></ul> * So no power greater than one can return base = {@code Integer.MIN_VALUE} for any long integer. * <p>Also, any modular arithmetic operation returns non-negative value, * so negative values can be used as error codes. * <p>Error codes are more desirable than throwing an exception when performance matters. */ public static final int NOT_FOUND = Integer.MIN_VALUE; /** * Modular subtraction.<br> * Returns ( a - b )( mod m ).<br> * Differs from ( a - b ) % m in that it always returns non-negative value and never overflows.<br> * If m = 0, {@link #NOT_FOUND} is returned. * @param a first value * @param b second value * @param m a modulus * @return ( a - b )( mod m ), or {@link #NOT_FOUND} if m = 0 */ public static int modSubtract(long a, long b, int m) { if (m <= 0) { if (m == 0) return NOT_FOUND; m = -m; } a %= m; b %= m; a = (a - b) % m; if (a < 0L) a += m; return (int) a; } /** * Modular subtraction.<br> * Returns ( a - b )( mod m ).<br> * Differs from ( a - b ) % m in that it always returns non-negative value and never overflows.<br> * If m = 0, {@link #NOT_FOUND} is returned. * @param a first value * @param b second value * @param m a modulus * @return ( a - b )( mod m ), or {@link #NOT_FOUND} if m = 0 */ public static long modSubtract(long a, long b, long m) { if (m <= 0L) { if (m == 0L) return NOT_FOUND; if (m == Long.MIN_VALUE) { a -= b; if (a < 0L) a += m; return a; } m = -m; } a %= m; b %= m; if (a < 0L) a += m; if (b < 0L) b = -b; else b = m - b; long leftTillOverflow = m - b; return leftTillOverflow > a ? a + b : a - leftTillOverflow; } }