-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmath.nut
39 lines (37 loc) · 923 Bytes
/
math.nut
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* This file is part of WormAI: An OpenTTD AI.
*
* @file math.nut Class with math related functions for WormAI.
*
* License: GNU GPL - version 2 (see license.txt)
* Author: Wormnest (Jacob Boerema)
* Copyright: Jacob Boerema, 2016.
*
*/
/**
* Define the WormMath class which holds the static math functions.
*/
class WormMath
{
/**
* Computes square root of i using Babylonian method.
* @param i The integer number to compute the square root of.
* @return The highest integer that is lower or equal to the square root of integer i.
* @note Taken from Rondje om de kerk
*/
static function Sqrt(i);
}
function WormMath::Sqrt(i)
{
assert(i>=0);
if (i == 0) {
return 0; // Avoid divide by zero
}
local n = (i / 2) + 1; // Initial estimate, never low
local n1 = (n + (i / n)) / 2;
while (n1 < n) {
n = n1;
n1 = (n + (i / n)) / 2;
}
return n;
}