github.com/searKing/golang/go@v1.2.117/exp/math/rem.go (about)

     1  // Copyright 2023 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package math
     6  
     7  import (
     8  	"golang.org/x/exp/constraints"
     9  )
    10  
    11  // RingRem returns the remainder of x looped by +ny until in y > 0 ? [0, y) : (y, 0].
    12  // RingRem panics for y == 0 (division by zero).
    13  // y > 0, then ∈ [0, y)
    14  // y < 0, then ∈ (y, 0]
    15  func RingRem[T constraints.Integer](x, y T) T {
    16  	if y == 0 {
    17  		panic("division by zero")
    18  	}
    19  	return ((x % y) + y) % y
    20  }