Jump to content

Talk:Z16684

From Wikifunctions
Latest comment: 3 months ago by DVrandecic (WMF) in topic bug with handling some negative numbers

bug with handling some negative numbers

While working on testing other functions via Claude Code, I ran into what may be a bug with this function, demonstrated by the test I created, Z33624. I can't vouch for the correctness, but here's the analysis and proposed fix from the AI. --Ragesoss (talk) 07:11, 16 April 2026 (UTC)Reply

Claude Code's analysis

Test Z33624 (-1 + 1 = 0) fails for implementation Z16699 (+ on BigInt) because this deserializer doesn't handle canonical-form sign values. When Z16683K1 is a bare string reference like "Z16662" rather than the expanded ZObject form, the code accesses `.Z16659K1` on the string, gets `undefined`, and defaults to positive. Negative integers silently become positive.

The existing tests all pass because they use the expanded form for the sign field. Z33624 uses canonical form, which is equally valid.

Proposed fix — start from `Z16683K1` instead of `Z16683K1.Z16659K1`, and only drill into the object if it's not already a string:

function Z16684( Z16684K1 ) {
let value = Z16684K1.Z16683K2.Z13518K1;
if (typeof value === 'string') {
value = BigInt( Z16684K1.Z16683K2.Z13518K1 );
} else {
value = BigInt( Z16684K1.Z16683K2.Z13518K1.Z6K1 );
}
let sign = Z16684K1.Z16683K1;

// Handle canonical form where Z16683K1 is a bare string like "Z16662"
if (typeof sign !== 'string') {
sign = sign.Z16659K1;
while ( typeof sign === 'object' ) {
if ( 'Z9K1' in sign ) {
sign = sign.Z9K1;
} else if ( 'Z16659K1' in sign ) {
sign = sign.Z16659K1;
} else {
sign = '';
}
}
}

// Now sign is a string, with a value or empty
// (it could be also undefined or null, which
// means default value will be assigned)
if ( sign === 'Z16662' ) {
sign = -1n;
} else if ( sign === 'Z16661' ) {
sign = 0n;
} else {
sign = 1n;
}

if ( value > 0n && sign === 0n ) {
return value;
}

return sign * value;
}

Z16663 (Sign to JS number) has the same issue — it throws an error instead of silently giving the wrong result. A similar fix applies there. Ragesoss (talk) 07:11, 16 April 2026 (UTC)Reply

@Ragesoss:: Thank you! I did a slightly different fix, but I think it should capture the issue. Thanks for finding this. --DVrandecic (WMF) (talk) 12:24, 16 April 2026 (UTC)Reply