Compare commits

...

2 Commits

Author SHA1 Message Date
Md Kaif
7fd7e2db15
fix(float): abs(-0.0) should return 0.0 (#534)
abs(-0.0) returns -0.0 instead of 0.0, and math.fabs(-0.0) does the
same. Both come from the idiom (x < 0) ? -x : x. Since -0.0 < 0 is
false, the negative zero is returned unchanged.

The idiom appears twice, once in dmath_fabs and once open-coded in
float__abs__. Fix dmath_fabs by clearing the sign bit, the same way
dmath_copysign just above it works, and make float__abs__ call the
shared helper instead of repeating the comparison.

abs(-0.0) == 0.0 is true even with the bug, because -0.0 == 0.0 under
IEEE 754, so the added tests compare str(...) to check the sign.
2026-07-21 19:36:08 +08:00
Md Kaif
005550e92a
fix(str): zfill should preserve a leading sign (#533)
zfill writes the padding zeros before the whole string, so a leading
+ or - ends up in the middle of the result instead of staying in front.
For example "-5".zfill(4) gave '00-5' where CPython gives '-005'.

CPython inserts the padding after the sign character, and the sign
counts toward the requested width. Write the sign first, then the
zeros, then the rest of the string. delta is still computed before
this, so the total length is unchanged.

The existing tests only covered unsigned strings, which is why this
went unnoticed. Added three asserts alongside them.
2026-07-21 19:09:11 +08:00
5 changed files with 17 additions and 2 deletions

View File

@ -384,7 +384,7 @@ static bool int__abs__(int argc, py_Ref argv) {
static bool float__abs__(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
py_f64 val = py_tofloat(&argv[0]);
py_newfloat(py_retval(), val < 0 ? -val : val);
py_newfloat(py_retval(), dmath_fabs(val));
return true;
}

View File

@ -447,6 +447,12 @@ static bool str_zfill(int argc, py_Ref argv) {
}
c11_sbuf buf;
c11_sbuf__ctor(&buf);
// a leading sign is kept in front; the padding goes after it
if(self.size > 0 && (self.data[0] == '+' || self.data[0] == '-')) {
c11_sbuf__write_char(&buf, self.data[0]);
self.data++;
self.size--;
}
for(int i = 0; i < delta; i++) {
c11_sbuf__write_char(&buf, '0');
}

View File

@ -701,8 +701,11 @@ double dmath_copysign(double x, double y) {
return ux.f;
}
// https://github.com/kraj/musl/blob/kraj/master/src/math/fabs.c
double dmath_fabs(double x) {
return (x < 0) ? -x : x;
union Float64Bits u = { .f = x };
u.i &= -1ULL/2;
return u.f;
}
double dmath_ceil(double x) {

View File

@ -97,6 +97,9 @@ assert 3.4e+3 == 3400.0
assert abs(1.0) == 1.0
assert abs(-1.0) == 1.0
assert abs(0.0) == 0.0
# abs(-0.0) is 0.0, not -0.0. `==` cannot tell them apart, so check the sign.
assert str(abs(-0.0)) == '0.0'
assert str(abs(0.0)) == '0.0'
# import math
# assert math.isnan(0/0)

View File

@ -112,6 +112,9 @@ assert s2.join( seq ) == "runoob"
assert 'x'.zfill(5) == '0000x'
assert '568'.zfill(1) == '568'
assert '-5'.zfill(4) == '-005'
assert '+5'.zfill(4) == '+005'
assert '-'.zfill(3) == '-00'
num = 6
assert str(num) == '6'