<p>In HLSL, you can use the built-in <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/bb509601(v=vs.85).aspx" rel="nofollow noreferrer"><code>fmod</code></a> function for this.</p>

<p>I'm not sure what's going wrong with your implementation, though. It looks mathematically correct. It's conceivable you're encountering a precision issue with the subtraction, depending on the inputs.</p>

<p>BTW, an alternative implementation which avoids the subtraction is:</p>

<pre><code>float Mod(float x, float y)
{
    return frac(x / y) * y;
}
</code></pre>

CC BY-SA 3.0
2018-04-19T19:45:37.593Z
But `frac(x / y) * y` is exactly what normal modulus operator compiles to (on GCN at least) and it gives incorrect results on both Nvidia and AMD. I'm not sure about `fmod`, though. It seems to give slightly different assembly code. I will check it, thanks.
Derag
CC BY-SA 3.0
2018-04-20T07:57:58.610Z
So I have chacked it and `frac(x / y) * y`, operator `%` and `fmod` give incorrect results on Nvidia and AMD. `x - floor(x / y) * y` works correct on Nvidia but AMD not so much.
Derag
CC BY-SA 3.0
2018-04-19T18:30:58.200Z
2018-04-19T18:30:58.200Z
Nathan Reed
<p>I have a problem with precision on AMD in shaders (<code>hlsl</code>). In vertex shader I calculate <code>UVs</code> for particle using modulus operator on float. With simple <code>%</code> there is a problem on Nvidia and on AMD (e.g. <code>7 % 7 = 7</code>) so I have written modulus as:</p>

<pre><code>float Mod( float x, float y )    
{       
     return x - floor(x / y) * y;    
}
</code></pre>

<p>This have fixed the problem on Nvidia but AMD still returns wrong values.</p>

<p>Any idea what is going on? And how to fixed it?</p>

<p>I know I can use:</p>

<pre><code>float( int( x ) % int( y ) )
</code></pre>

<p>but I think it is slower and I wonder if there is any better way.</p>

CC BY-SA 4.0
2018-05-30T19:58:01.160Z
Are you using DX or are you using vulkan?  Both support HLSL...
Krupip
CC BY-SA 4.0
2018-06-13T21:16:40.067Z
I'm using DirectX11
Derag
<hlsl><hardware><float>
Precision problem on AMD
Derag
# Precision problem on AMD

(Asked by: username_0 on 2018-04-19)

I have a problem with precision on AMD in shaders (`hlsl`). In vertex shader I calculate `UVs` for particle using modulus operator on float. With simple `%` there is a problem on Nvidia and on AMD (e.g. `7 % 7 = 7`) so I have written modulus as:

```
float Mod( float x, float y )    
{       
     return x - floor(x / y) * y;    
}

```

This have fixed the problem on Nvidia but AMD still returns wrong values.

Any idea what is going on? And how to fixed it?

I know I can use:

```
float( int( x ) % int( y ) )

```

but I think it is slower and I wonder if there is any better way.

**Tags:** hlsl, hardware, float

> **Comments:**
> **username_1**: Are you using DX or are you using vulkan?  Both support HLSL...  
> **username_0**: I'm using DirectX11  

---

## 1 answers

### Answer by username_2 on 2018-04-19. Score: 1

In HLSL, you can use the built-in [`fmod`](https://msdn.microsoft.com/en-us/library/windows/desktop/bb509601(v=vs.85).aspx) function for this.

I'm not sure what's going wrong with your implementation, though. It looks mathematically correct. It's conceivable you're encountering a precision issue with the subtraction, depending on the inputs.

BTW, an alternative implementation which avoids the subtraction is:

```
float Mod(float x, float y)
{
    return frac(x / y) * y;
}

```

> **Comments:**
> **username_0**: But `frac(x / y) * y` is exactly what normal modulus operator compiles to (on GCN at least) and it gives incorrect results on both Nvidia and AMD. I'm not sure about `fmod`, though. It seems to give slightly different assembly code. I will check it, thanks.  
> **username_0**: So I have chacked it and `frac(x / y) * y`, operator `%` and `fmod` give incorrect results on Nvidia and AMD. `x - floor(x / y) * y` works correct on Nvidia but AMD not so much.  

---

