Search code examples
ctypesfloating-pointlimit

what's the largest number float type can hold?


I'm new to programming and have recently come up with this simple question . float type has 32 bits in which 8 bits are for the whole number part (the mantissa). so my question is can float type hold numbers bigger than 255.9999 ?

and I would also appreciate if someone told me why this code is behaving unexpectedly. Is it a related issue?

int main(){
float a=123456789.1;
printf("%lf",a);
return 0;
}

for which the output is :

123456792.000000

Solution

  • <float.h> -- Numeric limits of floating point types has your answers, specifically...

    • FLT_MAX
    • DBL_MAX
    • LDBL_MAX

    maximum finite value of float, double and long double respectively

    ...and...

    • FLT_DIG
    • DBL_DIG
    • LDBL_DIG

    number of decimal digits that are guaranteed to be preserved in text -> float/double/long double -> text roundtrip without change due to rounding or overflow

    That last part is meant to say that a float value longer (i.e. more significant digits) than FLT_DIG is no longer guaranteed to be precisely representable.