简体   繁体   English

NSRoundBankers 没有按照 Apple 记录的那样工作

[英]NSRoundBankers is not working as documented by Apple

So, according to Apple's documentation about NSRoundBankers :因此,根据 Apple 关于NSRoundBankers的文档:

Round to the closest possible return value;四舍五入到最接近的可能返回值; when halfway between two possibilities, return the possibility whose last digit is even.当介于两种可能性之间时,返回最后一位为偶数的可能性。

While this is true for positive numbers, I am not getting the expected behaviour on negative numbers .虽然这适用于正数,但我没有得到负数的预期行为 Here is the piece of code I executed on the device and on the simulator, both printing the exact same results:这是我在设备和模拟器上执行的一段代码,两者都打印出完全相同的结果:

NSDecimalNumber *increment = [NSDecimalNumber decimalNumberWithMantissa:5 exponent:-2 isNegative:NO];
NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithMantissa:10 exponent:-1 isNegative:YES];
NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundBankers scale:1 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
while ([number compare:@1] == NSOrderedAscending)
{
    NSLog(@";%@;%@", number, [number decimalNumberByRoundingAccordingToBehavior:handler]);
    number = [number decimalNumberByAdding:increment];
}

On negative numbers, it's not returning the one whose last digit is even, it basically rounds down.对于负数,它不会返回最后一位为偶数的数字,它基本上是四舍五入。

For example, for -0.85 i should be getting -0.8 , but I am getting -0.9例如,对于-0.85我应该得到-0.8 ,但我得到-0.9

Am I doing something wrong?难道我做错了什么?

Left table shows the ACTUAL behaviour, in red marked the wrong rounded values.左表显示了实际行为,红色标记了错误的四舍五入值。

Right table shows the EXPECTED behaviour, in green the correct rounded values.右表显示了预期行为,绿色是正确的四舍五入值。

IOS 爪哇

Stumbled upon this today and it's probably a bug in Foundation.今天偶然发现了这个,它可能是 Foundation 中的一个错误。 If the source code of swift-corelibs-foundation has anything to do with the actual Foundation code, I might have found the culprit: https://github.com/apple/swift-corelibs-foundation/blob/70f8af962ff182c78a81673e75fe725b5b1b7827/Foundation/Decimal.swift#L1970 .如果 swift-corelibs-foundation 的源代码与实际的 Foundation 代码有任何关系,我可能已经找到了罪魁祸首: https : //github.com/apple/swift-corelibs-foundation/blob/70f8af962ff182c78a81673e75fe725b5b1b7827/Foundation/Decimal .swift#L1970

I believe 1 should be subtracted from remainder on the line 1970, not added to it.我认为 1 应该从 1970 行的余数中减去,而不是添加到其中。 Rationale: if remainder is 5, what does adding 1 to it actually change?基本原理:如果余数是 5,那么加 1 实际会改变什么? The case falls trough (line 1972) and the check on line 1974 succeeds either way, be it 5 or 6. If it were to become 4 for after subtracting, it will prevent adding 1 to self (line 1979) and keep the number even.案例跌至谷底(第 1972 行),第 1974 行的检查以任何方式成功,无论是 5 还是 6。如果减去后变为 4,它将阻止向self (第 1979 行)加 1 并保持数字偶数.

UPD the issue has been reported to Apple: FB7565793. UPD该问题已报告给 Apple:FB7565793。

UPD 2 the issue has been fixed by Apple in the first betas of iOS 14 / macOS 11.0. UPD 2该问题已由 Apple 在 iOS 14 / macOS 11.0 的第一个测试版中修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM