Home > Mobile >  Why Do I get the error "integer literal is too large to be represented in any integer type"
Why Do I get the error "integer literal is too large to be represented in any integer type"

Time:03-19

I know that according to the single point standard IEEE the maximum value of a float is 1.11111111111111111111111 *2^127 which converted in decimal is exactly this huge number -> 340282346638528859811704183484516925440.

How came that I get the error "integer literal is too large to be represented in any integer type" when trying to assign this value to a float like this :

int main(){

float biggest=340282346638528859811704183484516925440;

}

Shouldn't the compiler say "ok, this is the maximum value for a single float point number and I have to convert it to the 32bit value 01111111011111111111111111111111" where the first bit is the sign bit, the next eight bits represent the exponent and everything else the mantissa ?

CodePudding user response:

When 340282346638528859811704183484516925440 appears in source code, without a . or other characters that would modify its type, it is an integer literal, as specified by C 2018 6.4.4.1, which calls it an integer constant. Formally, then, the compiler interprets it as an integer type. The value represented by this numeral is too large to represent in any integer type your compiler supports, so your compiler warns you about it.

The fact the value being assigned to a float is insufficient. The compiler must process the numeral itself before it has a value that it can convert to a float.

The preferred way to get the maximum finite float value is to include <float.h> with an #include directive and use FLT_MAX.

If you want to use the numeral, you can make it a floating-point constant by suffixing .. You can also suffix f to make it a float constant rather than double: 340282346638528859811704183484516925440.f.

The C standard allows some slack in how implementations parse floating-point constants, so it may not be guaranteed that the compiler produces the exact value the numeral represents, even if it is representable in the type. Good C implementations will. However, you can avoid this issue by using the hexadecimal floating-point form, which is guaranteed to produce the correctly rounded result if the C implementation uses a base-two floating-point format. In this form, the number is 0x1.fffffep127f.

CodePudding user response:

As it does not have a decimal point nor an exponent, the number in your program is not parsed as a decimal floating point constant but as an integer constant and it does exceed the maximum value of all integer types available on your system. Your assumption about the compiler behavior is incorrect: the token type is determined from the syntax, not the context. Storing the value of an integer constant to a float involves a conversion to type float, performed at compile time or at run time.

To specify the number as a float constant, add a decimal point and an F suffix at the end:

int main() {
    float biggest = 340282346638528859811704183484516925440.F;
}

If you just write 340282346638528859811704183484516925440. the constant will have type double and will be converted to type float when stored to biggest, which may or may not change the value.

  • Related