10

Are these lines the same?

float a = 2.0f;

and

float a = 2.000000f;
leemes
  • 44,967
  • 21
  • 135
  • 183
Hải Phong
  • 5,094
  • 6
  • 31
  • 49
  • 2
    If (2.0f == 2.000000f) then yes. – Steve Wellens Jan 05 '13 at 03:06
  • Well, what happens if you try that? Did you look at the output from the compiler? – Brian Roach Jan 05 '13 at 03:06
  • please first try then ask – Shehabic Jan 05 '13 at 03:13
  • 10
    Simply trying things out rarely gives you definitive answers in C or C++. – Benjamin Lindley Jan 05 '13 at 03:20
  • 2
    It's not about whether `2.0f` is equal to `2.000000f`, but the general question whether such different representations have any influence on the executable or not. And the fact that on one machine using one compiler there's no difference doesn't mean that it will always be true on any platform/compiler/compilation flags. – Sergiu Dumitriu Jan 05 '13 at 03:21
  • 1
    The number one rule on SO is to answer the effing question _politely_. Wanna go re-read the [faq]? We are here to answer questions, not to demand proof or anything else. – jcolebrand Jan 05 '13 at 09:33

4 Answers4

11

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.

Sergiu Dumitriu
  • 11,455
  • 3
  • 39
  • 62
  • However, there are other standards (I'm thinking of IBM 360) still in use where numbers have multiple representations... (not sure if 2 is one of them, though) – Oliver Charlesworth Jan 05 '13 at 03:10
  • 1
    IEEE 754 allows multiple representations of the same value, notably in decimal floating point. E.g., 2 might be, in effect, 2e0 and 20e-1. However, the source texts “2.0f” and “2.000000f” are unlikely to result in different representations, and there is no difference in mathematical value. – Eric Postpischil Jan 05 '13 at 03:42
  • But computer programs don't use the decimal representations at all, and in binary representations you can't do that, since there's an implicit `1.` prepended to the _fraction_ part of the number. I edited the answer to make it clearer that the binary32 representation is used. – Sergiu Dumitriu Jan 05 '13 at 04:14
  • 1
    Whether a program uses decimal floating point or not is dependent on the C (or other language) implementation. It is simply untrue to say computer programs do not use them. Why else would they exist? – Eric Postpischil Jan 05 '13 at 05:10
9

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.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • Now, if IEEE-754 **is** used, the number `2` is exactly representable, and its neighbors ("the larger or smaller representable value immediately adjacent to the nearest representable value") are `1.99999988` and `2.00000024`. We can see that the neighbor to the right is twice as far away because we have one bit less for the fractional part when we move up to the next "duade" `2.0` to `4.0`. – Jeppe Stig Nielsen Jan 05 '13 at 11:06
3

Yes, they are the same.

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

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

^ Will output 1 (true)

John Brodie
  • 5,909
  • 1
  • 19
  • 29
  • 3
    Well, proving by an exemplary compiler is not a proof. (Your answer is of course correct, but the check isn't a proof.) – leemes Jan 05 '13 at 03:13
  • 1
    You're correct, I should have used different wording. It seemed sufficient in this case, though. The IEEE standard could be referenced for a more thorough answer. – John Brodie Jan 05 '13 at 03:15
1

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

Shehabic
  • 6,787
  • 9
  • 52
  • 93