Namespaces
Variants
Views
Actions

Real floating types

From cppreference.com
< c‎ | language

When a value of real floating type is converted to a real floating type, if the value being converted can be represented exactly in the new type, it is unchanged. If the value being converted is in the range of values that can be represented but cannot be represented exactly, the result is either the nearest higher or nearest lower representable value, chosen in an implementation-defined manner. If the value being converted is outside the range of values that can be represented, the behavior is undefined. Results of some implicit conversions may be represented in greater range and precision than that required by the new type (see Usual arithmetic conversions and Return statement).

[edit] Example

#include <stdio.h>
#include <float.h>
 
int main(void)
{
    /* The value being converted can be represented exactly in the new type. */
    /* The value is unchanged in the new type.                               */
    double d = 1.0;
    float f = d;
    printf("%f\n", f);
 
    /* The value being converted is in the range of values that can be represented */
    /* in the new type but cannot be represented exactly.                          */
    /* implementation defined                                                      */
    d = 1.23456789012345;
    printf("%.14f\n", d);
    f = d;
    printf("%.14f\n", f);
 
    /* The value being converted is outside the range of values that can be */
    /* represented in the type.                                             */
    /* undefined behavior                                                   */
    d = DBL_MAX;
    f = d;
    printf("%f\n", f);
 
    return 0;
}

Possible output:

1.000000
1.23456789012345
1.23456788063049
inf