Comparing floating point numbers

,

This is my solution for the problem ALCHEMY on SPOJ :

#include <stdio.h> int main (void) { float a = 1000, b = 37; unsigned long int x, y; float z = a / b; for (x,y; x != -1, y != -1;) { scanf ("%lu %lu", &x, &y); if ((float)x / (float)y == z && x != -1 && y != -1) printf ("Y\n"); else if (x / y != z && x != -1 && y != -1) printf ("N\n"); else if ( y == 0) return 1; else if (x == -1 && y == -1) return 876; } return 0; }

Even if I enter x and y as 1000 and 37 respectively, the first if statement is evaluated as false and it prints N instead of Y . Is it even possible to solve this problem the way I m trying and using float ? What am I doing wrong?
Please try to run it once.

see this code…LINK…i changed float to double for higher precision and also changed the condition from x/y==z to x/a == y/b…hope this helps…:slight_smile:

1 Like

But what’s the problem with x / y == z ?

Can you please tell me why it shows a runtime error qsh4Cy - Online C Compiler & Debugging Tool - Ideone.com

i think the precision is lost…as the floating point numbers are stored in the form of exp and mantissa…a slight change in the mantissa causes the loss of precision…you can refer to IEEE754 standards to represent the floating point numbers…or maybe this link…IEEE 754-1985 - Wikipedia

the RE i think…is due to NZEC that is non zero exit code…as you are returning numbers other than 0…remove that and see…it is not necessary btw…see the link i gave above!!!