Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NSDecimalPower(_:_:_:_:) behavior #4799

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions Sources/Foundation/Decimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1801,7 +1801,7 @@ public func NSDecimalPower(_ result: UnsafeMutablePointer<Decimal>, _ number: Un
return .overflow
}
NSDecimalCopy(result,number)
return result.pointee.power(UInt(power), roundingMode:roundingMode)
return result.pointee.power(power, roundingMode:roundingMode)
}

public func NSDecimalMultiplyByPowerOf10(_ result: UnsafeMutablePointer<Decimal>, _ number: UnsafePointer<Decimal>, _ power: Int16, _ roundingMode: NSDecimalNumber.RoundingMode) -> NSDecimalNumber.CalculationError {
Expand Down Expand Up @@ -2242,19 +2242,19 @@ extension Decimal {
_exponent = newExponent
return .noError
}
fileprivate mutating func power(_ p:UInt, roundingMode:RoundingMode) -> CalculationError {
fileprivate mutating func power(_ p:Int, roundingMode:RoundingMode) -> CalculationError {
if isNaN {
return .overflow
}
var power = p
var power = abs(p)
compnerd marked this conversation as resolved.
Show resolved Hide resolved
if power == 0 {
_exponent = 0
_length = 1
_isNegative = 0
self[0] = 1
_isCompact = 1
return .noError
} else if power == 1 {
} else if power == 1 || isZero {
return .noError
}

Expand Down Expand Up @@ -2297,7 +2297,20 @@ extension Decimal {
let previousError = error
var rightOp = self
error = NSDecimalMultiply(&self, &temporary, &rightOp, roundingMode)


// if power is negative, use multiplicative inverse
if p < 0 {
var leftOp = Decimal(1)
var rightOp = self

error = NSDecimalDivide(
&self,
&leftOp,
&rightOp,
roundingMode
)
}

if previousError != .noError { // FIXME is this the intent?
error = previousError
}
Expand Down
5 changes: 5 additions & 0 deletions Tests/Foundation/Tests/TestDecimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,14 @@ class TestDecimal: XCTestCase {
a = Decimal(8)
XCTAssertEqual(.noError, NSDecimalPower(&result, &a, 2, .plain))
XCTAssertEqual(Decimal(64), result)
a = Decimal(8)
XCTAssertEqual(.noError, NSDecimalPower(&result, &a, -2, .plain))
XCTAssertEqual(Decimal(1/64), result)
a = Decimal(-2)
XCTAssertEqual(.noError, NSDecimalPower(&result, &a, 3, .plain))
XCTAssertEqual(Decimal(-8), result)
XCTAssertEqual(.noError, NSDecimalPower(&result, &a, -3, .plain))
XCTAssertEqual(Decimal(-1/8), result)
for i in -2...10 {
for j in 0...5 {
var actual = Decimal(i)
Expand Down