You are here

"NAN" output of sprintf | Cypress Semiconductor

"NAN" output of sprintf

Summary: 2 Replies, Latest post by odissey1 on 30 Oct 2015 09:59 AM PDT
Verified Answers: 1
Last post
Log in to post new comments.
StudentA's picture
53 posts

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).

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);

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.

StudentA's picture
53 posts

Fixed the problem, it was neither to way I labeled the variables nor the sqrt(). And also not the size of my values.
Somehow the program can not multiple the values. The fault was by multiplying real_comb*real_comb.

By changing this code to magnitude=pow(real_comb,2) it worked. I think calculating the power of 2 or just multiplying with the same value should be same mathematical operation.

But nevertheless it is working, so never mind. ;)

user_342122993's picture
527 posts

Pay attention that calculating pow(n,2) takes ~5000 CPU ticks, while n*n is only about 100 ticks. Check output precision, you may be getting 'long' instead of 'long long' and that's why sprintf() was able to handle it. 

Log in to post new comments.