Go Down

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

Alban

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 ;)


www.reptile-addict.nl

Arrch


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

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.

GoForSmoke

Is HSL floating point?

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Go Up