bug-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Problem in GSDecimalCompare


From: Daniel Boyd
Subject: Problem in GSDecimalCompare
Date: Wed, 6 Dec 2023 17:25:41 +0000 (UTC)

I believe I may have found a bug in GSDecimalCompare in NSDecimal.m

If you run this code:

NSDecimalNumber * d1 = [NSDecimalNumber zero];
NSDecimalNumber * d2 = [NSDecimalNumber decimalNumberWithString: @"0.02"];

NSComparisonResult test = [d1 compare: d2];
NSLog(@"%@", test == NSOrderedAscending ? @"Ascending" : test == NSOrderedDescending ? @"Descending" : @"Same");

The output should be "Ascending", but is actually "Descending".

I have included an analysis of the issue provided by ChatGPT:

Given the representation of 0.02 and 0.00 in the GSDecimal format as you described, let's analyze how the GSDecimalCompare function behaves with these inputs:

  1. 0.02 in GSDecimal:

  2. 0.00 in GSDecimal:

Analysis:

Conclusion:

The issue seems to be arising from how the GSDecimalCompare function handles the case where one of the numbers (0.00) has a length of 0. It leads to skipping the digit comparison and making a decision based solely on the exponent and length calculations, which in this case, misleadingly indicates that 0.02 is smaller than 0.00.

Suggested Fix:

A potential fix in the GSDecimalCompare function could involve adding a check for cases where one operand has a length of 0. Specifically, if one operand has a length of 0 (indicating it's 0.00), and the other operand has a non-zero length, the function should return NSOrderedDescending if the non-zero length operand is positive, and NSOrderedAscending if it's negative. This logic should be placed before the digit comparison loop.


I have also included a patch which appears to correct the issue at least in this particular scenario.

Attachment: NSDecimal_patch.m
Description: Binary data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]