Skip to content

[SR-6671] Decimal.leastNonzeroMagnitude, leastNormalMagnitude should have -128 exponent #4266

@swift-ci

Description

@swift-ci
Previous ID SR-6671
Radar None
Original Reporter takasek (JIRA User)
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee @xwu
Priority Medium

md5: f8e60ccf11f2fdc6d95d64e7bcbd0e41

relates to:

  • SR-14974 Decimal.ulp produces inaccurate results

Issue Description:

`Decimal.leastNonzeroMagnitude` and `Decimal.leastNormalMagnitude` are set -127 as their exponents.
https://github.com/apple/swift-corelibs-foundation/blob/ee61823ae71f4c4f9ca157fd290c4b7de915b2a4/Foundation/Decimal.swift#L120

However, you can make less value than least magnitude by such code below.

func truelyLeastDecimal() -> Decimal {
    var x = Decimal(string: "0.1")!
    var r = Decimal()
    NSDecimalPower(&r, &x, 128, .plain)
    return r
}

func p(_ d: Decimal) {
    print("=====")
    print(d)
    print(d._mantissa)
    print(d._exponent)
}
p(Decimal.leastNormalMagnitude)
p(truelyLeastDecimal())

/*
 =====
 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
 (1, 0, 0, 0, 0, 0, 0, 0)
 -127
 =====
 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
 (1, 0, 0, 0, 0, 0, 0, 0)
 -128
 */

NSDecimalNumber defines its exponent's range is down to -128.
https://developer.apple.com/documentation/foundation/nsdecimalnumber

Thus `Foundation.Decimal` should follow it. Isn't it?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions