Condition in if statement is violated

Hi. I have a problem whit the next code (an extract of a medical program) when I use 'if' and 'else if' statements. When the funtion f_cT is evaluated in the 'case 2' (line 41) of the 'switch' statment, and the user input the valor 4.14, for example, the variable p_cT is equal to -3, and this meaning that the condition for true in the 'if' statment (line 42) is violated, because 4.14 is not minus than 4.14 (obviously).

If you do the same with the value 160 in the 'case 1' (line 34) of the 'switch' statment, the condition of the analogous 'if' statment is respected.

What is wrong?

Thanks for your help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>

using namespace std;

int f_cT( float cT, int m_u_cT, int p_cT );

int main()
{
    float cT;  // "Total cholesterol" variable
    int p_cT;  // "Points for total cholesterol" variable
    int m_u_cT;  // "Measurement unit" variable

    while ( cT != 123456789 ) {
        cout<<"Input the amount of total cholesterol: ";
        cin>> cT;
        cin.ignore();
        cout<<"\n---------------------------\n\n";
        cout<<"Input the measurement unit:\n 1 for mg/dL.\n 2 for mmol/L\n>>> ";
        cin>> m_u_cT;
        cin.ignore();
        cout<<"\n---------------------------\n\n";
        p_cT = f_cT ( cT, m_u_cT, p_cT );
        cout<<"Points for your total cholesterol: "<< p_cT;
        cout<<"\n\n---------------------------\n\n";
    }
    cin.get();

    return 0;
}

int f_cT( float cT, int m_u_cT, int p_cT )
{
    switch ( m_u_cT ) {
    case 1:
        if ( cT < 160 ) { p_cT = -3; }
        else if ( cT >= 160 && cT <= 199  ) { p_cT = 0; }
        else if ( cT > 199 && cT <= 239 ) { p_cT = 1; }
        else if ( cT > 239 && cT < 280 ) { p_cT = 2; }
        else if ( cT >= 280 ) { p_cT = 3; }
        break;
     case 2:
        if ( cT < 4.14 ) { p_cT = -3; }
        else if ( cT >= 4.14 && cT <= 5.17 ) { p_cT = 0; }
        else if ( cT > 5.17 && cT <= 6.21 ) { p_cT = 1; }
        else if ( cT > 6.21 && cT < 7.25 ) { p_cT = 2; }
        else if ( cT >= 7.25 ) { p_cT = 3; }
        break;
    }

    return p_cT;
}
Well, you see, floating point values are very annoying things.
Run this:
1
2
3
4
5
6
7
8
#include <iostream>

int main(){
	float x=4.14f;
	std::cout <<(x==4.14f)<<std::endl //note the f
		<<(x==4.14)<<std::endl;
	return 0;
}

General suggestion: don't make exact comparisons of floating point values if you can avoid it. In this case, if you only need precision up to hundredths, use 414 to mean 4.14 in code, and immediately multiply the user's input by 100 and truncate it.
Thanks helios, I note your counsel.

A question: What uses more machine resources: a float with f or a double?
closed account (zb0S216C)
A double has a larger range than that of float, meaning that double requires more resources to hold its range.

afrodriguezg wrote:
"a float with f" (sic)

The 'f' as in the suffix? If so, that suffix only serves as a denotation to indicate the type of the numerical value proceeding it. In this case, float. It allows the compiler to recognize the literal as a float type, not a double. This is the same story with: U, L, LL, UL, and ULL

Wazzak
Last edited on
Yes I refered as a suffix. Thanks.
Topic archived. No new replies allowed.