开发者

float + float = NAN

开发者_如何学Gofloat jk = 7700;  float ck = 8000;  - if i do int jk;  I get rim=0;

printf (asin(jk/10500)) = 1.57897  - for example
printf (asin(ck/9500)) = 0.87868  - for example

float rim;
rim= asin(jk/10500)+ asin(ck/9500);

printf("\n%f", rim) = nan

Why i get nan?


I don't believe your "for example". Because I don't believe in magic. If you have two valid floats both pretty small, then their sum is not a nan. So, my guess is this:

either |jk| > 10500 or |ck| > 9500. So you make asin with an invalid ( > 1.0 or < -1.0) argument and thus get a nan.

Or you have made another error. Please post a compilable runnable example which will print NAN


There's either something wrong with your code or something seriously wrong with the iphone. The following code:

#include<stdio.h>
#include<math.h>
int main (void) {
    printf ("%f\n", asin(1));
    printf ("%f\n", asin(0.5));
    float rim;
    rim = asin(1) + asin (0.5);
    printf ("%f\n", rim);
    return 0;
}

produces a more sensible:

1.570796
0.523599
2.094395

In other words, both your asin(0.5) and your sum are incorrect.

Are you sure that you didn't actually do something like:

rim = asin(1 + asin (0.5));

That will indeed give you NaN.


Update based on your added info:

Your code still works fine in my environment:

#include<stdio.h>
#include<math.h>
int main (void) {
    float jk = 7700;
    //jk = 7700/10500;
    jk = jk/10500;
    printf ("%f\n", asin(jk));
    float hg = 8000;
    hg = hg / 9500;
    printf ("%f\n", asin(hg));
    float rim;
    rim = asin(jk) + asin (hg);
    printf ("%f\n", rim);
    return 0;
}

outputting:

0.823212
1.001175
1.824387

You'll notice I changed jk = 7700/10500 to jk = jk/10500 since the latter gave you 0 due to the integer division, but I don't get NaN in either case. You really need to post a complete program which shows the errant behaviour.


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

main()
{
float jk=7700, ck=8000;
printf ("\n%f",asin(jk/10500));
printf ("\n%f",asin(ck/9500));

float rim;
rim= asin(jk/10500)+ asin(ck/9500);

printf("\n%f", rim);// = nan
}

Output

0.823212
1.001175
1.824387


I think your code is right. The problem is the value of jk and ck.

You kown if |jk/temp|>1 or |ck/temp|>1, the return of asin(jk/temp) will be nan.

so try to make |jk/temp|<=1 and |ck/temp| <=1, I believe that the return will be ok.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜