Understanding the working mechanis of python unary arithmetic operators.
Chris Angelico
rosuav at gmail.com
Tue Oct 5 17:10:01 EDT 2021
On Wed, Oct 6, 2021 at 7:52 AM hongy... at gmail.com <hongyi.zhao at gmail.com> wrote:
>
> On Saturday, October 2, 2021 at 4:59:54 PM UTC+8, ju... at diegidio.name wrote:
> > On Saturday, 2 October 2021 at 10:34:27 UTC+2, hongy... at gmail.com wrote:
> > > See the following testings:
> > >
> > > In [24]: a=3.1415926535897932384626433832795028841971
> > > In [27]: -a
> > > Out[27]: -3.141592653589793
> > You've never heard of floating-point? Double precision has 53 significant bits of mantissa, corresponding approximately to 16 decimal digits.
> > <https://en.wikipedia.org/wiki/Double-precision_floating-point_format#IEEE_754_double-precision_binary_floating-point_format:_binary64>
> > > In [17]: ~-+1
> > > Out[17]: 0
> > << The unary ~ (invert) operator yields the bitwise inversion of its integer argument. The bitwise inversion of x is defined as -(x+1). It only applies to integral numbers or to custom objects that override the __invert__() special method. >>
> > <https://docs.python.org/3/reference/expressions.html#unary-arithmetic-and-bitwise-operations>
>
> A further inference based on the above description:
>
> Let us consider this equation: -(x+1) = x, the solution is -0.5, which is not an integer. So we can safely come to a conclusion:
>
> If bool(a) == True, \forall a \in integer, then ~bool(a) == False; and vice versa.
>
If bool(a) is True, then ~bool(a) is exactly the same as writing
~True, and a has become irrelevant. That is simply how expression
evaluation works.
ChrisA
More information about the Python-list
mailing list