Hyperlinkv0.8.0-beta.28

BigDecimal

BigDecimal.divideUnsafeconsteffect/BigDecimal.ts:639
(that: BigDecimal): (self: BigDecimal) => BigDecimal
(self: BigDecimal, that: BigDecimal): BigDecimal

Provides an unsafe division operation on BigDecimals.

When to use

Use when you need to divide BigDecimal values where the divisor is known to be non-zero, so division by zero should be a thrown exception.

Details

If the dividend is not a multiple of the divisor, the result will be a BigDecimal value with up to the default division precision.

Gotchas

Throws a RangeError if the divisor is 0.

Example (Dividing decimals unsafely)

import { BigDecimal } from "effect"

console.log(BigDecimal.divideUnsafe(BigDecimal.fromStringUnsafe("6"), BigDecimal.fromStringUnsafe("3"))) // BigDecimal(2)
console.log(BigDecimal.divideUnsafe(BigDecimal.fromStringUnsafe("6"), BigDecimal.fromStringUnsafe("4"))) // BigDecimal(1.5)
mathdivide
export const divideUnsafe: {
  (that: BigDecimal): (self: BigDecimal) => BigDecimal
  (self: BigDecimal, that: BigDecimal): BigDecimal
} = dual(2, (self: BigDecimal, that: BigDecimal): BigDecimal => {
  if (that.value === bigint0) {
    throw new RangeError("Division by zero")
  }

  if (self.value === bigint0) {
    return zero
  }

  const scale = self.scale - that.scale
  if (self.value === that.value) {
    return make(bigint1, scale)
  }
  return divideWithPrecision(self.value, that.value, scale, DEFAULT_PRECISION)
})