Returning multiple values and also a problem with the class

void setup() {
}

float discriminant(float a, float b,float c ){
  float x1,x2;
  float d=(b*b)-4*a*c;
  if (d>0){
    x1=((-b)-sqrt(d))/2*a;
    x2=((-b)+sqrt(d))/2*a;
    return(x1) ;
          }
  if (d<0){
    x1=-b/2*a;
    return(x1);
  }
  else{
    println("pas de solution");
  }
    
}

i’m trying to make a program that calculates the solution of an equation of the second order , in the first case i want to return both x1 and x2 but apparently i cant .
And also if there is no solution i want the function to say with a string that there is no solutions this is killing me help pls

1 Like

You can make your function to return a float[] array containing both values:

If the function returns a float[], we can’t return a String or anything else.

However, we can return a float[] filled w/ 2 Float.NaN constants instead:
Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Float.html#NaN

/**
 * Bhaskara Akaria Quadratic (v1.0)
 * GoToLoop (2020/Jan/02)
 *
 * https://Discourse.Processing.org/t/
 * returning-multiple-values-and-also-a-problem-with-the-class/16821/2
 */

static final float[] discriminant(final float a, final float b, final float c) {
  final float d = b*b - 4*a*c;

  if (d < 0)  return new float[] { Float.NaN, Float.NaN };

  final float sqrDelta = sqrt(d), a2 = 2*a;
  final float x1 = ( -b + sqrDelta) / a2;

  if (d == 0)  return new float[] { x1, x1 };

  final float x2 = ( -b - sqrDelta) / a2;
  return new float[] { x1, x2 };
}