floating point - Float vs Decimal in C -


this question has answer here:

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

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -