[issue1640] Enhancements for mathmodule

Guido van Rossum report at bugs.python.org
Thu Jan 3 18:28:15 CET 2008


Guido van Rossum added the comment:

> > One nit: you added a blank line to the end of test_math.py.
> > This will cause the checkin to fail. :-)
>
> *grr* stupid white space check hook

No, you edited a line that didn't need editing. :-)

> > One bigger issue: the sign() function doesn't seem to work properly for
> > nans.  E.g. on Linux I get:
> >
> >>>> inf = 1e1000
> >>>> nan = inf/inf
> >>>> mnan = -nan
> >>>> math.sign(nan)
> > -1
> >>>> math.sign(mnan)
> > 1
> >
> > IOW a positive nan is considered negative and vice versa.  (This is
> > probably due to the way nans defy testing, always returning false.)
>
> If I recall the definition correctly NaNs don't have a sign. The content
> of the sign bit is not defined for NaNs. I could fix the sign but it's
> just eye candy and a waste of CPU cycles. IMO it would be more
> appropriate to return 0 for NaNs instead of +1 or -1.

Perhaps you recall wrong, as negating the nan returns one with the
opposite sign? This seems to indicate that there are positive and
negative nans.

> > I'm also curious why math.sign(0.0) returns 1 -- this is going to
> > cause a lot of confusion.
>
> math.sign(0.0) == 1 and math.sign(-0.0) == -1 is the main purpose of the
> sign() function. Most ordinary users are still going to use x > 0.0 or x
> < 0.0 instead of math.sign(). math.sign() is for the power users who
> need to determinate whether an operation hits the 0 from the left or
> right side of the number line.
>
> Because 0.0 == -0.0 it's not possible the distinguish a positive from a
> negative zero with comparison operations. if x == 0.0 and
> str(x).startswith("-") was the only existing way to detect a negative zero.

Hm, OK, but then passing a zero of some other type (e.g. int) should
also return +1 as the sign. I also think the function's name should be
changed, because I (and I assume many others) have grown up with a
sign() function that essentially returns cmp(x, 0.0).

Perhaps it would be better to have a function math.isneg() that
returns True for -0.0 and anything smaller and False for +0.0 and
anything larger. It could also return the proper sign of a nan.

I suggest that you check in the isinf() and isnan() functions and
their tests once you have docs for them, and hold off on the sign()
function until we've got agreement on it.

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue1640>
__________________________________


More information about the Python-bugs-list mailing list