Skip to content

[SR-15132][SR-15134] Fix Decimal implementation of significand APIs #3068

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

Merged
merged 3 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 21 additions & 0 deletions Darwin/Foundation-swiftoverlay-Tests/TestDecimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,27 @@ class TestDecimal : XCTestCase {
XCTAssertEqual(answer,num,"\(ones) / 9 = \(answer) \(num)")
}

func test_Significand() {
var x = -42 as Decimal
XCTAssertEqual(x.significand.sign, .plus)
var y = Decimal(sign: .plus, exponent: 0, significand: x)
XCTAssertEqual(y, -42)
y = Decimal(sign: .minus, exponent: 0, significand: x)
XCTAssertEqual(y, 42)

x = 42 as Decimal
XCTAssertEqual(x.significand.sign, .plus)
y = Decimal(sign: .plus, exponent: 0, significand: x)
XCTAssertEqual(y, 42)
y = Decimal(sign: .minus, exponent: 0, significand: x)
XCTAssertEqual(y, -42)

let a = Decimal.leastNonzeroMagnitude
XCTAssertEqual(Decimal(sign: .plus, exponent: -10, significand: a), 0)
let b = Decimal.greatestFiniteMagnitude
XCTAssertTrue(Decimal(sign: .plus, exponent: 10, significand: b).isNaN)
}

func test_SimpleMultiplication() {
var multiplicand = Decimal()
multiplicand._isNegative = 0
Expand Down
16 changes: 8 additions & 8 deletions Darwin/Foundation-swiftoverlay/Decimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -467,13 +467,13 @@ extension Decimal {
}

public init(sign: FloatingPointSign, exponent: Int, significand: Decimal) {
self.init(
_exponent: Int32(exponent) + significand._exponent,
_length: significand._length,
_isNegative: sign == .plus ? 0 : 1,
_isCompact: significand._isCompact,
_reserved: 0,
_mantissa: significand._mantissa)
self = significand
let error = withUnsafeMutablePointer(to: &self) {
NSDecimalMultiplyByPowerOf10($0, $0, Int16(exponent), .plain)
}
if error == .underflow { self = 0 }
// We don't need to check for overflow because `Decimal` cannot represent infinity.
if sign == .minus { negate() }
}

public init(signOf: Decimal, magnitudeOf magnitude: Decimal) {
Expand All @@ -492,7 +492,7 @@ extension Decimal {

public var significand: Decimal {
return Decimal(
_exponent: 0, _length: _length, _isNegative: _isNegative, _isCompact: _isCompact,
_exponent: 0, _length: _length, _isNegative: 0, _isCompact: _isCompact,
_reserved: 0, _mantissa: _mantissa)
}

Expand Down
16 changes: 8 additions & 8 deletions Sources/Foundation/Decimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -695,13 +695,13 @@ extension Decimal {
}

public init(sign: FloatingPointSign, exponent: Int, significand: Decimal) {
self.init(
_exponent: Int32(exponent) + significand._exponent,
_length: significand._length,
_isNegative: sign == .plus ? 0 : 1,
_isCompact: significand._isCompact,
_reserved: 0,
_mantissa: significand._mantissa)
self = significand
let error = withUnsafeMutablePointer(to: &self) {
NSDecimalMultiplyByPowerOf10($0, $0, Int16(exponent), .plain)
}
if error == .underflow { self = 0 }
// We don't need to check for overflow because `Decimal` cannot represent infinity.
if sign == .minus { negate() }
}

public init(signOf: Decimal, magnitudeOf magnitude: Decimal) {
Expand All @@ -720,7 +720,7 @@ extension Decimal {

public var significand: Decimal {
return Decimal(
_exponent: 0, _length: _length, _isNegative: _isNegative, _isCompact: _isCompact,
_exponent: 0, _length: _length, _isNegative: 0, _isCompact: _isCompact,
_reserved: 0, _mantissa: _mantissa)
}

Expand Down
25 changes: 25 additions & 0 deletions Tests/Foundation/Tests/TestDecimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//

import Foundation
import XCTest

class TestDecimal: XCTestCase {

func test_NSDecimalNumberInit() {
Expand Down Expand Up @@ -808,6 +811,27 @@ class TestDecimal: XCTestCase {
XCTAssertEqual(zero3.description, "0")
}

func test_Significand() {
var x = -42 as Decimal
XCTAssertEqual(x.significand.sign, .plus)
var y = Decimal(sign: .plus, exponent: 0, significand: x)
XCTAssertEqual(y, -42)
y = Decimal(sign: .minus, exponent: 0, significand: x)
XCTAssertEqual(y, 42)

x = 42 as Decimal
XCTAssertEqual(x.significand.sign, .plus)
y = Decimal(sign: .plus, exponent: 0, significand: x)
XCTAssertEqual(y, 42)
y = Decimal(sign: .minus, exponent: 0, significand: x)
XCTAssertEqual(y, -42)

let a = Decimal.leastNonzeroMagnitude
XCTAssertEqual(Decimal(sign: .plus, exponent: -10, significand: a), 0)
let b = Decimal.greatestFiniteMagnitude
XCTAssertTrue(Decimal(sign: .plus, exponent: 10, significand: b).isNaN)
}

func test_SimpleMultiplication() {
var multiplicand = Decimal()
multiplicand._isNegative = 0
Expand Down Expand Up @@ -1460,6 +1484,7 @@ class TestDecimal: XCTestCase {
("test_RepeatingDivision", test_RepeatingDivision),
("test_Round", test_Round),
("test_ScanDecimal", test_ScanDecimal),
("test_Significand", test_Significand),
("test_SimpleMultiplication", test_SimpleMultiplication),
("test_SmallerNumbers", test_SmallerNumbers),
("test_ULP", test_ULP),
Expand Down