NSDecimalNumber Math Error in the deep decimals [duplicate]

此生再无相见时 提交于 2020-01-06 08:02:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!