4

The following statement is valid HLSL:

float3(300.0f,200.0f,100.0f) % 256.0f

Apparently te % operator works on floating point numbers? How is that different from fmod? Is the following statement equivalent?

fmod(float3(300.0f,200.0f,100.0f), 256.0f)

Thanks

Mathew Block
  • 1,613
  • 1
  • 10
  • 9
Johan
  • 660
  • 1
  • 6
  • 13

2 Answers2

2

I believe so, yes. The % operator is documented here and is defined to work on floating point numbers as well as integers.

Mark Pattison
  • 2,964
  • 1
  • 22
  • 42
  • 1
    However it says `The % operator is defined only in cases where either both sides are positive or both sides are negative.` No such reservation for the `fmod` function as far as I can see. – Claude Sep 01 '14 at 17:14
-1

I'm not sure they are exactly the same, but don't quote me on it. Here was my test:

const float2 r = float2(
23.1406926327792690,  // e^pi (Gelfond's constant)
2.6651441426902251); // 2^sqrt(2) (Gelfond–Schneider constant)

return frac( cos( fmod( 12345678., 1e-7 + 256. * dot(p,r) ) ) );  

//return frac( cos( 12345678.% (1e-7 + 256. * dot(p,r)) ) );  

The 2nd version returns visual patterns when mapped as a texture whereas the 1st appears purely random. Perhaps I'm misunderstanding something but they certainly seem to act differently in this instance.

Maybe I just foofed the test but thought I would mention it as it seemed relevant.

EDIT: This code originated from here Can I generate a random number inside a pixel shader? seemingly.

Community
  • 1
  • 1
twobob
  • 354
  • 8
  • 22
  • One swallow does not a summer make, and one test does not prove the point. You need at least four, with every possible combination of positive and negative arguments. – user207421 Aug 03 '14 at 00:29
  • fair enough. I was trying to raise an interesting point, not make one. – twobob Aug 03 '14 at 03:54
  • @Michaelangel007 I didn't get it from there IIRC, it was from some random XNA place, which probably got it from there. Feel free to incorrectly accuse me of plagiarism at any time though. Oh you did. – twobob Jan 11 '17 at 08:15
  • I have added your link. your kudos is now secure. Please don't accuse me of plagiarism again. As I indicated that was not that case. After 7 years it is pretty clear I am not here for the cred. – twobob Jan 29 '17 at 03:49