Loading...
Logo
Processing Forum
Hey Folks,

this might be a total newbie question, but I can't figure it out.

So I am making a calculation in a float that brings me a NaN value (that is because the result of the computation is a complex number... for example sqrt(-1)  ).

How can I convert this float into a complex value.

I used
http://commons.apache.org/math/api-2.0/org/apache/commons/math/complex/Complex.html

but there they only tell you how to create a Complex Number from two values. I only have my float computation....

My ultimate goal is to return just the real-value of my computation (not sqrt(-1), something more complicated....)

Thanks for any help!

Replies(1)

if you expect the result to be complex, then you should (probably) be evaluating the entire expression as complex - just wrap each float "f" as a complex <f,0>.  for example, compare these two approaches:
 
Copy code
  1. import org.apache.commons.math.complex.*;
  2. float af = 2.0f;
    float bf = 3.0f;
    float cf = sqrt(af-bf); // FAILS
    println(cf);
  3. Complex ai = new Complex(2.0f,0.0f);
    Complex bi = new Complex(3.0f,0.0f);
    Complex ci = ai.subtract(bi).sqrt(); // WORKS
    println(ci.getReal() + "," + ci.getImaginary());