Go Down

### Topic: math problem (Read 550 times)previous topic - next topic

#### Alban

##### Jan 20, 2013, 10:34 pm
Hi,

I am writing a piece of code to convert RGB colours to HSL

Can anyone explain to my why the code below gives me 2 different results?
float _H;
float temp;
temp = 1/6 * (((_R - _G)/Delta) + 4);
_H = 0.1666667 * (((_R - _G)/Delta) + 4);

somehow the 1/6 is the problem... temp always returns 0. while _H returns the correct value :s

Best regards,

Alban

P.S. sorry for asking silly questions... I'm a mechanical engineer and not an electronics engineer

#### Arrch

#1
##### Jan 20, 2013, 10:35 pm

Hi,

I am writing a piece of code to convert RGB colours to HSL

Can anyone explain to my why the code below gives me 2 different results?
float _H;
float temp;
temp = 1/6 * (((_R - _G)/Delta) + 4);
_H = 0.1666667 * (((_R - _G)/Delta) + 4);

somehow the 1/6 is the problem... temp always returns 0. while _H returns the correct value :s

1/6 is 0 according to integer math.

You're looking for 1.0/6.0

#### PaulS

#2
##### Jan 20, 2013, 10:35 pm
Code: [Select]
` temp = 1/6 * (((_R - _G)/Delta) + 4);`
What result do you get when you divide 1 by 6? 0 with a remainder. The remainder is thrown away.

1.0/6.0 is a different story.
The art of getting good answers lies in asking good questions.

#### GoForSmoke

#3
##### Jan 20, 2013, 10:42 pm
Is HSL floating point?