# aego/math

> Aego 0.12.5 · development; docs build `4a32b816933e02930662f6a443cb25abe6050d090c55e237cbff5c44ae9b25f8`. [Build manifest](/docs-build.json).

There is no overloading: Vec3 variants carry a `3` suffix (`Length3`, `Dot3`) and int variants an `Int` suffix (`MinInt`, `ClampInt`). Angles are in radians; `Deg2Rad` and `Rad2Deg` convert. Vector arithmetic (`v + w`, `v * s`) is part of the language and needs no import.

## Example

```aes run
package demo

import (
    "aego/log"
    "aego/math"
)

script Orbit {
    @export var Radius float = 3
    @export var Center Vec2 = Vec2{1, 1}

    var angle float
    var frames int

    func Ready() {
        to := Vec2{3, 4}
        log.Info("length " + string(math.Length(to)) + ", heading " + string(math.Round(math.Rad2Deg(math.Angle(to)))) + " deg")
        log.Info("unit " + string(math.Normalize(to).X) + ", " + string(math.Normalize(to).Y))
        log.Info("clamp 12 to 0..10: " + string(math.Clamp(12, 0, 10)))
        log.Info("ammo after pickup: " + string(math.MinInt(25 + 10, 30)))
    }

    func Update(dt float) {
        angle += math.Tau / 2 * dt
        frames++
        if frames % 30 == 0 {
            p := Center + Vec2{math.Cos(angle), math.Sin(angle)} * Radius
            node.SetPosition(p)
            log.Info("frame " + string(frames) + ": at " + string(math.Round(p.X * 100) / 100) + ", " + string(math.Round(p.Y * 100) / 100) + ", " + string(math.Dist(p, Center)) + " from center")
        }
    }
}
```

## Abs

```
func Abs(x float) float
```

Returns the absolute value of x.

## AbsInt

```
func AbsInt(x int) int
```

Returns the absolute value of an int.

## Acos

```
func Acos(x float) float
```

Returns the angle in radians whose cosine is x, in [0, Pi].

## Angle

```
func Angle(v Vec2) float
```

Returns the direction of v in radians.

## Asin

```
func Asin(x float) float
```

Returns the angle in radians whose sine is x, in [-Pi/2, Pi/2].

## Atan2

```
func Atan2(y float, x float) float
```

Returns the angle in radians of the vector (x, y), in (-Pi, Pi].

## Ceil

```
func Ceil(x float) float
```

Returns the least whole number greater than or equal to x.

## Clamp

```
func Clamp(v float, lo float, hi float) float
```

Limits v to the range [lo, hi].

## ClampInt

```
func ClampInt(v int, lo int, hi int) int
```

Limits an int to the range [lo, hi].

## Cos

```
func Cos(x float) float
```

Returns the cosine of an angle in radians.

## Cross

```
func Cross(a Vec2, b Vec2) float
```

Returns the z component of the 3D cross product; its sign tells on which side b lies.

## Cross3

```
func Cross3(a Vec3, b Vec3) Vec3
```

Returns a vector perpendicular to a and b.

## Deg2Rad

```
func Deg2Rad(degrees float) float
```

Converts degrees to radians.

## Dist

```
func Dist(a Vec2, b Vec2) float
```

Returns the distance between two points.

## Dist3

```
func Dist3(a Vec3, b Vec3) float
```

Returns the distance between two 3D points.

## Dot

```
func Dot(a Vec2, b Vec2) float
```

Returns the dot product of two Vec2.

## Dot3

```
func Dot3(a Vec3, b Vec3) float
```

Returns the dot product of two Vec3.

## Exp

```
func Exp(x float) float
```

Returns e raised to the power x.

## Floor

```
func Floor(x float) float
```

Returns the greatest whole number less than or equal to x.

## Length

```
func Length(v Vec2) float
```

Returns the length of a Vec2.

## Length3

```
func Length3(v Vec3) float
```

Returns the length of a Vec3.

## Lerp

```
func Lerp(a float, b float, t float) float
```

Blends from a to b. t = 0 gives a, t = 1 gives b; t is not clamped.

## LerpVec2

```
func LerpVec2(a Vec2, b Vec2, t float) Vec2
```

Blends two Vec2 component-wise.

## LerpVec3

```
func LerpVec3(a Vec3, b Vec3, t float) Vec3
```

Blends two Vec3 component-wise.

## Log

```
func Log(x float) float
```

Returns the natural logarithm of x.

## Max

```
func Max(a float, b float) float
```

Returns the larger of a and b.

## MaxInt

```
func MaxInt(a int, b int) int
```

Returns the larger of two ints.

## Min

```
func Min(a float, b float) float
```

Returns the smaller of a and b.

## MinInt

```
func MinInt(a int, b int) int
```

Returns the smaller of two ints.

## Normalize

```
func Normalize(v Vec2) Vec2
```

Returns v scaled to length 1. A zero vector stays zero.

## Normalize3

```
func Normalize3(v Vec3) Vec3
```

Returns v scaled to length 1.

## Pi

```
const Pi float
```

Pi is the ratio of a circle's circumference to its diameter, about 3.14159.

## Pow

```
func Pow(x float, y float) float
```

Returns x raised to the power y.

## QuatAxisAngle

```
func QuatAxisAngle(axis Vec3, radians float) Quat
```

Builds a rotation of an angle in radians around an axis.

## QuatEuler

```
func QuatEuler(x float, y float, z float) Quat
```

Builds a rotation from Euler angles in radians, applied as Z·Y·X.

## QuatLookDir

```
func QuatLookDir(dir Vec3) Quat
```

Returns the rotation that points -Z along d.

## QuatMul

```
func QuatMul(a Quat, b Quat) Quat
```

Combines two rotations: b is applied first, then a.

## QuatRotate

```
func QuatRotate(q Quat, v Vec3) Vec3
```

Rotates a vector by q.

## QuatSlerp

```
func QuatSlerp(a Quat, b Quat, t float) Quat
```

Blends two rotations along the shortest arc.

## Rad2Deg

```
func Rad2Deg(radians float) float
```

Converts radians to degrees.

## Rotate

```
func Rotate(v Vec2, radians float) Vec2
```

Turns v clockwise by an angle in radians.

## Round

```
func Round(x float) float
```

Returns x rounded to the nearest whole number; halves round away from zero.

## Sign

```
func Sign(x float) float
```

Returns -1, 0 or 1 depending on the sign of x.

## Sin

```
func Sin(x float) float
```

Returns the sine of an angle in radians.

## Sqrt

```
func Sqrt(x float) float
```

Returns the square root of x. Negative x gives NaN.

## Tan

```
func Tan(x float) float
```

Returns the tangent of an angle in radians.

## Tau

```
const Tau float
```

Tau is a full turn in radians, 2·Pi.

## Trunc

```
func Trunc(x float) float
```

Drops the fractional part of x.
