Ask your own question, for FREE!
Computer Science 18 Online
OpenStudy (anonymous):

i'm attaching a C pgm file here. I couldn't get the reason for output. #include #include void main() { float f=0.7; if(f<0.7) printf("\n hi"); else printf("\n bi"); getch(); }

OpenStudy (anonymous):

and what is the output?

OpenStudy (anonymous):

try pasting the code at a site like http://codepad.org

OpenStudy (anonymous):

Ahh it prints "hi". Well, it's all about IEEE 754: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.17

OpenStudy (anonymous):

u should use code editors like codepad and ideone

OpenStudy (anonymous):

Specifically, in this example, you are comparing an object of type float to a double, which won't give you the expected false value.

OpenStudy (anonymous):

To solve this, add the 'f' qualifier to the number 0.7, writing it as 0.7f

OpenStudy (anonymous):

http://codepad.org/tsbI2gr9

OpenStudy (anonymous):

would it work if you defined double f =0.7?

OpenStudy (anonymous):

try it out! http://codepad.org/B2lMqdUV

OpenStudy (anonymous):

you probably always want to use double since it's more precise than float

OpenStudy (anonymous):

Right; double is safest if you aren't sure.

OpenStudy (anonymous):

and when float is better to use?

OpenStudy (anonymous):

Here's the long answer: http://docs.oracle.com/cd/E19422-01/819-3693/ncg_goldberg.html

OpenStudy (anonymous):

Short answer: it might depend on certain arithmetic you're trying to do, compiler implementation, machine implementation, FPU etc.

OpenStudy (anonymous):

thanks i got it

OpenStudy (anonymous):

the safest is variant

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!