hawk.math.index

Contents

hawk.math.index#

hawk.math.index(x, n)[source]#

x as an INDEX into an extent of n: truncated toward zero, then clamped to [0, n) (NaN gives 0). The runtime component form v[hawk.math.index(x, n)] takes it; an extent is a type, so an index outside it is not an error the read or the write can report, and this is where it is made safe. The clamp can HIDE an index bug (an input far outside the grid reads the end element); an author who wants the error computes the index without it.

Parameters:
  • x (Any) – a traced rank-0 number.

  • n (int) – the extent, a Python integer >= 1.

Return type:

Value

Returns:

the rank-0 i32 index.

Raises:

HawkError – for a bool, a rank>=1 value or n < 1.

Example: c = theta[hawk.math.index(u * 5.0, 13)]