3
eq = (((1 - (2 * normal_rpm - s8) ** s1) * s2 * math.sin(normal_rpm ** s3 / s4) * (1 - math.sin((normal_rpm + s5) ** (2) + 5) + s6) / (s7))) + 0.67

is my formula for this variable, where the S variables are floats. this sometimes returns a result like this

(0.6806708980989302+0.008606807113252896j)

I cannot use this result in further math, I need a float, even if I have to round the answer a bit.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Apparently the result of this expression is not a real number. Can you explain by which rules a resulting complex number should be changed to a `float`? – mkrieger1 May 14 '20 at 19:03
  • What is the expected output? – mad_ May 14 '20 at 19:04
  • `math.sin(normal_rpm ** s3 / s4)` raising a number to a fractional exponent will give a complex result if `normal_rpm` is negative – Alan Hoover May 14 '20 at 19:11

1 Answers1

3

This is not a rounding problem, but you are raising a negative number to a fractional exponent (e.g. you're taking the square root of -5).

For example:

 In [2]: (-5)**0.5                                                                     
 Out[2]: (1.3691967456605067e-16+2.23606797749979j)

If you cannot accept complex numbers as result then the only other logical way out is to raise an error when this happens (there is no real number that multiplied by itself gives, or gets near, -5).

If this is not expected you should double-check the formula or formulas preceding it because may be there is a typo, or may be there are some preconditions you need to check before applying this formula.

6502
  • 112,025
  • 15
  • 165
  • 265
  • you are correct, it seems I am raising a negative number by a fractional exponent, however, I can put the complex number on a graph with marplot lib. is there any way I can convert complex to float? – Javier Albarracin May 14 '20 at 19:10