title: Glsl mod vs Hlsl fmod
body: <p>I've implemented the spiral GLSL shader described in <a href="https://stackoverflow.com/q/4638317/2877364">this question</a> in HLSL, but the results are not the same.  I think it's because of the <code>mod</code> function in GLSL that I've translated to <code>fmod</code> in HLSL.  I suspect that this problem only happens when we have negative numbers in the input of the <code>fmod</code> function.</p>
  
  <p>I've tried replacing the call to <code>mod</code> by a call to a function that I've made which does what is described in the <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/mod.xml" rel="nofollow noreferrer">GLSL documentation</a> and it works:</p>
  
  <blockquote>
    <p><code>mod</code> returns the value of <code>x</code> modulo <code>y</code>. This is computed as <code>x - y * floor(x/y)</code>.</p>
  </blockquote>
  
  <p>The working code I use instead of <code>fmod</code> is:</p>
  
  <pre><code>float mod(float x, float y)
  {
    return x - y * floor(x/y)
  }
  </code></pre>
  
  <p>By contrast to GLSL <code>mod</code>, <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/bb509601(v=vs.85).aspx" rel="nofollow noreferrer">MSDN says</a> the HLSL <code>fmod</code> function does this:</p>
  
  <blockquote>
    <p>The floating-point remainder is calculated such that <code>x = i * y + f</code>, where <code>i</code> is an integer, <code>f</code> has the same sign as <code>x</code>, and the absolute value of <code>f</code> is less than the absolute value of <code>y</code>.</p>
  </blockquote>
  
  <p>I've used <a href="http://sourceforge.net/projects/hlsl2glsl" rel="nofollow noreferrer">an HLSL to GLSL converter</a>, and the <code>fmod</code> function is translated as <code>mod</code>.  However, I don't know if I can assume that <code>mod</code> translates to <code>fmod</code>.</p>
  
  <h3>Questions</h3>
  
  <ol>
  <li>What are the differences between GLSL <code>mod</code> and HLSL<code>fmod</code>?  </li>
  <li>How can I translate MSDN's cryptic description of <code>fmod</code> to a pseudo-code implementation?</li>
  </ol>
  
  <h3>GLSL Shader</h3>
  
  <pre><code>uniform float time;
  uniform vec2 resolution;
  uniform vec2 aspect;
  
  void main( void ) {
    vec2 position = -aspect.xy + 2.0 * gl_FragCoord.xy / resolution.xy * aspect.xy;
    float angle = 0.0 ;
    float radius = length(position) ;
    if (position.x != 0.0 &amp;&amp; position.y != 0.0){
      angle = degrees(atan(position.y,position.x)) ;
    }
    float amod = mod(angle+30.0*time-120.0*log(radius), 30.0) ;
    if (amod&lt;15.0){
      gl_FragColor = vec4( 0.0, 0.0, 0.0, 1.0 );
    } else{
      gl_FragColor = vec4( 1.0, 1.0, 1.0, 1.0 );                    
    }
  }
  </code></pre>
  
  <h3>HLSL Shader</h3>
  
  <pre><code>struct Psl_VertexShaderInput
  {
      float3 pos : POSITION;
  };
  
  struct Psl_VertexShaderOutput
  {
      float4 pos : POSITION;
  
  };
  
  struct Psl_PixelShaderOutput
  {
      float4 Output0 : COLOR0;
  };
  
  float3 psl_positionOffset;
  float2 psl_dimension;
  
  Psl_VertexShaderOutput Psl_VertexShaderFunction(Psl_VertexShaderInput psl_input)
  {
      Psl_VertexShaderOutput psl_output = (Psl_VertexShaderOutput)0;
  
      psl_output.pos = float4(psl_input.pos + psl_positionOffset, 1);
  
  
      return psl_output;
  }
  
  float time : TIME;
  float2 resolution : DIMENSION;
  
  
  Psl_PixelShaderOutput Psl_PixelShaderFunction(float2 pos : VPOS)
  {
      Psl_PixelShaderOutput psl_output = (Psl_PixelShaderOutput)0;
  
      float2 aspect = float2(resolution.x / resolution.y, 1.0);
      float2 position = -aspect.xy + 2.0 * pos.xy / resolution.xy * aspect.xy;
      float angle = 0.0;
      float radius = length(position);
      if (position.x != 0.0 &amp;&amp; position.y != 0.0)
      {
          angle = degrees(atan2(position.y, position.x));
      }
      float amod = fmod((angle + 30.0 * time - 120.0 * log(radius)), 30.0);
      if (amod &lt; 15.0)
      {
          psl_output.Output0 = float4(0.0, 0.0, 0.0, 1.0);
          return psl_output;
      }
  
      else
      {
          psl_output.Output0 = float4(1.0, 1.0, 1.0, 1.0);
          return psl_output;
      }
  
  }
  
  technique Default
  {
      pass P0
      {
          VertexShader = compile vs_3_0 Psl_VertexShaderFunction();
          PixelShader = compile ps_3_0 Psl_PixelShaderFunction();
      }
  }
  </code></pre>