TruncatedDivision

trait TruncatedDivision[@specialized(Byte, Short, Int, Long, Float, Double) A] extends Signed[A]

Division and modulus for computer scientists taken from https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf

For two numbers x (dividend) and y (divisor) on an ordered ring with y != 0, there exists a pair of numbers q (quotient) and r (remainder) such that these laws are satisfied:

(1) q is an integer
(2) x = y * q + r (division rule)
(3) |r| < |y|
(4t) r = 0 or sign(r) = sign(x)
(4f) r = 0 or sign(r) = sign(y).

where sign is the sign function, and the absolute value function |x| is defined as |x| = x if x >=0, and |x| = -x otherwise.

We define functions tmod and tquot such that:

q = tquot(x, y) and r = tmod(x, y) obey rule (4t),
(whichtruncates effectively towards zero) and functions fmod and fquot such that:
q = fquot(x, y) and r = fmod(x, y) obey rule (4f)
(which floors the quotient and effectively rounds towards negative infinity).

Law (4t) corresponds to ISO C99 and Haskell's quot/rem.

Law (4f) is described by Knuth and used by Haskell, and fmod corresponds to the REM function of the IEEE floating-point standard.

Companion
object
trait Signed[A]
trait Order[A]
trait PartialOrder[A]
trait Eq[A]
trait Serializable
class Any

Value members

Abstract methods

def fmod(x: A, y: A): A
def fquot(x: A, y: A): A
def tmod(x: A, y: A): A
def toBigIntOpt(x: A): Opt[BigInt]

Returns the integer a such that x = a * one, if it exists.

Returns the integer a such that x = a * one, if it exists.

def tquot(x: A, y: A): A

Concrete methods

def fquotmod(x: A, y: A): (A, A)
def tquotmod(x: A, y: A): (A, A)

Inherited methods

def abs(a: A): A

An idempotent function that ensures an object has a non-negative sign.

An idempotent function that ensures an object has a non-negative sign.

Inherited from
Signed
def compare(x: A, y: A): Int

Result of comparing x with y. Returns an Int whose sign is:

Result of comparing x with y. Returns an Int whose sign is:

  • negative iff x < y
  • zero iff x = y
  • positive iff x > y
Inherited from
Order
def comparison(x: A, y: A): Comparison

Like compare, but returns a cats.kernel.Comparison instead of an Int. Has the benefit of being able to pattern match on, but not as performant.

Like compare, but returns a cats.kernel.Comparison instead of an Int. Has the benefit of being able to pattern match on, but not as performant.

Inherited from
Order
override
def eqv(x: A, y: A): Boolean

Returns true if x = y, false otherwise.

Returns true if x = y, false otherwise.

Definition Classes
Order -> PartialOrder -> Eq
Inherited from
Order
override
def gt(x: A, y: A): Boolean

Returns true if x > y, false otherwise.

Returns true if x > y, false otherwise.

Definition Classes
Order -> PartialOrder
Inherited from
Order
override
def gteqv(x: A, y: A): Boolean

Returns true if x >= y, false otherwise.

Returns true if x >= y, false otherwise.

Definition Classes
Order -> PartialOrder
Inherited from
Order
def isSignNegative(a: A): Boolean
Inherited from
Signed
def isSignNonNegative(a: A): Boolean
Inherited from
Signed
def isSignNonPositive(a: A): Boolean
Inherited from
Signed
def isSignNonZero(a: A): Boolean
Inherited from
Signed
def isSignPositive(a: A): Boolean
Inherited from
Signed
def isSignZero(a: A): Boolean
Inherited from
Signed
override
def lt(x: A, y: A): Boolean

Returns true if x < y, false otherwise.

Returns true if x < y, false otherwise.

Definition Classes
Order -> PartialOrder
Inherited from
Order
override
def lteqv(x: A, y: A): Boolean

Returns true if x <= y, false otherwise.

Returns true if x <= y, false otherwise.

Definition Classes
Order -> PartialOrder
Inherited from
Order
def max(x: A, y: A): A

If x > y, return x, else return y.

If x > y, return x, else return y.

Inherited from
Order
def min(x: A, y: A): A

If x < y, return x, else return y.

If x < y, return x, else return y.

Inherited from
Order
override
def neqv(x: A, y: A): Boolean

Returns true if x != y, false otherwise.

Returns true if x != y, false otherwise.

Note: this default implementation provided by Order is the same as the one defined in Eq, but for purposes of binary compatibility, the override in Order has not yet been removed. See this discussion.

Definition Classes
Order -> Eq
Inherited from
Order
def partialCompare(x: A, y: A): Double
Inherited from
Order
def partialComparison(x: A, y: A): Option[Comparison]

Like partialCompare, but returns a cats.kernel.Comparison instead of an Double. Has the benefit of being able to pattern match on, but not as performant.

Like partialCompare, but returns a cats.kernel.Comparison instead of an Double. Has the benefit of being able to pattern match on, but not as performant.

Inherited from
PartialOrder
def pmax(x: A, y: A): Option[A]

Returns Some(x) if x >= y, Some(y) if x < y, otherwise None.

Returns Some(x) if x >= y, Some(y) if x < y, otherwise None.

Inherited from
PartialOrder
def pmin(x: A, y: A): Option[A]

Returns Some(x) if x <= y, Some(y) if x > y, otherwise None.

Returns Some(x) if x <= y, Some(y) if x > y, otherwise None.

Inherited from
PartialOrder
def sign(a: A): Sign

Returns Zero if a is 0, Positive if a is positive, and Negative is a is negative.

Returns Zero if a is 0, Positive if a is positive, and Negative is a is negative.

Inherited from
Signed
def signum(a: A): Int

Returns 0 if a is 0, 1 if a is positive, and -1 is a is negative.

Returns 0 if a is 0, 1 if a is positive, and -1 is a is negative.

Inherited from
Signed
def toOrdering: Ordering[A]

Convert a Order[A] to a scala.math.Ordering[A] instance.

Convert a Order[A] to a scala.math.Ordering[A] instance.

Inherited from
Order
def tryCompare(x: A, y: A): Option[Int]

Result of comparing x with y. Returns None if operands are not comparable. If operands are comparable, returns Some[Int] where the Int sign is:

Result of comparing x with y. Returns None if operands are not comparable. If operands are comparable, returns Some[Int] where the Int sign is:

  • negative iff x < y
  • zero iff x = y
  • positive iff x > y
Inherited from
PartialOrder