I have written a program in c language to roundoff floating point number but the output of the program does not follow any logic. The code is-->
#include<stdio.h>
int main(){
float a=2.435000;
float b=2.535000;
float c=2.635000;
float d=2.735000;
float e=2.835000;
//Rounding off the floating point numbers to 2 decimal places
printf("%f %.2f\n",a,a);
printf("%f %.2f\n",b,b);
printf("%f %.2f\n",c,c);
printf("%f %.2f\n",d,d);
printf("%f %.2f\n",e,e);
return 0;
}
OUTPUT:
2.435000 2.43
2.535000 2.54
2.635000 2.63
2.735000 2.73
2.835000 2.84
All the floating numbers have same pattern of digits i.e. they are in the form of 2.x35000 where x is different in different numbers. I am not getting why they are showing different behaviour while getting roundoff ,either they should give 2.x3 or 2.x4 but for different x it is giving different value.
What is the logic behind this?