Code return problem (second-degree equation)

Asked

Viewed 86 times

0

The return of the code below is "-Nan", and I am not identifying the error.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

struct variaveis
{
    float a,b,c;
};

float delta (float a, float b, float c){
    float a1=a,b1=b,c1=c;
    a1 = pow(b1,2) - 4*a1*c1;
    return a1;
}

float x1 (float a,float b,float c){
    float a1 = a, b1=b, c1=c;

    printf("x1 = %f\n", (b*-1 + sqrt(delta(a,c,b)))/2*a);

}

float x2(float a, float b, float c){
    float a1=a,b1=b,c1=c;
    printf("x2 = %f\n", (b1*-1 - sqrt(delta(a1,b1,c1)))/2*a1);

}

int main(){
    float a, b, c;

    scanf("%f %f %f", &a, &b, &c);

    x1(a,b,c);
    x2(a,b,c);

    return 0;
}
  • Your program only calculates real roots. Check if delta is negative and, in this case, calculate the imaginary roots of your equation (a+b*i) or report that it has no roots in the real field.

  • It’s always best to use double (*) when floating comma values are desired. (*) Remember to fix scanfs

2 answers

3

Many strange things, but the nan gave up because you did delta(a,c,b), reversing the values of b and c. This is possibly implying a negative return from delta and thus try to calculate the square root of a negative value, entering the domain of complex numbers, not supported by the function.

Other details are:

1) Declare a struct and not to use it:

struct variaveis
{
    float a,b,c;
};

2) Set return float of no-return functions:

float x1 (float a,float b,float c){
    float a1 = a, b1=b, c1=c;

    printf("x1 = %f\n", (b*-1 + sqrt(delta(a,c,b)))/2*a);

}

float x2(float a, float b, float c){
    float a1=a,b1=b,c1=c;
    printf("x2 = %f\n", (b1*-1 - sqrt(delta(a1,b1,c1)))/2*a1);
}

3) Redeclare variables unnecessarily in many places:

float a1 = a, b1=b, c1=c;

4) Do not use parentheses to ensure that operators take precedence:

(b*-1 + sqrt(delta(a,c,b)))/2*a

Here the value will be divided only by 2 and multiplied by a, undivided by 2*a as it should be. It needs to be /(2*a).

  • Thank you for the excellent correction. The struct is because I’m studying threads in college, and I read that thread parameters are passed through structures. It hasn’t been used yet but I intend to, because I need to make a second degree equation through threads. I haven’t touched C in a long time.

0

When working with floating point in C, one should check the exceptions of the floating point environment (Floating Point Environment) by enabling the #pragma FENV_ACCESS

In terms of your problem, all you had to do was identify that the delta function can generate negative numbers. The sqrt() function triggers the FE_INVALID (C99) exception when negative numbers are passed as argument to the function. Thus, it was sufficient to add the following check after the calculation of the roots:

if(fetestexcept(FE_INVALID)) 
      puts("FE_INVALID was raised");

This and more information can be found in the corresponding link: https://en.cppreference.com/w/c/numeric/fenv/FE_exceptions

Browser other questions tagged

You are not signed in. Login or sign up in order to post.