Intrinsics2 min read

fract

Returns the fractional part of a value.

Reading Time
2 min
Word Count
198
Sections
11
Try It Live

Test fract in a live shader

Open the playground, start from a visual preset, and wire fract into the fragment stage to see how it behaves with real values.

Open Playground
bwsl
fract :: (T x) -> T {...}

The fract function returns the fractional part of the input value, computed as x - floor(x). The result is always in the range [0, 1).

Where T can be float, float2, float3, or float4.

Example - Tiling Pattern

Parameters

ParameterTypeDescription
xTThe input value

Return Value

Returns the fractional part of x, always in range [0, 1).

Common Use Cases

UV Tiling

Use fract to tile textures or patterns multiple times across a surface.

Checkerboard Pattern

Combine fract with step to create classic checker patterns.

Scrolling Animation

Use fract with time to create seamless scrolling effects.

Brick Pattern

Offset alternating rows using fract to create brick layouts.

Radial Repetition

Use fract with polar coordinates for radial patterns.

Negative Values

For negative inputs, fract still returns values in [0, 1). For example, fract(-0.3) returns 0.7, not -0.3. This is because fract(x) = x - floor(x) and floor(-0.3) = -1.

Avoiding Seams

When using fract for tiling, multiply by whole numbers to ensure clean tile boundaries. Non-integer multiples can cause visible seams at texture edges.

Compiled Output

When compiled to GLSL:

glsl
fract(x)

When compiled to HLSL:

Uses the target backend's native fractional-part operation.

When compiled to Metal:

metal
fract(x)

See Also

  • floor - Round down to integer
  • ceil - Round up to integer
  • mod - Modulo operation