If Statement when x is near a value

后端 未结 2 498
隐瞒了意图╮
隐瞒了意图╮ 2020-12-22 01:26

I am trying to solve the numerical equation:

sin^2(x)tan(x) = 0.499999

Using a while loop in C. However I was only able to get program to print an answer if

相关标签:
2条回答
  • 2020-12-22 02:17

    Use a relative difference, such as:

    #include <math.h>
    
    static inline double reldiff(double x, double y)
    {
        return fabs(x - y) / fmax(fabs(x), fabs(y));
    }
    

    Now your test becomes:

    if (reldiff(x, y) < 0.01)   // 1% as requested
    
    0 讨论(0)
  • 2020-12-22 02:24

    Well, a simple solution would be.

    if (y >= x-x/100 && y <= x+x/100)
    {
       [execute code...]
    }
    

    Essentially what it does is check if y is between [x minus 1% of x] and [x plus 1% of x]. Should work alright.

    0 讨论(0)
提交回复
热议问题