Float

Associated Functions for the Float type

Built-in associated functions for the Float type.


Trigonometric

sin

fn sin(self) -> Float

Returns the sine of the angle in radians.

Returns

The sine of self, in the range [-1.0, 1.0].

Example

0.0.sin()       // 0.0
(PI / 2).sin()  // 1.0
PI.sin()        // ~0.0

cos

fn cos(self) -> Float

Returns the cosine of the angle in radians.

Returns

The cosine of self, in the range [-1.0, 1.0].

Example

0.0.cos()       // 1.0
(PI / 2).cos()  // ~0.0
PI.cos()        // -1.0

asin

fn asin(self) -> Float

Returns the arcsine (inverse sine) in radians.

Returns

The arcsine of self, in the range [-PI/2, PI/2].

Example

0.0.asin()     // 0.0
1.0.asin()     // PI/2
(-1.0).asin()  // -PI/2

acos

fn acos(self) -> Float

Returns the arccosine (inverse cosine) in radians.

Returns

The arccosine of self, in the range [0, PI].

Example

1.0.acos()     // 0.0
0.0.acos()     // PI/2
(-1.0).acos()  // PI

atan2

fn atan2(self, x: Float) -> Float

Returns the arctangent of self / x in radians, using the signs of both arguments to determine the quadrant.

Parameters

  • x — The x-coordinate

Returns

The arctangent of y / x (where y is self), in the range [-PI, PI].

Example

1.0.atan2(1.0)     //  PI/4 (45 degrees)
1.0.atan2(-1.0)    //  3*PI/4 (135 degrees)
(-1.0).atan2(1.0)  // -PI/4 (-45 degrees)

tan

Upcoming - Not yet implemented

fn tan(self) -> Float

Returns the tangent of the angle in radians.

Returns

The tangent of self.

Example

0.0.tan()       // 0.0
(PI / 4).tan()  // 1.0
(PI / 2).tan()  // undefined (approaches infinity)

Basic Math

abs

fn abs(self) -> Float

Returns the absolute value.

See also [Int::abs] for integer absolute value.

Returns

The non-negative value of self.

Example

-42.5.abs()  // 42.5
 42.5.abs()  // 42.5

sign

fn sign(self) -> Float

Returns the sign of the floating-point number.

See also [Int::sign].

Returns

  • -1.0 if self is negative
  • 0.0 if self is zero
  • 1.0 if self is positive

Example

 10.5.sign()  //  1.0
  0.0.sign()  //  0.0
-42.3.sign()  // -1.0

floor

fn floor(self) -> Int

Returns the largest integer less than or equal to self.

See also [round] for rounding to nearest integer.

Returns

The floor of self as an [Int].

Example

 3.7.floor()   //  3
 3.0.floor()   //  3
-2.3.floor()   // -3

round

fn round(self) -> Int

Returns the nearest integer, rounding half-way cases away from zero.

See also [floor] for truncation toward negative infinity.

Returns

The rounded value of self as an [Int].

Example

 3.4.round()   //  3
 3.5.round()   //  4
 3.6.round()   //  4
-2.5.round()   // -3

ceil

Upcoming - Not yet implemented

fn ceil(self) -> Int

Returns the smallest integer greater than or equal to self.

See also [floor] for truncation toward negative infinity.

Returns

The ceiling of self as an [Int].

Example

 3.1.ceil()   //  4
 3.0.ceil()   //  3
-2.7.ceil()   // -2

fract

Upcoming - Not yet implemented

fn fract(self) -> Float

Returns the fractional part of the number.

Equivalent to self - self.floor(). Always returns a value in the range [0.0, 1.0).

Returns

The fractional part of self.

Example

 3.7.fract()   // 0.7
 3.0.fract()   // 0.0
-2.3.fract()   // 0.7 (not -0.3)

Comparison

min

fn min(self, other: Float) -> Float

Returns the smaller of two floating-point numbers.

Parameters

  • other — The value to compare against

Returns

The minimum of self and other.

Example

10.5.min(20.3)  // 10.5
-5.2.min(3.1)   // -5.2

max

fn max(self, other: Float) -> Float

Returns the larger of two floating-point numbers.

Parameters

  • other — The value to compare against

Returns

The maximum of self and other.

Example

10.5.max(20.3)  // 20.3
-5.2.max(3.1)   // 3.1

clamp

fn clamp(self, min_value: Float, max_value: Float) -> Float

Restricts the value to be within the specified range.

See also [Int::clamp].

Parameters

  • min_value — The minimum allowed value
  • max_value — The maximum allowed value

Returns

  • min_value if self < min_value
  • max_value if self > max_value
  • self otherwise

Example

  5.5.clamp(0.0, 10.0)   //  5.5
 15.0.clamp(0.0, 10.0)   // 10.0
 -5.0.clamp(0.0, 10.0)   //  0.0

Arithmetic

div

fn div(self, divisor: Float) -> Float

Performs floating-point division.

Parameters

  • divisor — The divisor

Returns

The quotient of self / divisor.

Example

7.0.div(2.0)   // 3.5
9.0.div(4.0)   // 2.25

rem

fn rem(self, divisor: Float) -> Float

Performs the floating-point remainder operation.

Parameters

  • divisor — The divisor

Returns

The remainder of self / divisor.

Example

7.5.rem(2.0)   // 1.5
5.0.rem(2.5)   // 0.0

normalize

fn normalize(self, other: Float) -> (Float, Float)

Returns a normalized version of the vector (unit vector in the same direction).

magnitude

fn magnitude(self, other: Float) -> Float

Returns the magnitude (length) of the 2D vector.

Example

x := 10.2
y := 20.3

l := x.magnitude(y)

magnitude_sqr_scaled

fn magnitude_sqr_scaled(self, other: Float) -> Float

Returns the squared magnitude of the vector (more efficient than magnitude when comparing lengths).


Random

rnd

fn rnd(self) -> Float

Returns a deterministic pseudo-random float based on the seed value.

Uses self as a seed to a noise function, producing the same output for the same input. This is useful for procedural generation where repeatability is required.

Uses a high-quality, fast noise function designed for procedural generation. (e.g. squirrel noise function v5)

See also [Int::rnd].

Returns

A pseudo-random float in the range [0.0, 1.0]. The same seed always produces the same result.

Example

42.5.rnd()   // Always returns the same value for seed 42.5
100.0.rnd()  // Always returns the same value for seed 100.0

Conversion

string

fn string(self) -> String

Converts the floating-point number to a [String].

See also [Int::string].

Returns

The [String] representation of self.

Example

 42.5.string()  // "42.5"
-15.0.string()  // "-15.0"
  0.0.string()  // "0.0"