---
title: "fract / frac: WGSL and GLSL examples, uses and input limits"
description: "The fractional-part function subtracts floor from its input, producing a repeating ramp. Use it for tiled coordinates, repeated gradients, and loop phases."
url: https://typeshade.dev/reference/functions/fract/
language: en
---

# `fract`

Function

The fractional-part function subtracts floor from its input, producing a repeating ramp.

**Related names in other languages**

These related operations can differ in arguments, support, and semantics. The foreign names are not accepted TypeShade aliases. Check the target forms and restrictions.

- HLSL: `frac`

[Targets](https://typeshade.dev/reference/functions/fract/#targets)

## Explore the function

`y = fract(x)`

![fract graph. A curve shows the output and a highlighted point follows the selected input.](https://typeshade.dev/stills/reference-function-fract.webp)

`y` = 1.25 `y` = -0.25 `x` = -2 `x` = 2

The blue curve shows the output. The orange point marks the selected input.

Wheel to zoom; drag to pan, or rotate in 3D. Keyboard: arrows, +/-, Home to reset.

The axis labels show the displayed range. Values outside this window are not drawn.

Explore the result:

- Move x across successive integer boundaries, including negative inputs.

**Full shader source**

[Open in the Playground](https://typeshade.dev/playground/)

fract-plot.shade.ts

```ts
"use typeshade";

import { PlotUniforms, plot2D, plot3D } from "./function-plot.shade.ts";

class Uniforms extends PlotUniforms {}
declare const u: uniform<Uniforms>;

// Inputs outside the mathematical domain are omitted, including after pan or zoom.
function sample(x: f32): f32 {

  return fract(x);
}

function trajectory(t: f32): vec3 {
  return vec3(t, sample(t), 0.);
}

@fragment
export function main(uv: location<0, vec2>): vec4 {
  const flat = plot2D(sample, uv, u, true);
  if (u.plotBlend <= 0.) { return flat; }
  const space = plot3D(trajectory, uv, u, true);
  return mix(flat, space, clamp(u.plotBlend, 0., 1.));
}
```

Filled by the page every frame, with no control: `resolution`, `plotCenter`, `plotSpan`, `plotRotation`, `plotBlend`.

## When to use it

Use it for tiled coordinates, repeated gradients, and loop phases.

Example calculation: `fract(-1.2) ≈ 0.8`

The reference result is rounded for readability. Shader output follows the precision rules of its target.

## Inputs and limits

Conditions in this example: x ∈ ℝ

The ramp jumps at each boundary. That discontinuity can matter for filtering and derivatives.

For shader input rules and precision, consult [WGSL](https://www.w3.org/TR/WGSL/) and [GLSL ES 3.00](https://registry.khronos.org/OpenGL/specs/es/3.0/GLSL_ES_Specification_3.00.pdf).

## Related functions

- [`abs`](https://typeshade.dev/reference/functions/abs/)
- [`sign`](https://typeshade.dev/reference/functions/sign/)
- [`floor`](https://typeshade.dev/reference/functions/floor/)
- [`ceil`](https://typeshade.dev/reference/functions/ceil/)

**WGSL**

`fract(a)`

**GLSL ES 3.00**

`fract(a)`

The signature, the sentence and any message are the compiler's own text at commit af54ddad5.

Returns the fractional part of `x` as `x` minus `floor(x)`, in the range \[0, 1), componentwise over vectors.

## Syntax

```ts
declare function fract<V extends vec2 | vec3 | vec4>(a0: V): V
declare function fract<P0 extends number>(a0: P0): ScalarOf<P0>
declare function fract<T extends Numeric | Vec64Any>(a0: T): T
```

A `number` in a parameter or a return position is the scalar slot. The ambient file declares a GPU scalar as a branded number whose brand is optional, so a literal and any branded scalar both fill it.

`type f32 = number & { readonly [f32Tag]?: true }`

A `number` you write as a type is inferred from its uses, and `f32`, `i32` and `u32` fix a width; [the TypeScript constructs page](https://typeshade.dev/guide/language/from-typescript/) carries that row with the compiler's emit.

A signature may name a type the ambient file declares for its own use. These are those declarations.

**`Vec64Any`**

`type Vec64Any = vec2f64 | vec3f64 | vec4f64`

**`ScalarOf`**

`type ScalarOf<A, B = unknown, C = unknown> = unknown extends A ? number : [A] extends [boolean] ? bool : typeof f32Tag extends keyof A ? f32 : typeof i32Tag extends keyof A ? i32 : typeof u32Tag extends keyof A ? u32 : typeof f64Tag extends keyof A ? f64 : ScalarOf<B, C>`

**`Numeric`**

`type Numeric = number | vec2 | vec3 | vec4 | vec2i | vec3i | vec4i | vec2u | vec3u | vec4u`

## Parameters

A parameter named by position (`a0`, `a1`) carries a name the ambient file generates. A call passes its arguments in order and names none of them.

- `a0` `V``P0``T`

## Return value

`V``ScalarOf<P0>``T`

## Description

Also accepts `f64` operands.

Evaluated in `f64` by the CPU oracle.

## Targets

**WGSL**

`fract(a)`

**GLSL ES 3.00**

`fract(a)`

## Examples

- [`path/tile-pattern/tile-pattern.shade.ts`](https://typeshade.dev/guide/examples/tile-pattern/)
- [`path/palette/palette.shade.ts`](https://typeshade.dev/guide/examples/palette/)
- [`path/ray-tracer/ray-tracer.shade.ts`](https://typeshade.dev/guide/examples/ray-tracer/)
- [`gallery/capsule-corp-namek-class/capsule-corp-namek-class.shade.ts`](https://typeshade.dev/guide/examples/capsule-corp-namek-class/)
- [`gallery/domain-warp-twin/domain-warp-twin.shade.ts`](https://typeshade.dev/guide/examples/domain-warp-twin/)
- [`gallery/kaleidoscope-twin/kaleidoscope-twin.shade.ts`](https://typeshade.dev/guide/examples/kaleidoscope-twin/)
- [`gallery/ocean-twin/ocean-twin.shade.ts`](https://typeshade.dev/guide/examples/ocean-twin/)
- [`gallery/starfield-twin/starfield-twin.shade.ts`](https://typeshade.dev/guide/examples/starfield-twin/)
- [`gallery/voronoi-twin/voronoi-twin.shade.ts`](https://typeshade.dev/guide/examples/voronoi-twin/)
- [`gallery/fp64-cancellation-twin/fp64-cancellation-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-cancellation-twin/)
- [`gallery/fp64-checker-plane-twin/fp64-checker-plane-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-checker-plane-twin/)
- [`gallery/fp64-clock-twin/fp64-clock-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-clock-twin/)
- [`gallery/fp64-deep-zoom-twin/fp64-deep-zoom-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-deep-zoom-twin/)
- [`gallery/fp64-lane-stripes/fp64-lane-stripes.shade.ts`](https://typeshade.dev/guide/examples/fp64-lane-stripes/)
- [`gallery/fp64-loran-twin/fp64-loran-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-loran-twin/)
- [`gallery/fp64-rtc-twin/fp64-rtc-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-rtc-twin/)
- [`gallery/fp64-sine-sweep-twin/fp64-sine-sweep-twin.shade.ts`](https://typeshade.dev/guide/examples/fp64-sine-sweep-twin/)
- [`gallery/bare-position/bare-position.shade.ts`](https://typeshade.dev/guide/examples/bare-position/)
- [`gallery/cube-array-gather/cube-array-gather.shade.ts`](https://typeshade.dev/guide/examples/cube-array-gather/)
- [`gallery/cube-env/cube-env.shade.ts`](https://typeshade.dev/guide/examples/cube-env/)
- [`gallery/default-args/default-args.shade.ts`](https://typeshade.dev/guide/examples/default-args/)
- [`gallery/generic-class/generic-class.shade.ts`](https://typeshade.dev/guide/examples/generic-class/)
- [`gallery/generic-helpers/generic-helpers.shade.ts`](https://typeshade.dev/guide/examples/generic-helpers/)
- [`gallery/id-pick/id-pick.shade.ts`](https://typeshade.dev/guide/examples/id-pick/)
- [`gallery/loops-over-data/loops-over-data.shade.ts`](https://typeshade.dev/guide/examples/loops-over-data/)
- [`gallery/orbit-inout/orbit-inout.shade.ts`](https://typeshade.dev/guide/examples/orbit-inout/)
- [`gallery/pick-composite/pick-composite.shade.ts`](https://typeshade.dev/guide/examples/pick-composite/)
- [`gallery/reference-parameters/reference-parameters.shade.ts`](https://typeshade.dev/guide/examples/reference-parameters/)
- [`gallery/shadow-compare/shadow-compare.shade.ts`](https://typeshade.dev/guide/examples/shadow-compare/)
- [`gallery/tuple-and-brand/tuple-and-brand.shade.ts`](https://typeshade.dev/guide/examples/tuple-and-brand/)

## See also

- [Functions](https://typeshade.dev/reference/functions/)
- [Builtin table](https://typeshade.dev/guide/language/builtins/#maths)
- [`fract()` in `typeshade/core/ir`](https://typeshade.dev/api/fract/)
