wongzigii
(Zigii Wong)
April 16, 2022, 7:25am
#1
let decimal = Decimal(string: "0.000499")
decimal.nextUp // 0.000500
decimal.nextUp // 0.000600
The least representable value that compares greater than this value.
Why decimal's nextUp
is ignoring the precision?
Expect:
let decimal = Decimal(string: "0.000499")
decimal.nextUp // 0.000500
decimal.nextUp // 0.000501
What extra work I need to do at this case?
Martin
(Martin R)
April 16, 2022, 8:03am
#2
Possible bugs of Decimal.ulp
and Decimal.nextUp
were mentioned here:
Trying to find the number of decimals in a decimal number, I tried this in a REPL:
39> Decimal(0.01) > Decimal(1.0)
$R28: Bool = false
40> Decimal(0.01).ulp
$R29: Decimal = 0.010000
41> Decimal(0.01).ulp > Decimal(1.0)
$R30: Bool = true
This doesn't seem to work as expected. In fact, the ulp of even a small Decimal number, or a number with many decimal places, will still be larger than a very large Decimal number with no decimal places:
105> Decimal(string: "1.01")!.ulp > Decimal(string:…
xwu
(Xiaodi Wu)
April 16, 2022, 5:31pm
#3
I fixed this for the open-source version (swift-corelibs-foundation) but Apple hasn’t picked up the changes for the proprietary version shipped on their platforms
8 Likes
Maybe compatibility tests with Darwin, that should fail, could draw attention this issue.
wongzigii
(Zigii Wong)
April 17, 2022, 7:49am
#5
Is there any workaround for now ?
xwu
(Xiaodi Wu)
April 17, 2022, 12:45pm
#6
I suppose you could copy and paste the corrected code into your own project.