Mathematics

Do you think this two codes should do the same?

H and i are floats. H is between 0 and 360.

The codes are for HSB to RGB convertion. The following code does give me correct result (RGB image):

Original
1
2
H /= 60;
i=floorf(H);


And the second does not give correct results (RGB image has wrong colors).

1
2
3
4
5
6
7
8
9
10
i =
   (H<60? 0 :
        (H<120? 1 :
             (H<180? 2 :
                  (H<240? 3 :
                      (H<300? 4 : 5)
                  )
             )
        )
    ); 


I wonder where is mistake.
I get the same results except for when H == 360.0f

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
#include <iostream>
#include <cmath>
float f1(float H)
{
    H /= 60;
    float i = floorf(H);
    return i;
}
float f2(float H)
{
    float i =
       (H<60? 0 :
            (H<120? 1 :
                 (H<180? 2 :
                      (H<240? 3 :
                          (H<300? 4 : 5)
                      )
                 )
            )
        );
    return i;
}

int main()
{
    for (float i = 0; i <= 360.0f; i+=0.5f)
    {
        std::cout << f1(i) << ", " <<
                     f2(i) << std::endl;
    }
}


The different behavior when H == 360.0f exactly, H /= 60; makes H == 6.0f
Your other function only goes up to 5.
Other than that, I see no difference, although I've never done HSB to RGB conversion.
Last edited on
Topic archived. No new replies allowed.