Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting some C# code to Javascript [duplicate]

Tags:

javascript

c#

I'm working on converting some C# code to Javascript. I have the following code snippet.

float goldenRatioConjugate = 0.618033988749895f;
float currentHue = (float) random.NextDouble();

currentHue += goldenRatioConjugate;
currentHue %= 1.0f;

My question is I don't understand what the last line is doing? I've never seen a modulo operation with a float.

like image 336
Mike Glaz Avatar asked Aug 16 '26 12:08

Mike Glaz


2 Answers

It will set currentHue to the fractional portion of currentHue

For example:

  • 1.5 will become 0.5
  • 3.488 will become 0.488
like image 159
pquest Avatar answered Aug 18 '26 03:08

pquest


Let suppose

currentHue = 2.5f;
currentHue = (currentHue % 1.0f); /* Output will be 0.5*/

It will first calculate modulus and then assign it to the currentHue which is 0.5 in the above case

currentHue %= 1.0f;

you can write above line as below

currentHue = (currentHue  % 1.0f);
like image 44
Khurram Ali Avatar answered Aug 18 '26 03:08

Khurram Ali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!