Skip to content

Other functions

Error handling · Floating point · Quantities · Chemical elements · Mixed unit conversion · Temperature conversion · Color format conversion

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

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

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

Converts from degree Celsius (°C) to Kelvin. More information here.

fn from_celsius(t_celsius: Scalar) -> Temperature

300 °C in Kelvin.

from_celsius(300)

    = 573.15 K    [Temperature]
Run this example

celsius

Converts from Kelvin to degree Celcius (°C). This can be used on the right hand side of a conversion operator: 200 K -> celsius. More information here.

fn celsius(t_kelvin: Temperature) -> Scalar

300 K in degree Celsius.

300K -> celsius

    = 26.85
Run this example

from_fahrenheit

Converts from degree Fahrenheit (°F) to Kelvin. More information here.

fn from_fahrenheit(t_fahrenheit: Scalar) -> Temperature

300 °F in Kelvin.

from_fahrenheit(300)

    = 422.039 K    [Temperature]
Run this example

fahrenheit

Converts from Kelvin to degree Fahrenheit (°F). This can be used on the right hand side of a conversion operator: 200 K -> fahrenheit. More information here.

fn fahrenheit(t_kelvin: Temperature) -> Scalar

300 K in degree Fahrenheit.

300K -> fahrenheit

    = 80.33
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