frexp function

double frexp(
  1. double x,
  2. List<int> exp
)

Implementation

double frexp(double x, List<int> exp) {
  exp[0] = 0;
  if (x == 0.0 || x.isNaN || x.isInfinite) return x;

  bool neg = x < 0;
  x = x.abs();

  int e = 0;
  if (x < _DBL_MIN) {
    x *= 18446744073709551616.0; // 2^64
    e -= 64;
  }

  int bits = _doubleBits(x);
  e += ((bits >> 52) & 0x7FF) - 1022;
  // Set exponent to -1 (biased 1022) -> mantissa in [0.5, 1.0)
  bits = (bits & 0x800FFFFFFFFFFFFF) | (0x3FE << 52);
  x = _bitsToDouble(bits);

  exp[0] = e;
  return neg ? -x : x;
}