Java Integer Mod mod(long l, int m)

Here you can find the source of mod(long l, int m)

Description

Computes l mod m , such that the result is always in [0,m-1], for any long value including negative values.

License

Apache License

Parameter

Parameter Description
l long value
m modulus

Return

l mod m if l is nonnegative, (l mod m) + m otherwise

Declaration

public static int mod(long l, int m) 

Method Source Code

//package com.java2s;
/*/*w  w  w .ja  v a 2s.com*/
 * Copyright Myrrix Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Computes {@code l mod m}, such that the result is always in [0,m-1], for any {@code long}
     * value including negative values.
     *
     * @param l long value
     * @param m modulus
     * @return {@code l mod m} if l is nonnegative, {@code (l mod m) + m} otherwise
     */
    public static int mod(long l, int m) {
        return ((int) (l % m) + m) % m;
    }
}

Related

  1. mod(int v, int m)
  2. mod(int x, int y)
  3. mod(int x, int y)
  4. mod(int x, int y)
  5. mod(int[]... vectors)
  6. mod4(int a)
  7. mod_diff(int x, int y, int R)
  8. modByPowerOfTwo(int value, int mod)
  9. modCeil(int x, int mod)