Hải Phong
Hải Phong

Reputation: 5144

In C, is specifying 2.0f the same as 2.000000f?

Are these lines the same?

float a = 2.0f;

and

float a = 2.000000f;

Upvotes: 10

Views: 962

Answers (4)

Sergiu Dumitriu
Sergiu Dumitriu

Reputation: 11601

Yes, it is. No matter what representation you use, when the code is compiled, the number will be converted to a unique binary representation. There's only one way of representing 2 in the IEEE 754 binary32 standard used in modern computers to represent float numbers.

Upvotes: 11

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272487

The only thing the C99 standard has to say on the matter is this (section 6.4.4.2):

For decimal floating constants ... the result is either the nearest representable value, or the larger or smaller representable value immediately adjacent to the nearest representable value, chosen in an implementation-defined manner.

That bit about "implementation-defined" means that technically an implementation could choose to do something different in each case. Although in practice, nothing weird is going to happen for a value like 2.

It's important to bear in mind that the C standards don't require IEEE-754.

Upvotes: 9

John Brodie
John Brodie

Reputation: 6017

Yes, they are the same.

Simple check: http://codepad.org/FOQsufB4

int main() {
printf("%d",2.0f == 2.000000f);
}

^ Will output 1 (true)

Upvotes: 3

Shehabic
Shehabic

Reputation: 6877

Yes Sure it is the same extra zeros on the right are ignored just likes zeros on the left

Upvotes: 1

Related Questions