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

why cant i use == or != operators for floating point numbers in c++ programmes?

2007-02-28 21:38:46 · 2 answers · asked by book lover 1 in Computers & Internet Programming & Design

2 answers

Very good question. To understsnd the answer, you need to know how the float numbers are saved inside the computesr. The float numbers are saved as two different numbers, fraction and power. e.g. 0.05 will be first translated as 5 * 10^-2. And now the numbers 5 and -2 will be saved together. Clearly comparing two float numbers takes comparing these the fraction and the power part. Normally its not the problem with the power, but the fraction part. The computaion of this can make it different. So 5.0 and 5.00 could be different. Infact 5.0 and 4.9999 could be same.

2007-02-28 22:05:33 · answer #1 · answered by manoj Ransing 3 · 1 0

because floating point are approximated upto certain decimal points, so == does not make sense rather use abs(f1-f2) < a small number

2007-03-01 05:42:52 · answer #2 · answered by Anonymous · 1 0

fedest.com, questions and answers