0

Hi I'm little confused Here is code

#include <iostream>
using namespace std;
int main(){
    float a=182.52;
    if(a==182.52f){
        cout << "A";
    }
    else{
        cout << "B";
    }

    return 0;
}

When i remove f literal from a==182.52f it's show me B as output because I need to convert 182.52 in float. Okay my question is that why I need to convert it to float? when I decreased 182.52 to 1.5 small values then it's working fine without f literal if it's binary size problem then why on input it's showing the same input value like below code.

#include <iostream>
using namespace std;
int main(){
    float a;
    cin >> a;
    cout << a;
    return 0;
}

why in the cin code it's showing the same output even when I input 182.52..!! May be my question confusing sorry for that..!!

Asif Mushtaq
  • 3,658
  • 4
  • 44
  • 80
  • 2
    http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html will answer all your questions. And cout won´t give you the full number because depending on the number, it´s impossible to print it in finite time. Hint: 182.52 is such a number. it´s infinite, and your computer can´t do that. – deviantfan May 02 '15 at 16:11
  • possible duplicate of [How dangerous is it to compare floating point values?](http://stackoverflow.com/questions/10334688/how-dangerous-is-it-to-compare-floating-point-values) – vsoftco May 02 '15 at 16:14
  • @deviantfan thanks for your interest in my question. My English is not good, can you told me in shortcut? mean main process with example? Sorry – Asif Mushtaq May 02 '15 at 16:14
  • Your mistake is in assuming that a `double`'s best approximation of `182.52` would be equal to a `float`'s best approximation. Those numbers are not stored the same way that you type them. – Drew Dormann May 02 '15 at 16:15
  • @AsifMushtaq No, I can´t (and don´t want to) give you a shortcut, because it´s already short. FP stuff is no easy topic at all. – deviantfan May 02 '15 at 16:16

2 Answers2

1

Here is a little example program that shows what folks are trying to explain:

#include <iostream>

using namespace std;

int main()
{
    union TestDouble {
        double d;
        uint64_t l;
    } dy;

    dy.d = 182.52;
    cout << hex << dy.l << endl;
    dy.d = 1.5;
    cout << hex << dy.l << endl;

    union TestFloat {
        float f;
        uint32_t i;
    } fy;

    fy.f = 182.52;
    cout << hex << fy.i << endl;
    fy.f = 1.5;
    cout << hex << fy.i << endl;

    return 0;
}

Here is the output on my computer:

4066d0a3d70a3d71
3ff8000000000000
4336851f
3fc00000

Without going into the details of the IEEE754 standard, what should be obvious is that numbers like 1.5 can be represented exactly as 32 bit float and 64 bit double formats while numbers like 182.52 cannot, which shouldn't be too surprising since 52/100 = 13/25 and 25 is not a power of two so this fraction cannot be represented exactly in binary.

sfjac
  • 7,119
  • 5
  • 45
  • 69
-1

By default the 182.52 is treated as double. We use f to force the compiler to treat the number as a float.

For example, see the code below:

float x = 1.2;
if ( x == 1.2 )
    cout << "x is equal to 1.2"<<endl;
else
    cout << "x is not equal to 1.2"<<endl;

float y = 1.2f;
if ( y == 1.2f )
    cout << "y is equal to 1.2f"<<endl;
else
    cout << "y is not equal to 1.2f"<<endl;

Output:

x is not equal to 1.2

y is equal to 1.2f

Hope this helps!

Moiz Sajid
  • 644
  • 1
  • 10
  • 20
  • ...and how does this explain the problem? Comparing different types like `int` and `char` will work without any problems. – deviantfan May 02 '15 at 18:23
  • @Moiz When I change value of a to 1.5 then it's working fine without using f.. :/ why? – Asif Mushtaq May 02 '15 at 18:28
  • @AsifMushtaq You should really really read the link in the comments above. Short answer, 1.5 is finite and fulfills all other requirements for all sizes of IEEE754 variables, but 182.52 is not finite, thus it´s impossible for your computer to handle it correctly. If you don´t understand what I mean, read the link. – deviantfan May 02 '15 at 18:51