1

I am trying to print the next results on my terminal but something happens when I try placing the cos() function inside the asin(). This is my code:

#include "stdio.h"
#include "math.h"

int main() {

   float R7, R4, Roc2Y;

   //Calculo de los angulos
   
   Roc2Y = asin(-R7) * 180.0 / PI;

   Roc2Z = asin(R4 / cos(Roc2Y)) * 180.0 / PI;

   printf("\n"); //Linea en blanco
   printf("   Resultado:      \n");
   printf("   Angulo r7       \n");
   printf("   RY=%f\n", Roc2Y    );
   printf("   RZ=%f\n", Roc2Z    ) ;
}

And this is what it prints out on terminal:

Resultado:
  Angulo r7
   RY=11.506551
   RZ=nan

Some help would be appreciated!! Thanks in advance :)

chqrlie
  • 131,814
  • 10
  • 121
  • 189
ronny
  • 41
  • 7
  • 2
    Looks like you need to figure out why `asin(p_resultados->R4/cos(p_resultados->Roc2Y))*180.0/PI` evaluates to `NaN`. I recommend basic debugging techniques: either print variable values, or use a debugger to look at them, before that line. – hyde Jun 17 '22 at 07:06
  • 1
    Looks like your your `asin` produces results in radian, then your `Roc2Y` stored the result in degrees. Shouldn't your `cos` function expect radian instead? `asin` of anything greater than 1 or less than -1 will result in nan – qrsngky Jun 17 '22 at 07:11
  • 3
    You should provide a [mre], so that we can copy paste your code and compile to test our own, this speeds up the helping process. In this particular case there is no need for us to use the `p_resultados` struct, nor the array `Roc2`. – Fra93 Jun 17 '22 at 07:13
  • 1
    Also, what are the values of `R7` and `R4`? Seems that the solution of the problem lies in what those numbers are. – Fra93 Jun 17 '22 at 07:14
  • 1
    Suggestion: Only use radians internally and only convert the values to degrees when presenting them. – Ted Lyngmo Jun 17 '22 at 07:16
  • Maybe make an answer to close this question – Mike Jun 17 '22 at 07:17
  • I looks like the value passed to asin is out of bound (-1...+1), . Also, you are mixing degrees and radiants, in patricular in "cos(p_resultados->Roc2Y)". "cos" wants radiants, but you are feeding it with degrees. – Giuseppe Guerrini Jun 17 '22 at 07:17

1 Answers1

3

Your code has undefined behavior because neither R7 nor R4 are initialized. The computation results are meaningless, if the argument to asin happens to have a value outside the range [-1;1], the function returns NaN, not a value.

Note also that the standard headers "stdio.h" and "math.h" should be written <stdio.h> and <math.h>.

The variable Roc2Z is not defined anywhere in the posted code, neither is PI, which is not an identifier defined in the C standard. M_PI is defined on POSIX systems, but this macro is not specified by the C Standard, so it may not be available on your system, especially if compiling for strict conformance to the Standard.

The value Roc2Y passed to cos(Roc2Y) is in degrees instead of radians. It is better to make all computations in radians and convert to degrees for display only.

Here is a modified version:

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

#ifdef M_PI
// M_PI is defined on POSIX systems
#define PI  M_PI
#else
#define PI  3.14159265358979323846264338327950288
#endif

int main() {

    double R7 = 0.5, R4 = 0.5;

    //Calculo de los angulos
   
    double Roc2Y = asin(-R7);
    double Roc2Z = asin(R4 / cos(Roc2Y));

    printf("\n"); //Linea en blanco
    printf("   Resultado:      \n");
    printf("   Angulo r7       \n");
    printf("   RY=%f\n", Roc2Y * 180.0 / PI);
    printf("   RZ=%f\n", Roc2Z * 180.0 / PI);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • The value of `pi` is provided by `math.h` library, defined as `M_PI`. See this- https://stackoverflow.com/a/1727896/19323916 – L_R Jun 17 '22 at 09:20
  • 1
    @user1234: `M_PI` is defined in `` on POSIX systems, but it is not part of the C Standard, not even C2x. Answer amended with an explanation. – chqrlie Jun 17 '22 at 09:55