问题
I have a Swift extension on NSDecimalNumber
:
extension NSDecimalNumber {
var asRawValue: NSDecimalNumber {
return self.multiplying(byPowerOf10: 30)
}
}
And a test:
let t = NSDecimalNumber(value: 335).asRawValue
let u = NSDecimalNumber(value: 0.00001).asRawValue
// fails
XCTAssert(t.subtracting(u).compare((NSDecimalNumber(value: 334.99999)).asRawValue) == .orderedSame)
// console
po NSDecimalNumber(value: 334.99999).asRawValue
334999990000000051200000000000000
Why is there a random 512 in the middle of the long tail of zeroes of my NSDecimalNumber? How can I avoid this so I can rely on the math of very small numbers?
Thanks
来源:https://stackoverflow.com/questions/48329601/nsdecimalnumber-math-error-in-the-deep-decimals