Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/bench/tests/sunspider/math-partial-sums.lua
2727 views
1
--[[
2
The Great Computer Language Shootout
3
http://shootout.alioth.debian.org/
4
contributed by Isaac Gouy
5
]]
6
local function prequire(name) local success, result = pcall(require, name); return success and result end
7
local bench = script and require(script.Parent.bench_support) or prequire("bench_support") or require("../../bench_support")
8
9
function test()
10
11
local function partial(n)
12
local a1, a2, a3, a4, a5, a6, a7, a8, a9 = 0, 0, 0, 0, 0, 0, 0, 0, 0;
13
local twothirds = 2.0/3.0;
14
local alt = -1.0;
15
local k2, k3, sk, ck = 0, 0, 0, 0;
16
17
for k = 1,n do
18
k2 = k*k;
19
k3 = k2*k;
20
sk = math.sin(k);
21
ck = math.cos(k);
22
alt = -alt;
23
24
a1 = a1 + math.pow(twothirds,k-1);
25
a2 = a2 + math.pow(k,-0.5);
26
a3 = a3 + 1.0/(k*(k+1.0));
27
a4 = a4 + 1.0/(k3 * sk*sk);
28
a5 = a5 + 1.0/(k3 * ck*ck);
29
a6 = a6 + 1.0/k;
30
a7 = a7 + 1.0/k2;
31
a8 = a8 + alt/k;
32
a9 = a9 + alt/(2*k -1);
33
end
34
35
return a6 + a7 + a8 + a9;
36
end
37
38
local total = 0;
39
local i = 1024
40
41
while i <= 16384 do
42
total = total + partial(i);
43
i = i * 2
44
end
45
46
local expected = 60.08994194659945;
47
48
if (total ~= expected) then
49
assert(false, "ERROR: bad result: expected " .. expected .. " but got " .. total);
50
end
51
52
end
53
54
bench.runCode(test, "math-partial-sums")
55
56