-2

I edited code here to assign variables to float. It is not working.

This code is to find value of either factor in (x+y)^2 = x^2 + 2xy + y^2

There are three factors, x, y and result. Value must be available for any two.

The only problem here is, you can't find value of x or y if they are float. For that I'm looking forward in google & to use some math's library if I can understand.

x = (input("\nEnter value of x: "))
y = (input("\nEnter value of y: "))
z = (input("\nEnter value of (x+y)^2: "))

#-----------------------------------------------------------------------
#Converting input to integers.
#-----------------------------------------------------------------------

a=0.0
b=0.0
c=0.0

if len(x)!=0:
     a = float(x)
else:
     print ("\nWe will search value of 'x'")

if len(y)!=0:
     b = float(y)
else:
     print ("\nWe will search value of 'y'")

if len(z)!=0:
     c = float(z)
else:
     print ("\nWe will search value of '(x+y)^2'")

#-----------------------------------------------------------------------
#Calculations
#-----------------------------------------------------------------------
     
from math import sqrt

fv=0.0 #fv = find value
rs=0.0 #rs = result

if len(x)==0:
     while True:
          rs = ((fv*fv) + (2*fv*b) + (b*b))
          fv = fv + 1
          if rs==c:
               print (f"\nValue of 'x' is either {(fv-1)} or {((sqrt(c)*-1) - b)}")
               break

if len(y)==0:
     while True:
          rs = ((a*a) + (2*a*fv) + (fv*fv))
          fv = fv+1
          if rs==c:
               print (f"\nValue of 'y' is either {(fv-1)} or {((sqrt(c)*-1) - a)}")
               break

if len(z)==0:
     rs = ((a*a) + (2*a*b) + (b*b))
     print (f"\nValue of '(x+y)^2' is {rs}")


print ("\nThank you")
  • All you have to do is replace your `int` conversions with `float`. Where are you stuck? – Prune Sep 15 '20 at 06:42
  • No. It was not working. From my perspective, (i)float values are infinite after decimal to count, that should be limited, which may save processing power. specially in while loop (ii)in while structure where I put fv+1, is also an issue, I also tried there fv+0.001. My expectation is if I enter (x+2.5)^2=25, it should show me answer x= 2.5 or -7.5 which is running ok in case of integers. (x+2)^2=16, where x=2 or -6 – Sandip Luhar Sep 15 '20 at 06:49
  • Show the actual behavior (display values), not just code. Not everyone can run your code on their computer, or in their head. If there are errors, show them. – hpaulj Sep 15 '20 at 07:11
  • I entered x=2.5, y="", z=25. It shows this message after I use Ctrl+C. Traceback (most recent call last): File "D:\ptsolo\x_y_square_version_2.py", line 57, in rs = ((a*a) + (2*a*fv) + (fv*fv)) KeyboardInterrupt. – Sandip Luhar Sep 15 '20 at 07:21
  • *"float values are infinite after decimal"* Have you considered that this may not actually be true? – Joni Sep 15 '20 at 11:41
  • Why the `numpy` and `sympy` tags? I don't see their use in the code. – hpaulj Sep 15 '20 at 15:11
  • Using `==` to compare floats is not reliable. floats are rarely exactly equal. I wonder if that's what you mean by "infinite after decimal". Also searching by stepping `fv` by 1 doesn't sound reliable. If I start with `b=2.5`, `rs` is successively 6.25, 12.25, 30.25 ... – hpaulj Sep 15 '20 at 15:18
  • See https://stackoverflow.com/questions/588004/is-floating-point-math-broken. You realize that you are using a digital computer, but you don't seem to realize the implications. As @hpaulj pointed out, you cannot *assume* that FP math is exact. Instead, you need to learn how to search for nearly-exact solutions, within the tolerance of your algorithm. – Prune Sep 15 '20 at 15:42
  • @Joni, What I meant by inifinite, was my concern with stepping process of while loop argument. I knew fv+1 is not reliable. However, I tried 0.0001 or 0.0000001 as stepping, That wasn't working either. for example = (2.5+x)^2=15.3257 where x==1.4148052314259516 or 6.414805231425952. (That's what I meant) I know I'm not able here to express myself clearly. Sorry for that. I want my user to enter any number which they may imagine within limits of simple processing power of PC. – Sandip Luhar Sep 15 '20 at 18:41
  • @hpaulj, Thanks for your concern & replys. 1)I think, using numpy or simpy, this problem can be solved. But, I'm beginner, so, didn't pointed it out in my subject or body, because I should study it first. In fact, I had temptation, if someone would do it for me. :). – Sandip Luhar Sep 15 '20 at 18:47
  • @Prune, Thank you for your advice : you need to learn how to search for nearly-exact solutions, within the tolerance of your algorithm. My way or learning is "err & learn". I will search for more solutions. However, can you suggest a function that may be helpful for me in this case? Please also reffer above comments. – Sandip Luhar Sep 15 '20 at 18:53
  • Search "Python approximately equal". Also see https://stackoverflow.com/questions/588004/is-floating-point-math-broken – Prune Sep 15 '20 at 18:55

1 Answers1

0

try this

print ("\n\nEnter values in below equations. \nIf you don't know the value, than press \"Enter\" ")

x = (input("\nEnter value of x: "))
y = (input("\nEnter value of y: "))
z = (input("\nEnter value of (x+y)^2: "))

#-----------------------------------------------------------------------
#Converting input to integers.
#-----------------------------------------------------------------------

a=0
b=0
c=0

if len(x)!=0:
     a = float(x)
else:
     print ("\nWe will search value of 'x'")

if len(y)!=0:
     b = float(y)
else:
     print ("\nWe will search value of 'y'")

if len(z)!=0:
     c = int(z)
else:
     print ("\nWe will search value of '(x+y)^2'")

#-----------------------------------------------------------------------
#Calculations
#-----------------------------------------------------------------------
     
from math import sqrt

fv=0 #fv = find value
rs=0 #rs = result

if len(x)==0:
     while True:
          rs = ((fv*fv) + (2*fv*b) + (b*b))
          fv = fv + 1
          if rs==c:
               print (f"\nValue of 'x' is either {(fv-1)} or {int((sqrt(c)*-1) - b)}")
               break

if len(y)==0:
     while True:
          rs = ((a*a) + (2*a*fv) + (fv*fv))
          fv = fv+1
          if rs==c:
               print (f"\nValue of 'y' is either {(fv-1)} or {int((sqrt(c)*-1) - a)}")
               break

if len(z)==0:
     rs = ((a*a) + (2*a*b) + (b*b))
     print (f"\nValue of '(x+y)^2' is {rs}")


print ("\nThank you")

just change int to float

  • No. It is not working. From my perspective, (i)float values are infinite after decimal to count, that should be limited, which may save processing power. (ii)in while structure where I put fv+1, is also an issue, I also tried there fv+0.001. My expectation is if I enter (x+2.5)^2=25, it should show me answer 2.5 or -7.5 which is running ok in case of integers. – Sandip Luhar Sep 15 '20 at 06:44
  • then we can try to multiply some value to the input which makes an int and then divide the value –  Sep 15 '20 at 07:10
  • or first, you find the square root and then subtract the other number you get the answer then we get both a and b. if they ask for (a+b)^2 then add a and b then square it works fine –  Sep 15 '20 at 07:46