[Python-Dev] Should I care what -3.14 // inf produces?
Is an implementation of Python free to make up its own answers to
division and modulus operatons in the case of inf and nan arguments?
The standard library documentation says only that // is "rounded towards
minus infinity". The language reference says that ||:
1. |x == (x//y)*y + (x%y)|,
2. the modulus has the same sign as y, and
3. division by (either kind of) zero raises |ZeroDivisionError| .
It's consistent, but it doesn't define the operator over the full range
of potential arguments. In Python 3.8 and 3.9:
>>> from decimal import Decimal
>>> Decimal('-3.14') // Decimal('Infinity')
Decimal('-0')
>>> -3.14 // float("inf")
-1.0
>>> import math
>>> math.floor(-3.14 / float("inf"))
0
I can see sense in all three answers, as possible interpretations of
"rounded towards minus infinity", but I quite like decimal's. There seem
to be no regression tests for floor division of floats, and for modulus
only with finite arguments, perhaps intentionally.
--
Jeff Allen
___
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/BXYBSUMNSP6AAAS6OL23ANSML4IOARVB/
Code of Conduct: http://python.org/psf/codeofconduct/
[Python-Dev] Re: Should I care what -3.14 // inf produces?
Hi Jeff,
The decimal module docstring starts with:
"""
This is an implementation of decimal floating point arithmetic based on
the General Decimal Arithmetic Specification:
http://speleotrove.com/decimal/decarith.html
and IEEE standard 854-1987:
http://en.wikipedia.org/wiki/IEEE_854-1987
Decimal floating point has finite precision with arbitrarily large bounds.
"""
I suggest you to look into these standards.
Victor
On Thu, Sep 30, 2021 at 9:13 AM Jeff Allen wrote:
>
> Is an implementation of Python free to make up its own answers to division
> and modulus operatons in the case of inf and nan arguments?
>
> The standard library documentation says only that // is "rounded towards
> minus infinity". The language reference says that :
>
> x == (x//y)*y + (x%y),
> the modulus has the same sign as y, and
> division by (either kind of) zero raises ZeroDivisionError .
>
> It's consistent, but it doesn't define the operator over the full range of
> potential arguments. In Python 3.8 and 3.9:
>
> >>> from decimal import Decimal
> >>> Decimal('-3.14') // Decimal('Infinity')
> Decimal('-0')
> >>> -3.14 // float("inf")
> -1.0
> >>> import math
> >>> math.floor(-3.14 / float("inf"))
> 0
>
> I can see sense in all three answers, as possible interpretations of "rounded
> towards minus infinity", but I quite like decimal's. There seem to be no
> regression tests for floor division of floats, and for modulus only with
> finite arguments, perhaps intentionally.
>
> --
>
> Jeff Allen
>
> ___
> Python-Dev mailing list -- [email protected]
> To unsubscribe send an email to [email protected]
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at
> https://mail.python.org/archives/list/[email protected]/message/BXYBSUMNSP6AAAS6OL23ANSML4IOARVB/
> Code of Conduct: http://python.org/psf/codeofconduct/
--
Night gathers, and now my watch begins. It shall not end until my death.
___
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/GLFGZ6D2YTFL3HYO4JL4OR6UAANR7JP6/
Code of Conduct: http://python.org/psf/codeofconduct/
[Python-Dev] Re: Should I care what -3.14 // inf produces?
30.09.21 10:07, Jeff Allen пише:
from decimal import Decimal
Decimal('-3.14') // Decimal('Infinity')
> Decimal('-0')
You do not need an infinity to see the difference.
>>> Decimal('3.14') % Decimal('2')
Decimal('1.14')
>>> Decimal('3.14') % Decimal('-2')
Decimal('1.14')
>>> Decimal('-3.14') % Decimal('2')
Decimal('-1.14')
>>> Decimal('-3.14') % Decimal('-2')
Decimal('-1.14')
Decimals use a different rule than integers and floats: the modulus has
the same sign as the dividend. It was discussed using this rule for
floats (perhaps there is even FAQ or HOWTO for this), there are
advantages of using it for floats (the result is more accurate). But the
current rule (the modulus has the same sign as the divisor) is much much
more convenient for integers, and having different rules for integers
and floats is a source of bugs.
___
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/YBIDA44TP4EA3L3OP6TQUHA6UWA6EJR2/
Code of Conduct: http://python.org/psf/codeofconduct/
