# SMath

## Index

### Static Functions Index

| Function Name                                                                                                                       |
| ----------------------------------------------------------------------------------------------------------------------------------- |
| static float [**Random** ](#random)()                                                                                               |
| static float [**RandomRange** ](#randomrange)(float min, float max)                                                                 |
| static int [**RandomInteger** ](#randominteger)(int min, int max)                                                                   |
| <p>static float <a href="#abs"><strong>Abs</strong> </a>(float v)<br>static int <a href="#abs"><strong>Abs</strong> </a>(int v)</p> |
| static float [**Acos** ](#acos)(float v)                                                                                            |
| static bool [**Approximately** ](#approximately)(float a, float b)                                                                  |
| static float [**Asin** ](#asin)(float v)                                                                                            |
| static float [**Atan** ](#atan)(float v)                                                                                            |
| static float [**Atan2** ](#atan2)(float y, float x)                                                                                 |
| static int [**Ceil** ](#ceil)(float v)                                                                                              |
| static float [**Clamp** ](#clamp)(float v, float min, float max)                                                                    |
| static float [**Clamp01** ](#clamp01)(float v)                                                                                      |
| static int [**ClosestPowerOfTwo** ](#closestpoweroftwo)(int v)                                                                      |
| static float [**Cos** ](#cos)(float v)                                                                                              |
| static float [**DeltaAngle** ](#deltaangle)(float current, float target)                                                            |
| static float [**Exp** ](#exp)(float v)                                                                                              |
| static int [**Floor** ](#floor)(float v)                                                                                            |
| static float [**GammaToLinearSpace** ](#gammatolinearspace)(float v)                                                                |
| static float [**InverseLerp** ](#inverselerp)(float a, float b, float value)                                                        |
| static bool [**IsPowerOfTwo** ](#ispoweroftwo)(int v)                                                                               |
| static float [**Lerp** ](#lerp)(float a, float b, float v)                                                                          |
| static float [**LerpAngle** ](#lerpangle)(float a, float b, float v)                                                                |
| static float [**LerpUnclamped** ](#lerpunclamped)(float a, float b, float v)                                                        |
| static float [**LinearToGammaSpace** ](#lineartogammaspace)(float v)                                                                |
| <p>static float <a href="#log"><strong>Log</strong> </a>(float v)<br>static float <a href="#log">Log </a>(float v, float p)</p>     |
| static float [**Log10** ](#log10)(float v)                                                                                          |
| static float [**Max** ](#max)(float a, float b)                                                                                     |
| static float [**Min** ](#min)(float a, float b)                                                                                     |
| static float [**MoveTowards** ](#movetowards)(float value, float target, float delta)                                               |
| static float [**MoveTowardsAngle** ](#movetowardsangle)(float value, float target, float delta)                                     |
| static int [**NextPowerOfTwo** ](#nextpoweroftwo)(int v)                                                                            |
| static float [**PerlinNoise** ](#perlinnoise)(float x, float y)                                                                     |
| static float [**PingPong** ](#pingpong)(float t, float length)                                                                      |
| static float [**Pow** ](#pow)(float value, float pow)                                                                               |
| static float [**Repeat** ](#repeat)(float value, float length)                                                                      |
| static int [**Round** ](#round)(float value)                                                                                        |
| static float [**Sign** ](#sign)(float value)                                                                                        |
| static float [**Sin** ](#sin)(float value)                                                                                          |
| static float [**SmoothStep** ](#smoothstep)(float from, float to, float t)                                                          |
| static float [**Sqrt** ](#sqrt)(float value)                                                                                        |
| static float [**Tan** ](#tan)(float value)                                                                                          |

### Static Attributes Index

| Property Name                               |
| ------------------------------------------- |
| static readonly float [**Pi**](#pi)\*\*\*\* |

## Static Functions

### Random

static float **Random** ()

*Returns a random float between 0 and 1 (inclusive)*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local randomNumber = Space.Math.Random();
Space.Log(randomNumber);
-- prints 0.689094245433807
```

{% endtab %}
{% endtabs %}

### RandomRange

static float **RandomRange** (float min, float max)

*Returns a random float between min and max (inclusive)*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local min = 100.0;
local max = 500.0;
local randomNumber = Space.Math.RandomRange(min, max );
Space.Log(randomNumber);
-- prints 0206.659149169922
```

{% endtab %}
{% endtabs %}

### RandomInteger

static int **RandomInteger** (int min, int max)

*Returns a random float between min (inclusive) and max (exclusive)*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local min = 50;
local max = 75;
local randomInteger = Space.Math.RandomInteger(min, max);
Space.Log(randomInteger);
-- prints 52
```

{% endtab %}
{% endtabs %}

### Abs

static float **Abs** (float v)\
static int **Abs** (int v)

*Returns the absolute value of 'val'*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local value = -4.2
local absoluteNum = Space.Math.Abs(value);
Space.Log(absoluteNum);
-- prints 4 [BUG] should be 4.2
```

{% endtab %}
{% endtabs %}

### Acos

static float **Acos** (float v)

*Returns the arc cosine value of 'val'*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local value = 0.5;
local arcCosine = Space.Math.Acos(value);
Space.Log(arcCosine);
-- prints 1.04719758033752 (radians) which is 60 degrees
```

{% endtab %}
{% endtabs %}

### Approximately

static bool **Approximately** (float a, float b)

*True if the difference between a and b is less than epsilon*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local a = 5.0;
local b = 5;
local approx1 = Space.Math.Approximately(a, b);
Space.Log(approx1);
-- prints true

local a = 5.01;
local b = 5.0;
local approx2 = Space.Math.Approximately(a, b);
Space.Log(approx2);
-- prints false

local a = 5.01;
local b = 5.0;
local approx3 = Space.Math.Approximately(a, b);
Space.Log(approx3);
-- prints false
```

{% endtab %}
{% endtabs %}

### Asin

static float **Asin** (float v)

*Returns the arc sine value of 'val'*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local value = 0.5;
local arcSine = Space.Math.Asin(value);
Space.Log(arcSine);
-- prints 0.523598790168762 (radians) which is 30 degrees
```

{% endtab %}
{% endtabs %}

### Atan

static float **Atan** (float v)

*Returns the arc tangent value of 'val'*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local value = 1.732050808;
local arcTangent = Space.Math.Atan(value);
Space.Log(arcTangent);
-- prints 1.04719758033752 (radians) which is 60 degrees
```

{% endtab %}
{% endtabs %}

### Atan2

static float **Atan2** (float y, float x)

*Returns the arc tangent of y/x*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local x = 0.5;
local y = 0.5;
local arcTangent = Space.Math.Atan2(y, x);
Space.Log(arcTangent);
-- prints 0.785398185253143
```

{% endtab %}
{% endtabs %}

### Ceil

static int **Ceil** (float v)

*Returns the ceil value of 'val' as an integer*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
Space.Log(Space.Math.Ceil(4.0));
-- prints 4

Space.Log(Space.Math.Ceil(4.2));
-- prints 5

Space.Log(Space.Math.Ceil(-4.2));
-- prints -4
```

{% endtab %}
{% endtabs %}

### Clamp

static float **Clamp** (float v, float min, float max)

*Clamps val between min and max, and returns the result*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local value = 100.0;
local min = 20.0;
local max = 82.0;

local clampedValue = Space.Math.Clamp(value, min, max);
Space.Log(clampedValue);
-- prints 82
```

{% endtab %}
{% endtabs %}

### Clamp01

static float **Clamp01** (float v)

*Clamps val between 0 and 1, and returns the result*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local value = -1.0;

local clampedValue = Space.Math.Clamp01(value);
Space.Log(clampedValue);
-- prints 0
```

{% endtab %}
{% endtabs %}

### ClosestPowerOfTwo

static int **ClosestPowerOfTwo** (int v)

*Returns the closest power of two to val*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local value = 33;

local powerOfTwo = Space.Math.ClosestPowerOfTwo(value);
Space.Log(powerOfTwo);
-- prints 32
```

{% endtab %}
{% endtabs %}

### Cos

static float **Cos** (float v)

*Returns the cosine of val*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local angle = 1.04719758033752; -- converts to 60 degrees
local cosine = Space.Math.Cos(angle);
Space.Log(cosine);
-- prints 0.499999970197678

local angle2 = Space.Math.Pi/3; -- converts to 60 degrees
local cosine2 = Space.Math.Cos(angle2);
Space.Log(cosine2);
-- prints 0.499999970197678
```

{% endtab %}
{% endtabs %}

### DeltaAngle

static float **DeltaAngle** (float current, float target)

*Returns the difference in degrees between two values (e.g. 350' and 17' returns 27')*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
Space.Log(Space.Math.DeltaAngle(350.0, 17.0));
-- prints 27
```

{% endtab %}
{% endtabs %}

### Exp

static float **Exp** (float v)

*Returns e raised to val power.*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
Space.Log(Space.Math.Exp(3.0));
-- prints 20.0855369567871
```

{% endtab %}
{% endtabs %}

### Floor

static int **Floor** (float v)

*Returns floor of val, converted to an int*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
Space.Log(Space.Math.Floor(4.0));
-- prints 4

Space.Log(Space.Math.Floor(4.2));
-- prints 4

Space.Log(Space.Math.Floor(-4.2));
-- prints -5
```

{% endtab %}
{% endtabs %}

### GammaToLinearSpace

static float **GammaToLinearSpace** (float v)

*Converts a colour value from Gamma to Linear Space (Pow 2.2)*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
Space.Log(Space.Math.GammaToLinearSpace(0.5));
-- prints 0.214041143655777
```

{% endtab %}
{% endtabs %}

### InverseLerp

static float **InverseLerp** (float a, float b, float value)

*Returns the percentage between a and b that 'val' is on a line (opposite of Lerp)*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local sliderStart = 0.0;
local sliderStop = 100.0;
local currentSliderPos = 75.0;

local percentage = Space.Math.InverseLerp(sliderStart, sliderStop, currentSliderPos);
Space.Log(percentage);
-- prints 0.75
```

{% endtab %}
{% endtabs %}

### IsPowerOfTwo

static bool **IsPowerOfTwo** (int v)

*Returns true if val is a power of two*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
Space.Log(Space.Math.IsPowerOfTwo(55));
-- prints False

Space.Log(Space.Math.IsPowerOfTwo(32));
-- prints True
```

{% endtab %}
{% endtabs %}

### Lerp

static float **Lerp** (float a, float b, float v)

*Interpolates between 'a' and 'b' based on 'val', assuming 'val' is between 0 and 1*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local sliderStart = 0.0;
local sliderStop = 100.0;
local percentage = 0.75;

local currentSliderPos = Space.Math.Lerp(sliderStart, sliderStop, percentage);
Space.Log(currentSliderPos);
-- prints 75
```

{% endtab %}
{% endtabs %}

### LerpAngle

static float **LerpAngle** (float a, float b, float v)

*Interpolates between angles 'a' and 'b' based on 'val', assuming 'val' is between 0 and 1*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local sliderStartAngle = 0.0;
local sliderStopAngle = Space.Math.Pi;-- 180 degrees
local percentage = 0.50;

local currentSliderPos = Space.Math.LerpAngle(sliderStartAngle, sliderStopAngle, percentage);
Space.Log(currentSliderPos);
-- prints 1.57079637050629 (90 degrees)
```

{% endtab %}
{% endtabs %}

### LerpUnclamped

static float **LerpUnclamped** (float a, float b, float v)

*Interpolates between 'a' and 'b' based on 'val', assuming 'val' is between 0 and 1, but unbounded (allowing higher/lower values)*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local sliderStart = 0.0;
local sliderStop = 100.0;
local percentage = 2.0;

local currentSliderPos = Space.Math.LerpUnclamped(sliderStart, sliderStop, percentage);
Space.Log(currentSliderPos);
-- prints 200
```

{% endtab %}
{% endtabs %}

### LinearToGammaSpace

static float **LinearToGammaSpace** (float v)

*Converts a colour value from Linear to Gamma Space (Pow 1/2.2)*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
Space.Log(Space.Math.LinearToGammaSpace(0.214041143655777));
-- prints 0.5
```

{% endtab %}
{% endtabs %}

### Log

static float **Log** (float v)\
static float **Log** (float v, float p)

*Returns the natural logarithm for 'val'*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
Space.Log(Space.Math.Log(30.0));
-- prints 3.40119743347168

Space.Log(Space.Math.Log(4.0, 2.0));
-- prints 2
```

{% endtab %}
{% endtabs %}

### Log10

static float **Log10** (float v)

*Returns the Log10 value for 'val'*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
Space.Log(Space.Math.Log10(100.0));
-- prints 2
```

{% endtab %}
{% endtabs %}

### Max

static float **Max** (float a, float b)

*Returns higher of 'a' or 'b'*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
Space.Log(Space.Math.Max(20.0, 100.0));
-- prints 100
```

{% endtab %}
{% endtabs %}

### Min

static float **Min** (float a, float b)

*Returns lower of 'a' or 'b'*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
Space.Log(Space.Math.Min(20.0, 100.0));
-- prints 20
```

{% endtab %}
{% endtabs %}

### MoveTowards

static float **MoveTowards** (float value, float target, float delta)

*Move value to target, but by no more than delta*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local sliderStart = 5.0;
local sliderStop = 10.0;
local deltaChange = 1.0;

local currentSliderPos = Space.Math.MoveTowards(sliderStart, sliderStop, deltaChange);
Space.Log(currentSliderPos);
-- prints 6 (moves forward 1)


local sliderStart = 5.0;
local sliderStop = 10.0;
local deltaChange = 7.0;

local currentSliderPos = Space.Math.MoveTowards(sliderStart, sliderStop, deltaChange);
Space.Log(currentSliderPos); -- prints 10 (caps out at at the target value)

-- NOTE: Use negative delta to move away from target.
```

{% endtab %}
{% endtabs %}

### MoveTowardsAngle

static float **MoveTowardsAngle** (float value, float target, float delta)

*Move angle value to target, but by no more than delta*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local sliderStartAngle = Space.Math.Pi/2; -- 90 degrees
local sliderStopAngle = Space.Math.Pi; -- 180 degrees
local deltaChange = Space.Math.Pi/6; -- 30 degrees

local currentSliderPos = Space.Math.MoveTowardsAngle(sliderStartAngle, sliderStopAngle, deltaChange);
Space.Log(currentSliderPos);
-- prints 2.09439516067505 (moves forward 30 degrees to 120 degrees)

-- NOTE: Add 180 degrees (Space.Math.Pi) to move away from target.
```

{% endtab %}
{% endtabs %}

### NextPowerOfTwo

static int **NextPowerOfTwo** (int v)

*Return the next power of two larger or equal to val*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
Space.Log(Space.Math.NextPowerOfTwo(16));
-- prints 16

Space.Log(Space.Math.NextPowerOfTwo(17));
-- prints 32
```

{% endtab %}
{% endtabs %}

### PerlinNoise

static float **PerlinNoise** (float x, float y)

*Return 2D Perlin noise for coordinates x and y*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
-- Lua Translation of Unity C# Documentation

local ball = Space.Host.ExecutingObject;
ball.SubscribeToEvents();

-- animates this object to move upwards on the y axis with slight random movement
local animateBall = function()
  local heightScale = 0.1; -- controls speed of movement
  local xScale = 1.0; -- shifts x position on perlin noise plane

  local height = heightScale * Space.Math.PerlinNoise(Space.Time * xScale, 0.0);
  local pos = ball.LocalPosition;
  pos.y = pos.y + height;
  ball.LocalPosition = pos;
  Space.Log(pos.y);
end

ball.OnUpdate(animateBall);

-- NOTE: If you need a guaranteed range of [0,1], make sure to clamp the value with Space.Math.Clamp01.
```

{% endtab %}
{% endtabs %}

### PingPong

static float **PingPong** (float t, float length)

*Return a value between 0 and length that oscillates upwards and back based on the position of 'val'*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
--[[ In this example, if you traced the output of Space.Math.PingPong(value, length),
the return values would cycle forward through the range [0,5] then cycle backwards through the range [5,0] in order. --]]

local ball = Space.Host.ExecutingObject;
local originalPos = ball.LocalPosition;
ball.SubscribeToEvents();

-- The ball object oscillates back and forth on the x-axis.
local animateBall = function()
  local value = Space.Time;
  local length = 5.0;

  local newPos = Vector.New(Space.Math.PingPong(value, length) + originalPos.x, originalPos.y, originalPos.z);
  ball.LocalPosition = newPos;
end

ball.OnUpdate(animateBall);
```

{% endtab %}
{% endtabs %}

### Pow

static float **Pow** (float value, float pow)

*Return x raised to y power*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
Space.Log(Space.Math.Pow(2, 4));
-- prints 16
```

{% endtab %}
{% endtabs %}

### Repeat

static float **Repeat** (float value, float length)

*Return a value between 0 and length that returns to 0 after exceeding length based on 'val'*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
--[[ In this example, if you traced the output of Space.Math.Repeat(value, length),
the return values would cycle forward through the range [0,5]. The cycle is repeated again from 0.--]]

local ball = Space.Host.ExecutingObject;
local originalPos = ball.LocalPosition;
ball.SubscribeToEvents();

-- The ball object moves forward on the x-axis.
-- Then, it repeats the same motion again from the beginning.
local animateBall = function()
  local value = Space.Time;
  local length = 5.0;

  local newPos = Vector.New(Space.Math.Repeat(value, length) + originalPos.x, originalPos.y, originalPos.z);
  ball.LocalPosition = newPos;
end

ball.OnUpdate(animateBall);
```

{% endtab %}
{% endtabs %}

### Round

static int **Round** (float value)

*Returns the nearest integer value to val*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
Space.Log(Space.Math.Round(4.0));
-- prints 4

Space.Log(Space.Math.Round(4.2));
-- prints 4

Space.Log(Space.Math.Round(4.5));
-- prints 4

Space.Log(Space.Math.Round(4.55));
-- prints 5

Space.Log(Space.Math.Round(4.8));
-- prints 5

Space.Log(Space.Math.Round(-4.2));
-- prints -4

Space.Log(Space.Math.Round(-4.8));

-- prints -5
```

{% endtab %}
{% endtabs %}

### Sign

static float **Sign** (float value)

*Returns either 1 or -1 based on the sign of 'val'*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
Space.Log(Space.Math.Sign(4.2));
-- prints 1

Space.Log(Space.Math.Sign(-4.2));
-- prints -1
```

{% endtab %}
{% endtabs %}

### Sin

static float **Sin** (float value)

*Returns the sine of val*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local angle = 1.04719758033752; -- converts to 60 degrees
local sine = Space.Math.Sin(angle);
Space.Log(sine);
-- prints 0.866025447845459

local angle2 = Space.Math.Pi/3; -- converts to 60 degrees
local sine2 = Space.Math.Sin(angle2);
Space.Log(sine2);
-- prints 0.866025447845459
```

{% endtab %}
{% endtabs %}

### SmoothStep

static float **SmoothStep** (float from, float to, float t)

*Similar to Lerp but moves slowly closer to the edges ('Spherical Lerp')*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
-- Lua Translation of Unity C# Documentation

--[[ In this example, Space.Math.SmoothStep(min, max, time) returns values in the range [1,10] in order.
As values approach 10, the interpolation slows down. --]]

local ball = Space.Host.ExecutingObject;
local originalPos = ball.LocalPosition;
local min = 1.0;
local max = 10.0;
local duration = 5.0; --lower value to speed up; raise to slow down
local startTime;
ball.SubscribeToEvents();

local initStartTime = function()
  startTime = Space.Time;
end

-- The ball jumps to min + originalPos, then it moves towards
-- max + originalPos while slowing down at the end.
local animateBall = function()
  local time = (Space.Time - startTime) / duration;
  local newPos = Vector.New(Space.Math.SmoothStep(min, max, time)
        + originalPos.x, originalPos.y, originalPos.z);
  ball.LocalPosition = newPos;
end

ball.OnStart(initStartTime);
ball.OnUpdate(animateBall);
```

{% endtab %}
{% endtabs %}

### Sqrt

static float **Sqrt** (float value)

*Returns the square root of val*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
Space.Log(Space.Math.Sqrt(16));
-- prints 4

Space.Log(Space.Math.Sqrt(-16));
-- prints NaN
```

{% endtab %}
{% endtabs %}

### Tan

static float **Tan** (float value)

*Returns the tangent value of 'val'*

| Parameter | Type | Description |
| --------- | ---- | ----------- |
|           |      |             |

{% tabs %}
{% tab title="Lua" %}

```lua
local angle = 1.04719758033752; -- converts to 60 degrees
local tangent = Space.Math.Tan(angle);
Space.Log(tangent);
-- prints 1.73205089569092

local angle2 = Space.Math.Pi/3; -- converts to 60 degrees
local tangent2 = Space.Math.Tan(angle2);
Space.Log(tangent2);
-- prints 1.73205089569092
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Lua" %}

```lua
local angle = 1.04719758033752; -- converts to 60 degrees
local tangent = Space.Math.Tan(angle);
Space.Log(tangent);
-- prints 1.73205089569092

local angle2 = Space.Math.Pi/3; -- converts to 60 degrees
local tangent2 = Space.Math.Tan(angle2);
Space.Log(tangent2);
-- prints 1.73205089569092
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Lua" %}

```lua
local angle = 1.04719758033752; -- converts to 60 degrees
local tangent = Space.Math.Tan(angle);
Space.Log(tangent);
-- prints 1.73205089569092

local angle2 = Space.Math.Pi/3; -- converts to 60 degrees
local tangent2 = Space.Math.Tan(angle2);
Space.Log(tangent2);
-- prints 1.73205089569092
```

{% endtab %}
{% endtabs %}

## Static Attributes

### Pi

static readonly float **Pi** = 3.14159265358979f

*Returns the constant value of Π.*

{% tabs %}
{% tab title="Lua" %}

```lua
pi = Space.Math.Pi
```

{% endtab %}
{% endtabs %}
