Understanding the working mechanis of python unary arithmetic operators.
hongy...@gmail.com
hongyi.zhao at gmail.com
Sun Oct 3 01:16:17 EDT 2021
On Sunday, October 3, 2021 at 3:05:23 AM UTC+8, ju... at diegidio.name wrote:
> On Saturday, 2 October 2021 at 14:48:39 UTC+2, hongy... 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>
> > >
> > > > I'm very puzzled by these operators. Any hints will be highly appreciated.
> > >
> > > Try and read the proverbial manual: that's truly a fundamental skill...
> >
> > Thank you for your explanation. Then what about the following questions?:
> >
> > 1. Should `+' and `-' be classified as binary operators or unary operators?
> "Symbol overloading": a+b is binary *addition*, +a is unary *identity* (or however you may like to call it). The meaning of a symbol or name depends on context.
As I understand now, it can be used to identify/normalize the operand by the corresponding precision in the given context.
> > As we all know, `a + b', and `a - b' are the normal ways we do basic arithmetic operations.
> Nonsense. You yourself wrote ~(-(+1)) above, just without parentheses.
Thank you for pointing out my contradictory assertion.
> > 2. See the following testings:
> Read the bloody manual.
>
> Given that:
> int(True) = 1
> int(False) = 0
>
> and that:
> bool(x) is True iff x <> 0
In [8]: bool(None)
Out[8]: False
In [9]: bool('')
Out[9]: False
In [10]: bool(0)
Out[10]: False
So, bool(x) is True iff x <> 0 , None, and '', as shown here [1]:
In [3]: import numpy as np
In [11]: np.array([1, 0.5, 0, None, 'a', '', True, False], dtype=bool)
Out[11]: array([ True, True, False, False, True, False, True, False])
[1] https://riptutorial.com/numpy/example/21181/creating-a-boolean-array#example
> and that:
> ~~x = x for all x (integer)
>
> These:
> ~1 = -(1+1) = -2
> ~~1 = ~-2 = -(-2+1) = 1
> ~~~1 = ~1 = -2
> ...
> all evaluate to True.
>
> And these:
> ~0 = -(0+1) = -1
> ~~0 = ~-1 = -(-1+1) = 0
> ~~~0 = ~0 = -1
> ...
> evaluate to True and False alternatingly.
>
> In short, ~1=-2 (and ~-2=1) and 1 and -2 both convert to True, while ~0=1 but 0 converts to False while 1 converts to True.
> > Why can’t/shouldn't we get something similar results for both `True' and `False' in the above testings?
> Because bitwise complement is not symmetric around 0. For comparison, try with negation instead.
Thanks again. I see, as follows:
In [1]: ~1
Out[1]: -2
In [2]: ~-1
Out[2]: 0
> But don't just guess, try and unpack those expressions while trying and reading the docs: which is a necessary skill in itself.
Thank you for the advice that showed me the way to truth.
HZ
More information about the Python-list
mailing list