| | |
logic error or problem with if stmt ?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2007
Posts: 2
Reputation:
Solved Threads: 0
main()
{
float a=0.7;
if(a<0.7) cout<<"true" ;else cout<<"false";
}
problem---> it prints true .....
when 0.7 is replaced everywhere with 0.8 it displays false
again with 0.9 , it displays true..
typically only for 0.7 and 0.9 it gives different answer ,then the conventional logical answer..why so??
{
float a=0.7;
if(a<0.7) cout<<"true" ;else cout<<"false";
}
problem---> it prints true .....
when 0.7 is replaced everywhere with 0.8 it displays false
again with 0.9 , it displays true..
typically only for 0.7 and 0.9 it gives different answer ,then the conventional logical answer..why so??
change your if condition as below
I do not remember the exact reason for the behavior in your code, it has got something to do with how floats are treated by the hardware.
Someone please elaborate.
c++ Syntax (Toggle Plain Text)
if(a < 0.7f)
I do not remember the exact reason for the behavior in your code, it has got something to do with how floats are treated by the hardware.
Someone please elaborate.
This thread here talks about float comparisons. There's a good link available in the thread about the same.
Thread1
Thread1
thanks
-chandra
-chandra
•
•
Join Date: Aug 2007
Posts: 2
Reputation:
Solved Threads: 0
thank you mrinal..
i think it is because the default datatype is double ..hence to do a proper comparison of float values,we have to specify it .
thanks.
i think it is because the default datatype is double ..hence to do a proper comparison of float values,we have to specify it .
thanks.
•
•
•
•
change your if condition as below
c++ Syntax (Toggle Plain Text)
if(a < 0.7f)
I do not remember the exact reason for the behavior in your code, it has got something to do with how floats are treated by the hardware.
Someone please elaborate.
![]() |
Similar Threads
- Please Hlpe me, lojec erooe (C++)
- Logic error in my code. (C++)
- Error Problem (C++)
- Logic Error in Java Recursive Method (Java)
- Logic error? (C++)
- Erm, logic error?! (C++)
- Could someone please help me with this logic error?? (VB.NET)
- IDE Error #1 problem.... (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Help with Arrays
- Next Thread: Program help...
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





