Skip to content

Raise Decimal to a power with FoundationEssentials #1455

@zcohan

Description

@zcohan

The problem

The following math operations can be performed on Decimal with FoundationEssentials:

import FoundationEssentials

let number: Decimal = 2.0
let added = number + number
let subtracted = number - number
let multiplied = number * number
let divided = number / number

However, raising aDecimal to a power requires Foundation:

import Foundation

let number: Decimal = 2.0
let raised = pow(number, 2)

The solution

  • The power operation should be available in FoundationEssentials alongside the basic decimal arithmetic operations.
  • An exponentiation operator could be offered here to be consistent with the existing inline operator support (Python uses **, for example).

Additional context

  • My app is particularly sensitive to binary size, so it's unfortunate that I have to import Foundation on non-Darwin platforms just for this somewhat essential function.
  • func pow(_ x: Decimal, _ y: Int) -> Decimal in Foundation appears to be a wrapper around NSDecimalPower().
  • The NSDecimalX() functions are part of Foundation, not FoundationEssentials.
  • NSDecimalAdd, NSDecimalSubtract, NSDecimalMultiply, and NSDecimalDivide are available in FoundationEssentials via operators, but an equivalent for NSDecimalPower() is not offered.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions