0
scanf("%lf",&alpha);

alpha = (alpha * PI)/180;

if(alpha==PI/2)

{

printf("0");

}

i also defined PI and declared alpha...it just skipping this if and i don't know why

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • 1
    related: [How should I do floating point comparison?](http://stackoverflow.com/q/4915462/4279) – jfs Nov 09 '13 at 14:27
  • relaed: [Most effective way for float and double comparison](http://stackoverflow.com/q/17333/4279) – jfs Nov 09 '13 at 14:28
  • pi is not representable. It is transcendental!! – David Heffernan Nov 09 '13 at 14:37
  • Also consider using the value of pi from math.h #define M_PI 3.14159265358979323846 – EvilTeach Nov 09 '13 at 15:14
  • Your question does not include either the declaration of `alpha`, the definition of `PI`, or the input you provide to the program. This information is critical to diagnosing the specific problem you are encountering. – Eric Postpischil Nov 09 '13 at 19:39
  • 1
    This does not appear to be a floating-point rounding problem. When I define `PI` to be the `double` closest to π and perform the calculations shown in the code using either IEEE-754 64-bit binary floating-point or Intel’s 80-bit format, in any combination permitted by the C standard, with input of “90”, the comparison always returns true. In other words, the roundings permitted by C do not cause the problem reported in the question unless some other floating-point format is being used or the source text defining `PI` did not result in the `double` closest to π. – Eric Postpischil Nov 09 '13 at 19:46

2 Answers2

2

Equality and floating point numbers do not go down well. You have rounding errors.

Need to put in some tolerance.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • Actually, the comparison is the only floating-point operation in the code guaranteed not to have any errors. The `scanf` may round when converting from decimal to floating-point, the multiplication and division may round, and the assignment to `alpha` might round an extended-precision intermediate result to the nominal type. In contrast, the `==` operator for floating-point returns true if and only if its operands are two numbers with exactly the same value. – Eric Postpischil Nov 09 '13 at 19:41
1
#include <stdio.h>

double PI = 3.14159265359;

int main (void)
{
    double alpha = 90.0;

    scanf("%lf",&alpha);

    alpha = (alpha * PI)/180;

    if(alpha==PI/2.0)
    {
        printf("0");
    }
}

Enter 90 and the 0 gets printed. Works as expected, did you declare alpha as float? Then you would have to change the scanf to "%f" to get correct results.

user2859193
  • 434
  • 3
  • 11