i wanted to convert double to float in C, but wanted to preserve the decimal point exactly as possible without any changes...
for example, let's say i have
double d = 0.1108;
double dd = 639728.170000;
double ddd = 345.2345678
now correct me if i am wrong, i know that floating point precision is about 5 numbers after the dot. can i get those five numbers after the dot exactly as the double had it? so that above results as follows:
float f = x(d);
float ff = x(dd);
float fff = x(ddd);
printf("%f\n%f\n%f\n", f, ff, fff);
it should print
0.1108
639728.17000
345.23456
all digits after the precision limit (which i assume as 5) would be truncated.
x
that you are looking for is the cast from double to float(float)
. If you didn't change the rounding mode from its default of nearest-even, this cast computes thefloat
that is the closest to thedouble
you are converting. There is no better conversion than this one, so if this one loses too much precision for your taste, keep yourdouble
s as they are. – Issuancefloat
does not have that much precision (assuming it's IEEE single precision). Why do you want to usefloat
instead ofdouble
? – Razz