AlexBrand
AlexBrand

Reputation: 12401

Data types in C

A long double is known to use 80 bits.

2^80 = 1208925819614629174706176;

Why, when declaring a variable such as:

    long double a = 1208925819614629174706175; // 2^80 - 1

I get a warning saying: Integer constant is too large for its type.

Upvotes: 2

Views: 440

Answers (3)

AnT stands with Russia
AnT stands with Russia

Reputation: 320361

Firstly, it is not known how many bits a long double type is using. It depends on the implementation.

Secondly, just because some floating-point type uses some specific number of bits it does not mean that this type can precisely represent an integer value using all these bits (if that's what you want). Floating-point types are called floating-point types because they represent non-integer values, which normally implies a non-trivial internal representation. Due to specifics of that representation, only a portion of these bits can be used for the actual digits of the number. This means that your 2^80 - 1 number will get truncated/rounded in one way or another. So, regardless of how you do it, don't be surprised if the compiler warns you about the data loss.

Thirdly, as other answers have already noted, the constant you are using in the text of your program is an integral constant. The limitations imposed on that constant have nothing to do with floating-point types at all. Use a floating-point constant instead of an integral one.

Upvotes: 5

Marcin
Marcin

Reputation: 12590

The value 1208925819614629174706175 is first crated as a const int, and then converted to a long double, when the assignment happens.

Upvotes: 2

Thom Smith
Thom Smith

Reputation: 14086

1208925819614629174706175 is an integer literal, not a double. Your program would happily convert it, but it would have to be a valid integer first. Instead, use a long double literal: 1208925819614629174706175.0L.

Upvotes: 15

Related Questions