ulp method

FloatingPointValue ulp()

Compute the unit in the last place for the given FloatingPointValue.

Implementation

FloatingPointValue ulp() {
  if (isNaN || isAnInfinity) {
    return abs();
  }

  final populator = clonePopulator();
  if (exponent.isZero) {
    return populator.ofConstant(subNormalAsZero
        ? FloatingPointConstants.smallestPositiveNormal
        : FloatingPointConstants.smallestPositiveSubnormal);
  }

  final fractionWidth = mantissaWidth - (explicitJBit ? 1 : 0);
  final encodedExponent = exponent.toInt();
  if (encodedExponent <= fractionWidth) {
    if (subNormalAsZero) {
      return populator
          .ofConstant(FloatingPointConstants.smallestPositiveNormal);
    }
    return populator.ofBigInts(
        BigInt.zero, BigInt.one << (encodedExponent - 1));
  }

  return populator.ofBigInts(BigInt.from(encodedExponent - fractionWidth),
      explicitJBit ? BigInt.one << fractionWidth : BigInt.zero);
}