Wiki

Clone wiki

Core / rsqrt

Back to Beyond the Codea in-app reference


FunctionIconFunction-Small.png rsqrt() function

Introduction

Codea's rsqrt(x) function uses the fast inverse square root algorithm to calculate an approximation of x ^ (-0.5).

Cached, it is a few percentage points faster than x ^ (-0.5). For example:

function setup()
    local rsqrtCached = rsqrt
    local t1 = os.clock()
    local j = 0
    for i = 1, 10000000 do
        j = j +  rsqrtCached(i)
    end
    print(os.clock() - t1, j) -- Output: about 4.25s.
    local t2 = os.clock()
    local j = 0
    for i = 1, 10000000 do
        j = j + i^(-0.5)
    end
    print(os.clock() - t2, j) -- Output: about 5.00s.
end

function draw() background(0) end

Updated