apple/swift-numerics

Add `compound` function

Open

#161 建立於 2020年11月2日

在 GitHub 查看
 (4 留言) (0 反應) (0 負責人)Swift (176 fork)auto 404
enhancementgood first issuequestion

倉庫指標

Star
 (1,862 star)
PR 合併指標
 (PR 指標待抓取)

描述

IEEE 754 recommends (but does not require) a compound function, which is in the process of being standardized for C and C++; we should expose it in swift-numerics. The simplest, most literal translation of the operation into Swift would be:

extension RealFunctions {
  /// (1+x)ⁿ
  /// 
  /// Returns NaN if x < -1.
  static func compound(_ x: Self, _ n: Int) -> Self {
    // not quite production-quality implementation
    // (good for well-scaled results, loses precision
    // near overflow and underflow, also misbehaves
    // when n cannot be represented as Self).
    exp(Self(n) * log(onePlus: x))
  }
}

A good implementation is somewhat subtle, but we can come up with a good solution. I'm initially mainly interested in feedback on what the API should look like for this operation, both the naming and whether to put it on RealFunctions or ElementaryFunctions (where it can also be defined).

貢獻者指南