[Python-Dev] math.hypot, complex.__abs__, and documentation
I have a minor concern about certain corner cases with math.hypot and complex.__abs__, namely when one component is infinite and one is not a number. If we execute the following code: import math inf = float('inf') nan = float('nan') print math.hypot(inf, nan) print abs(complex(nan, inf)) ... then we see that 'inf' is printed in both cases. The standard library tests (for example, test_cmath.py:test_abs()) seem to test for this behavior as well, and FWIW, I personally agree with this convention. However, the math module's documentation for both 2.6 and 3.1 states, "All functions return a quiet NaN if at least one of the args is NaN." math.pow(1.0, nan) is another such exception to the rule. Perhaps the documentation should be updated to reflect this. Thanks, - David ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] math.hypot, complex.__abs__, and documentation
Ok, thanks! It's submitted as issue 7947. - David -Original Message- From: Mark Dickinson [mailto:dicki...@gmail.com] Sent: Tuesday, February 16, 2010 2:15 PM To: David DiCato Cc: python-dev@python.org Subject: Re: [Python-Dev] math.hypot, complex.__abs__, and documentation On Tue, Feb 16, 2010 at 9:19 PM, David DiCato wrote: > I have a minor concern about certain corner cases with math.hypot and > complex.__abs__, namely when one component is infinite and one is not a > number. > as well, and FWIW, I personally agree with this convention. However, the > math module’s documentation for both 2.6 and 3.1 states, “All functions > return a quiet NaN if at least one of the args is NaN.” Yes; this is a doc bug. Please could you open an issue on http://bugs.python.org ? > math.pow(1.0, nan) is another such exception to the rule. Perhaps the > documentation should be updated to reflect this. Yes, it should. Thanks! Mark ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] math.hypot, complex.__abs__, and documentation
Mathematically, think of nan as 'indeterminate'. When you're trying to get the magnitude of a vector, you know that it's infinite if even one of the components is infinite. So, the fact that the other component is indeterminate can be ignored. It's the same with math.pow(1.0, float('nan')); the second argument simply doesn't matter when the first is 1.0. FWIW, these conventions also exist in the C99 standard. Hope this helps, - David -Original Message- From: python-dev-bounces+ddicato=microsoft@python.org [mailto:python-dev-bounces+ddicato=microsoft@python.org] On Behalf Of Steven D'Aprano Sent: Tuesday, February 16, 2010 2:47 PM To: python-dev@python.org Subject: Re: [Python-Dev] math.hypot, complex.__abs__, and documentation On Wed, 17 Feb 2010 08:19:00 am David DiCato wrote: > I have a minor concern about certain corner cases with math.hypot and > complex.__abs__, namely when one component is infinite and one is not > a number. If we execute the following code: > > import math > inf = float('inf') > nan = float('nan') > print math.hypot(inf, nan) > print abs(complex(nan, inf)) > > ... then we see that 'inf' is printed in both cases. The standard > library tests (for example, test_cmath.py:test_abs()) seem to test > for this behavior as well, and FWIW, I personally agree with this > convention. What's the justification for that convention? It seems wrong to me. If you expand out hypot and substitute a=inf and b=nan, you get: >>> math.sqrt(inf*inf + nan*nan) nan which agrees with my pencil-and-paper calculation: sqrt(inf*inf + nan*nan) = sqrt(inf + nan) = sqrt(nan) = nan -- Steven D'Aprano ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/ddicato%40microsoft.com ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com