Pages: [1]   Go Down
Author Topic: math problem  (Read 256 times)
0 Members and 1 Guest are viewing this topic.
Near Rotterdam in Holland
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Best regards,

Alban

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


www.reptile-addict.nl

California
Offline Offline
Faraday Member
**
Karma: 91
Posts: 3426
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50039
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is HSL floating point?

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Pages: [1]   Go Up
Jump to: