PDA

Click to See Complete Forum and Search --> : A strange C++ problem????


AbhishekD
October 7th, 2004, 02:18 PM
I have got a strange c++ problem in which the code is not behaving properly as it should.

The code is

#include<iostream.h>
#include<conio.h>
void main()
{ float a=0.7;
if(a<0.7)
cout<<"c";
else
cout<<"c++";
getch();
}

The problem is that for 0.7 it prints c and for any other value like 0.6 or 0.8 it prints c++ when both 0.7 numbers are replaced.
I can,t figure out how this is so.
Please help me as fast as you can

SwordFish_13
October 7th, 2004, 04:38 PM
Hi,

Puters puters what can i say :eek7:

Ok Lets see .............Do you have any prior knowledge of Floating Point Arthematics or how computers handle Floating Point Numbers?


you are storing a double precision float value in single precision float location ..........The number 0.7 is being rounded off to around 6999999888 something like that .............thats the closed you get with double preceson ............. which is less than 0.7 thus C is printed.

to see this insert a printf statement and see the value of a printf("a=%10.10g",a);


to correct this what you can do is

if(a<0.7f)

now both sides are single precition and thus if would evaluate correctly. ..........this problem has it's root in the fact that Computer has limited Memory and the Binary thingy ............those fraction that can be easily written in decimal can streach to amzaing lengths in Binary Syatem and as we know Computer memory is still limited .

If you want to read about Floating Point Arthematic read here

The IEEE standard for floating point arithmetic (http://www.psc.edu/general/software/packages/ieee/ieee.html)

Floating Point Arithmetic: Issues and Limitations (http://docs.python.org/tut/node15.html)


--Good Luck--

Juridian
October 8th, 2004, 12:42 AM
Hi. I'd recommend you learn how to use your debugger, and liberally use print statements for debugging your code.

AbhishekD
October 8th, 2004, 08:21 AM
Hi!
Why is it not so with any other number except 0.7
I've tried many numbers but they seem to print c++.
Well I'm at college right now in hostal and will try to see the links when i reach home.
Thanx

SwordFish_13
October 8th, 2004, 08:39 AM
Hi,

Originally posted here (http://www.AntiOnline.com/showthread.php?threadid=262792#post797960) by AbhishekD
Hi!
Why is it not so with any other number except 0.7
I've tried many numbers but they seem to print c++.


Buddy listen to what Juridian said and you would have known it by itself why this is happening.

Debugging

Insert print statements.........insert watches on your variable to keep track of their values keep track .

AbhishekD Insert a printf (printf("a=%10.10g",a); ) statement into your code and keep a check of the value of the variable a and you would notice

a=0.5

printf returns .5
prints C++


a=.9

Printf returns .8999999762
Prints C

a=.11
printf returns .10999994
prints C

So it's not just .7 ...............and again it's got to do with how Floating points are handeled in BINARY .

I know it's a bit confusing............what you got to remember is we work on Desimal system and computrs work on BINARY............and until you iwll learn to create floating point represation in Binary it would be a bit difficult to understand ths unexpected result.............So try working on Floating Point Represation for a while to get a zist of it .

Using Double insted of Float or adding a f (e.g 0.7f) to the RHS of the condidion will solve your problem ......but that you have run into this problem i qould suggest you get your hands wet with Floating Point Arthametic.

Read the links i gave you about floating point arthematics or get a any Good Book on Computer Artecture it's generally there indepth.


--Good Luck---