Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d2364e8

Browse files
committed
SF bug #477221: abs and divmod act oddly with -0.0.
Partial fix. float_abs(): ensure abs(-0.0) returns +0.0. Bugfix candidate.
1 parent 3808045 commit d2364e8

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

Objects/floatobject.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ PyFloat_FromString(PyObject *v, char **pend)
114114
}
115115
if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
116116
PyUnicode_GET_SIZE(v),
117-
s_buffer,
117+
s_buffer,
118118
NULL))
119119
return NULL;
120120
s = s_buffer;
@@ -196,16 +196,16 @@ PyFloat_AsDouble(PyObject *op)
196196
PyNumberMethods *nb;
197197
PyFloatObject *fo;
198198
double val;
199-
199+
200200
if (op && PyFloat_Check(op))
201201
return PyFloat_AS_DOUBLE((PyFloatObject*) op);
202-
202+
203203
if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
204204
nb->nb_float == NULL) {
205205
PyErr_BadArgument();
206206
return -1;
207207
}
208-
208+
209209
fo = (PyFloatObject*) (*nb->nb_float) (op);
210210
if (fo == NULL)
211211
return -1;
@@ -214,10 +214,10 @@ PyFloat_AsDouble(PyObject *op)
214214
"nb_float should return float object");
215215
return -1;
216216
}
217-
217+
218218
val = PyFloat_AS_DOUBLE(fo);
219219
Py_DECREF(fo);
220-
220+
221221
return val;
222222
}
223223

@@ -505,7 +505,7 @@ float_pow(PyObject *v, PyObject *w, PyObject *z)
505505
else
506506
ix = 1.0;
507507
PyFPE_END_PROTECT(ix)
508-
return PyFloat_FromDouble(ix);
508+
return PyFloat_FromDouble(ix);
509509
}
510510
if (iv == 0.0) { /* 0**w is error if w<0, else 1 */
511511
if (iw < 0.0) {
@@ -537,7 +537,7 @@ static PyObject *
537537
float_int_div(PyObject *v, PyObject *w)
538538
{
539539
PyObject *t, *r;
540-
540+
541541
t = float_divmod(v, w);
542542
if (t != NULL) {
543543
r = PyTuple_GET_ITEM(t, 0);
@@ -570,8 +570,10 @@ float_abs(PyFloatObject *v)
570570
{
571571
if (v->ob_fval < 0)
572572
return float_neg(v);
573-
else
573+
else if (v->ob_fval > 0)
574574
return float_pos(v);
575+
else /* ensure abs(-0) is +0 */
576+
return PyFloat_FromDouble(+0.0);
575577
}
576578

577579
static int

0 commit comments

Comments
 (0)