Floating-point shift operators

Those are standard behaviors for any operation that results in a subnormal or infinite value.

Sure, for any arithmetic operation (as scaleB is). Getting exponent isn’t an arithmetic operation, and for that reason it’d be surprising for setting exponent to be an arithmetic operation—put in terms of the IEEE 754 standard, exponent is part of the representation (level 3 specification) of a floating-point value; whereas scaleB is an operation defined in terms of the real number that is represented (level 1 specification) by the floating-point value.

Similarly, it’s unusual to find that setting a property succeeds and then getting the same property (without any intervening modifications) returns a different value.

Is there a reason to cast about for any other spelling than a run-of-the-mill method here?

3 Likes

All right, if we go with a method like scaled(byPowerOfTwo:), should we include a mutating counterpart?

If so, is it feasible to implement in such a way that a hypothetical BigFloat (with an arbitrary-width BigInt exponent) would be able to mutate its exponent in place, and thus avoid an extra allocation (for the new exponent’s words buffer)?

The words buffer should be CoW, no? A mutating counterpart is certainly conceivable if there's a compelling reason for one, but I'm not sure it's needed for that particular reason.

What API exists to mutate the exponent of a floating-point value?

If the implementation simply calls the confusing initializer like this:

extension BinaryFloatingPoint {
  mutating func scale(byPowerOfTwo p: Exponent) {
    self = Self(sign: .plus, exponent: p, significand: self)
  }
}

Is there a documented guarantee that the initializer works by mutating the significand argument rather than, say, constructing a new instance from scratch and copying in the data?

And if so, is there a guarantee that doing so will not itself trigger CoW?

Agree, seems like a reasonable thing for a BigFloat type to make those guarantees in its documentation where it can.

2 Likes
Terms of Service

Privacy Policy

Cookie Policy