mercredi 8 avril 2015

IF statements evaluates incorrectly [duplicate]


This question already has an answer here:





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

int main(){
double t1,t2,v;
t1 = 147.81;
t2 = 147.80;
printf("Value of absolute value of t1-t2 is %lf\n", fabs(t1-t2));
if(fabs(t1-t2) < 0.010000){
printf("Points are same.\n");
}
else printf("Points are different.\n");
v=fabs(t1-t2);
printf("Value of v is %lf\n", v);
if(v < 0.010000){
printf("True1\n");
}
else printf("False1\n");
if(0.010000 < 0.010000){
printf("True2\n");
}
else printf("False2\n");
}


I am trying to compare two real numbers(i will use this in program that compares values of x or y coordinates of two different points). That is i want my program to print that two number are same if they differ by less than 0.1. However it seems that my program evaluates fist two if statements incorrectly. When i run this program i get:



Value of absolute value of t1-t2 is 0.010000
Points are same.
Value of v is 0.010000
True1
False2

Aucun commentaire:

Enregistrer un commentaire