Skip to content

Other functions

Error handling · Debugging · Floating point · Quantities · Chemical elements · Mixed unit conversion · Temperature conversion · Speed of sound · Color format conversion · Celestial calculations

Error handling

Defined in: core::error

error

Throw an error with the specified message. Stops the execution of the program.

fn error<T>(message: String) -> T

Debugging

Defined in: core::debug

inspect

Print the value (and type) of the argument and return it. Useful for debugging.

fn inspect<T>(x: T) -> T

Example

inspect(36 km / 1.5 hours) * 1 day

    = 576 km    [Length]
Run this example

Example

range(1, 3) |> map(sqr) |> map(inspect) |> sum

    = 14
Run this example

Floating point

Defined in: core::numbers

is_nan

Returns true if the input is NaN. More information here.

fn is_nan<T: Dim>(n: T) -> Bool

Example

is_nan(37)

    = false    [Bool]
Run this example

Example

is_nan(NaN)

    = true    [Bool]
Run this example

is_infinite

Returns true if the input is positive infinity or negative infinity. More information here.

fn is_infinite<T: Dim>(n: T) -> Bool

Example

is_infinite(37)

    = false    [Bool]
Run this example

Example

is_infinite(-inf)

    = true    [Bool]
Run this example

is_finite

Returns true if the input is neither infinite nor NaN.

fn is_finite<T: Dim>(n: T) -> Bool

Example

is_finite(37)

    = true    [Bool]
Run this example

Example

is_finite(-inf)

    = false    [Bool]
Run this example

is_zero

Returns true if the input is 0 (zero).

fn is_zero<D: Dim>(value: D) -> Bool

Example

is_zero(37)

    = false    [Bool]
Run this example

Example

is_zero(0)

    = true    [Bool]
Run this example

is_nonzero

Returns true unless the input is 0 (zero).

fn is_nonzero<D: Dim>(value: D) -> Bool

Example

is_nonzero(37)

    = true    [Bool]
Run this example

Example

is_nonzero(0)

    = false    [Bool]
Run this example

is_integer

Returns true if the input is an integer.

fn is_integer(x: Scalar) -> Bool

Example

is_integer(3)

    = true    [Bool]
Run this example

Example

is_integer(pi)

    = false    [Bool]
Run this example

Quantities

Defined in: core::quantities

value_of

Extract the plain value of a quantity (the 20 in 20 km/h). This can be useful in generic code, but should generally be avoided otherwise.

fn value_of<T: Dim>(x: T) -> Scalar

Example

value_of(20 km/h)

    = 20
Run this example

unit_of

Extract the unit of a quantity (the km/h in 20 km/h). This can be useful in generic code, but should generally be avoided otherwise. Returns an error if the quantity is zero.

fn unit_of<T: Dim>(x: T) -> T

Example

unit_of(20 km/h)

    = 1 km/h    [Velocity]
Run this example

base_unit_of

Extract the base unit of a quantity without prefixes (e.g., base_unit_of(5 km) returns m instead of km). This can be useful for normalizing values. Returns an error if the quantity is zero.

fn base_unit_of<T: Dim>(x: T) -> T

Example

base_unit_of(5 km)

    = 1 m    [Length]
Run this example

Example

5 km / base_unit_of(5 km)

    = 5000
Run this example

has_unit

Returns true if quantity has the same unit as unit_query, or if quantity evaluates to zero.

fn has_unit<T: Dim>(quantity: T, unit_query: T) -> Bool

Example

has_unit(20 km/h, km/h)

    = true    [Bool]
Run this example

Example

has_unit(20 km/h, m/s)

    = false    [Bool]
Run this example

is_dimensionless

Returns true if quantity is dimensionless, or if quantity is zero.

fn is_dimensionless<T: Dim>(quantity: T) -> Bool

Example

is_dimensionless(10)

    = true    [Bool]
Run this example

Example

is_dimensionless(10 km/h)

    = false    [Bool]
Run this example

unit_name

Returns a string representation of the unit of quantity. Returns an empty string if quantity is dimensionless.

fn unit_name<T: Dim>(quantity: T) -> String

Example

unit_name(20)

    = ""    [String]
Run this example

Example

unit_name(20 m^2)

    = "m²"    [String]
Run this example

Example

unit_name(20 km/h)

    = "km/h"    [String]
Run this example

quantity_cast

Unsafe function that returns the quantity from unmodified with the target dimension To. This can be useful in generic code, but should generally be avoided otherwise.

fn quantity_cast<From: Dim, To: Dim>(f: From, t: To) -> To

Example

quantity_cast(1 nm, m)

    = 1 nm    [Length]
Run this example

Chemical elements

Defined in: chemistry::elements

element (Chemical element)

Get properties of a chemical element by its symbol or name (case-insensitive).

fn element(pattern: String) -> ChemicalElement

Get the entire element struct for hydrogen.

element("H")

    = ChemicalElement { symbol: "H", name: "Hydrogen", atomic_number: 1, group: 1, group_name: "Alkali metals", period: 1, melting_point: 13.99 K, boiling_point: 20.271 K, density: 0.00008988 g/cm³, electron_affinity: 0.754 eV, ionization_energy: 13.598 eV, vaporization_heat: 0.904 kJ/mol }    [ChemicalElement]
Run this example

Get the ionization energy of hydrogen.

element("hydrogen").ionization_energy

    = 13.598 eV    [Energy or Torque]
Run this example

Mixed unit conversion

Defined in: units::mixed

unit_list (Unit list)

Convert a value to a mixed representation using the provided units.

fn unit_list<D: Dim>(units: List<D>, value: D) -> List<D>

Example

5500 m |> unit_list([miles, yards, feet, inches])

    = [3 mi, 734 yd, 2 ft, 7.43307 in]    [List<Length>]
Run this example

DMS (Degrees, minutes, seconds)

Convert an angle to a mixed degrees, (arc)minutes, and (arc)seconds representation. Also called sexagesimal degree notation. More information here.

fn DMS(alpha: Angle) -> List<Angle>

Example

46.5858° -> DMS

    = [46°, 35, 8.88]    [List<Scalar>]
Run this example

DM (Degrees, decimal minutes)

Convert an angle to a mixed degrees and decimal minutes representation. More information here.

fn DM(alpha: Angle) -> List<Angle>

Example

46.5858° -> DM

    = [46°, 35.148]    [List<Scalar>]
Run this example

feet_and_inches (Feet and inches)

Convert a length to a mixed feet and inches representation. More information here.

fn feet_and_inches(length: Length) -> List<Length>

Example

180 cm -> feet_and_inches

    = [5 ft, 10.8661 in]    [List<Length>]
Run this example

pounds_and_ounces (Pounds and ounces)

Convert a mass to a mixed pounds and ounces representation. More information here.

fn pounds_and_ounces(mass: Mass) -> List<Mass>

Example

1 kg -> pounds_and_ounces

    = [2 lb, 3.27396 oz]    [List<Mass>]
Run this example

Temperature conversion

Defined in: physics::temperature_conversion

from_celsius

fn from_celsius(t_celsius: Scalar) -> Temperature

°C

Converts from Kelvin to degree Celsius (°C). Can be used on the right hand side of a conversion operator. More information here.

fn °C(t_kelvin: Temperature) -> Scalar

Convert 300 K to degree Celsius.

300 K -> °C

    = 26.85
Run this example

Convert 55 °F to degree Celsius.

55 °F -> °C

    = 12.7778
Run this example

celsius

An alias for °C.

fn celsius(t_kelvin: Temperature) -> Scalar

degree_celsius

An alias for °C.

fn degree_celsius(t_kelvin: Temperature) -> Scalar

from_fahrenheit

fn from_fahrenheit(t_fahrenheit: Scalar) -> Temperature

°F

Converts from Kelvin to degree Fahrenheit (°F). Can be used on the right hand side of a conversion operator. More information here.

fn °F(t_kelvin: Temperature) -> Scalar

Convert 300 K to degree Fahrenheit.

300 K -> °F

    = 80.33
Run this example

Convert 25 °C to degree Fahrenheit.

25 °C -> °F

    = 77
Run this example

fahrenheit

An alias for °F.

fn fahrenheit(t_kelvin: Temperature) -> Scalar

degree_fahrenheit

An alias for °F.

fn degree_fahrenheit(t_kelvin: Temperature) -> Scalar

Speed of sound

Defined in: physics::speed_of_sound

speed_of_sound (Speed of sound in dry air)

Calculate the speed of sound in dry air as a function of air temperature. More information here.

fn speed_of_sound(T_air: Temperature) -> Velocity

Example

speed_of_sound(20 °C)

    = 343.263 m/s    [Velocity]
Run this example

Color format conversion

Defined in: extra::color

rgb

Create a Color from RGB (red, green, blue) values in the range \( [0, 256) \).

fn rgb(red: Scalar, green: Scalar, blue: Scalar) -> Color

Example

use extra::color
rgb(125, 128, 218)

    = Color { red: 125, green: 128, blue: 218 }    [Color]
Run this example

color

Create a Color from a (hexadecimal) value.

fn color(rgb_hex: Scalar) -> Color

Example

use extra::color
color(0xff7700)

    = Color { red: 255, green: 119, blue: 0 }    [Color]
Run this example

color_rgb

Convert a color to its RGB representation.

fn color_rgb(color: Color) -> String

Example

use extra::color
cyan -> color_rgb

    = "rgb(0, 255, 255)"    [String]
Run this example

color_rgb_float

Convert a color to its RGB floating point representation.

fn color_rgb_float(color: Color) -> String

Example

use extra::color
cyan -> color_rgb_float

    = "rgb(0.000, 1.000, 1.000)"    [String]
Run this example

color_hex

Convert a color to its hexadecimal representation.

fn color_hex(color: Color) -> String

Example

use extra::color
rgb(225, 36, 143) -> color_hex

    = "#e1248f"    [String]
Run this example

Celestial calculations

Defined in: extra::celestial

sunrise_sunset (Sunrise and sunset)

Compute sunrise, solar noon (transit), and sunset times for a given location and date.

fn sunrise_sunset(position: Position, dt: DateTime) -> SunTimes

Example

use extra::celestial
sunrise_sunset(Position { lat: 40.713°, lon: -74.006° }, datetime("2023-03-21 12:00:00 America/New_York"))

    = SunTimes { sunrise: 2023-03-21 10:57:38 UTC, transit: 2023-03-21 17:03:09 UTC, sunset: 2023-03-21 23:08:40 UTC }    [SunTimes]
Run this example

moon_phase (Moon phase)

Compute the moon phase for a given date and time. Returns the phase from 0 to 1 lunar_cycle, where 0 is a new moon and 0.5 lunar_cycle is a full moon.

fn moon_phase(dt: DateTime) -> LunarCycle

Example

use extra::celestial
datetime("2026-01-30 12:00:00 UTC") -> moon_phase

    = 0.402656 lunar_cycle    [LunarCycle]
Run this example

moon_phase_name (Moon phase name)

Convert a moon phase to its name and Unicode symbol (e.g., "🌘 Waning Crescent").

fn moon_phase_name(phase: LunarCycle) -> String

Example

use extra::celestial
datetime("2026-01-30 12:00:00 UTC") -> moon_phase -> moon_phase_name

    = "🌔 Waxing Gibbous"    [String]
Run this example