floating point - Float vs Decimal in C -
this question has answer here:
- is floating point math broken? 20 answers
the following piece of code :
int main() { float a=0.7; printf("%.10f %.10f\n",0.7, a); return 0; }
gives 0.7000000000 0.6999999881 answer while following piece of code :
int main() { float a=1.7; printf("%.10f %.10f\n",1.7, a); return 0; }
gives 1.7000000000 1.7000000477 output.
why in first case upon printing got value less 0.7 , in second case more 1.7?
when pass floating-point constant printf
, passed double
.
so not same passing float
variable "the same" value printf
.
change constant value 0.7f
(or 1.7f
), , you'll same results.
alternatively, change float a
double a
, , you'll same results.
option #1:
double = 0.7; printf("%.10f %.10f\n",0.7,a); // passing 2 double values printf
option #2:
float = 0.7; printf("%.10f %.10f\n",0.7f,a); // expanding 2 float values double values , passing them printf
Comments
Post a Comment