"NAN" output of sprintf

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hello again 😉
 

   

I have a little problem with sprintf. Basically I am calculating the magnitude out of my measured real part and imaginary part (impedance).

   

real_comb_quad=(real_comb*real_comb);
imaginary_comb_quad=(imaginary_comb*imaginary_comb);
magnitude=(long double)(sqrt(real_comb_quad+imaginary_comb_quad));

   

The squared values (real_comb_quad, imaginary_comb_quad) are both "long" variables. I get 9 digits long numbers and via sprintf it is no problem to get them on the screen via UART. But when I then try to calculate the magnitude (row3), my output shows me "NAN". 

   

sprintf(TransmitBuffer, "magnitude: %Lf\n", magnitude);
 UART_1_PutString(TransmitBuffer);

   

My variable "magnitude" is "long double", so I choose %Lf as the argument for sprintf. I wrote a code with the same calculations but with small numbers and it is working fine. So is it a problem because of my huge values?

   

Thanks in advance for your response.

0 Likes
2 Replies