English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

can u tell me why the output of the following code is like this....
#include
using namespace std;
int main()
{
float a=12.35;
if(a>12.35)
cout<<"correct";
else cout<<"wrong";
return 0;
} the answer using linux compilers is correct ...... why is that so? can u tell me the reason why?

2007-03-30 18:15:07 · 7 answers · asked by sanchu@cse 2 in Computers & Internet Programming & Design

7 answers

i faced that problem b4 ... and i found the best way to compare such a numbers, is to multiply it * 10^4 or 10^5, then convert it to integer, then compare the integers.

int b = a*10^5;
if(b > 1235E3)

2007-03-30 23:55:07 · answer #1 · answered by Mena M 3 · 0 0

when you compare variables it depends on the way a variable is stored in the memory. it differs from OS to OS in windows/ dos
float a=12.35; >> will be stored in to memory as 12.35000
where as in UNIX or LINUX it will be rounded and stored as 12.4
so, when you check a>12.35 then it will check as 12.4>12.35 its true so, the output will be "correct" but in the case of Windows or dos, the output will be "wrong"

2007-04-01 18:54:33 · answer #2 · answered by Arul 2 · 0 0

It has to do with the way floating point numbers are stored. 12.35 gets stored as 12.344444449 or something similar.

If you change the float to a double it will behave as you would expect.

2007-03-31 01:38:37 · answer #3 · answered by Vegan 7 · 1 0

real or floating point numbers can't always be represented exactly in he computer memory...
it is due to the way they are represented in memory...
so its best to avoid comparision operators with them....

2007-04-01 04:24:43 · answer #4 · answered by thekillerkat 3 · 0 0

I don't but by changing the condition in if class i found that 12.350

2007-03-31 06:39:02 · answer #5 · answered by Gopinath M 3 · 0 0

floating numbers can not be compared for == > ,etc since they are approximated

2007-03-31 02:26:51 · answer #6 · answered by Anonymous · 0 0

so i opened this message thinking it said floating vibrators....sorry i cant help u on this one

2007-03-31 01:23:34 · answer #7 · answered by Anonymous · 0 2

fedest.com, questions and answers