Hyperlinkv0.8.0-beta.28

BigDecimal

BigDecimal.remainderconsteffect/BigDecimal.ts:1107
(divisor: BigDecimal): (self: BigDecimal) => Option.Option<BigDecimal>
(self: BigDecimal, divisor: BigDecimal): Option.Option<BigDecimal>

Computes the decimal remainder safely when one operand is divided by a second operand.

When to use

Use to compute a decimal remainder while representing division by zero as Option.none.

Details

If the divisor is 0, the result will be Option.none().

Example (Computing remainders safely)

import { BigDecimal, Option } from "effect"
import * as assert from "node:assert"

assert.deepStrictEqual(
  BigDecimal.remainder(
    BigDecimal.fromStringUnsafe("2"),
    BigDecimal.fromStringUnsafe("2")
  ),
  Option.some(BigDecimal.fromStringUnsafe("0"))
)
assert.deepStrictEqual(
  BigDecimal.remainder(
    BigDecimal.fromStringUnsafe("3"),
    BigDecimal.fromStringUnsafe("2")
  ),
  Option.some(BigDecimal.fromStringUnsafe("1"))
)
assert.deepStrictEqual(
  BigDecimal.remainder(
    BigDecimal.fromStringUnsafe("-4"),
    BigDecimal.fromStringUnsafe("2")
  ),
  Option.some(BigDecimal.fromStringUnsafe("0"))
)
export const remainder: {
  (divisor: BigDecimal): (self: BigDecimal) => Option.Option<BigDecimal>
  (self: BigDecimal, divisor: BigDecimal): Option.Option<BigDecimal>
} = dual(2, (self: BigDecimal, divisor: BigDecimal): Option.Option<BigDecimal> => {
  if (divisor.value === bigint0) {
    return Option.none()
  }

  const max = Math.max(self.scale, divisor.scale)
  return Option.some(make(scale(self, max).value % scale(divisor, max).value, max))
})