Home > Enterprise >  The lambda function result is mathematically wrong
The lambda function result is mathematically wrong

Time:09-30

why when i write -2 ** 2 in python the result is -4 but my lambda function is returning 4 anyone could explain why this is happening

Code result

two = lambda: 2
sqr = lambda x: x * x
pwr = lambda x, y: x ** y
for a in range(-2, 3):
    print(sqr(a), end=" ") 
    print(a,"**",two(),"=",pwr(a, two()))

CodePudding user response:

The ** operator binds more tightly than the - operator does in Python. If you want to override that, you'd use parentheses, e.g. (-i)**4

CodePudding user response:

-2 ** 2 means -2 to the power 2, which is -2 * -2 = 4

  • Related