Skip to content

Latest commit

 

History

History
489 lines (335 loc) · 6.5 KB

scalars.md

File metadata and controls

489 lines (335 loc) · 6.5 KB
id stdlib-scalars
title Int, Float, Bool Modules
slug /stdlib/scalars

Int

Int.tryParse : string -> int option

Parses an invariant integer string.

let maybeN = Int.tryParse "42"

Int.toString : int -> string

Formats an integer using invariant formatting.

let text = Int.toString 42

Float

Float.tryParse : string -> float option

Parses an invariant floating-point string.

let maybePi = Float.tryParse "3.14"

Float.toString : float -> string

Formats a float using a round-trippable representation.

let text = Float.toString 3.14

Bool

Bool.tryParse : string -> bool option

Parses true or false.

let maybeFlag = Bool.tryParse "true"

Bool.toString : bool -> string

Formats a boolean as lowercase text.

let text = Bool.toString true

Parsing pattern

Use parsing helpers with options:

let port =
  Int.tryParse "8080"
  |> Option.defaultValue 80

Pure support additions

Scalar math uses signed 64-bit integers and IEEE-754 doubles. Transcendental functions follow the host math library; last-bit results may differ across platforms, so they are not a portable bitwise cache-key format. Invalid floating math domains generally produce NaN/infinity as specified below.

Int.abs

Int.abs : int -> int

Returns the absolute value. The minimum signed 64-bit integer raises an error because its positive value is out of range.

Int.abs (0 - 3)
// 3

Int.sign

Int.sign : int -> int

Returns -1, 0, or 1.

Int.sign (0 - 3)
// -1

Int.min

Int.min : int -> int -> int

Returns the smaller value.

Int.min 2 3
// 2

Int.max

Int.max : int -> int -> int

Returns the larger value.

Int.max 2 3
// 3

Int.clamp

Int.clamp : int -> int -> int -> int

Bounds the value inclusively. Reversed bounds raise an error.

Int.clamp 0 10 20
// 10

Float.abs

Float.abs : float -> float

Returns the absolute value, propagating NaN.

Float.abs (0.0 - 3.0)
// 3

Float.sign

Float.sign : float -> int

Returns -1, 0, or 1. NaN raises an error.

Float.sign (0.0 - 3.0)
// -1

Float.min

Float.min : float -> float -> float

Returns the smaller value. Either NaN input produces NaN.

Float.min 2.0 3.0
// 2

Float.max

Float.max : float -> float -> float

Returns the larger value. Either NaN input produces NaN.

Float.max 2.0 3.0
// 3

Float.clamp

Float.clamp : float -> float -> float -> float

Bounds the value inclusively. Reversed or NaN bounds raise an error; a NaN value produces NaN.

Float.clamp 0.0 10.0 20.0
// 10

Float.floor

Float.floor : float -> float

Rounds toward negative infinity; the result remains a float.

Float.floor 1.8
// 1

Float.ceil

Float.ceil : float -> float

Rounds toward positive infinity; the result remains a float.

Float.ceil 1.2
// 2

Float.round

Float.round : float -> float

Rounds to the nearest integer-valued float, with ties to even.

Float.round 2.5
// 2

Float.truncate

Float.truncate : float -> float

Discards the fractional part toward zero; the result remains a float.

Float.truncate (0.0 - 1.8)
// -1

Float.sqrt

Float.sqrt : float -> float

Returns the square root. Negative inputs produce NaN.

Float.sqrt 9.0
// 3

Float.log

Float.log : float -> float

Returns the natural logarithm. Zero produces negative infinity; negative inputs produce NaN.

Float.log 1.0
// 0

Float.log10

Float.log10 : float -> float

Returns the base-10 logarithm. Zero produces negative infinity; negative inputs produce NaN.

Float.log10 100.0
// 2

Float.exp

Float.exp : float -> float

Returns the natural exponential. Overflow produces positive infinity.

Float.exp 0.0
// 1

Float.sin

Float.sin : float -> float

Returns the sine of an angle in radians.

Float.sin 0.0
// 0

Float.cos

Float.cos : float -> float

Returns the cosine of an angle in radians.

Float.cos 0.0
// 1

Float.tan

Float.tan : float -> float

Returns the tangent of an angle in radians.

Float.tan 0.0
// 0

Float.asin

Float.asin : float -> float

Returns the inverse sine in radians. Inputs outside [-1, 1] produce NaN.

Float.asin 0.0
// 0

Float.acos

Float.acos : float -> float

Returns the inverse cosine in radians. Inputs outside [-1, 1] produce NaN.

Float.acos 1.0
// 0

Float.atan

Float.atan : float -> float

Returns the inverse tangent in radians.

Float.atan 0.0
// 0

Float.pow

Float.pow : float -> float -> float

Raises the first argument to the power of the second.

Float.pow 2.0 3.0
// 8

Float.atan2

Float.atan2 : float -> float -> float

Returns the angle in radians for coordinates (y, x): the first argument is y, the second x.

Float.atan2 0.0 1.0
// 0

Float.isNaN

Float.isNaN : float -> bool

Tests for NaN.

Float.isNaN (Float.sqrt (0.0 - 1.0))
// true

Float.isInfinity

Float.isInfinity : float -> bool

Tests for either positive or negative infinity.

Float.isInfinity (Float.exp 1000.0)
// true

Float.isFinite

Float.isFinite : float -> bool

Tests that the value is neither NaN nor infinity.

Float.isFinite 1.0
// true

Float.pi

Float.pi : float

The double-precision constant π. This is a value, not a function.

Float.pi > 3.0 && Float.pi < 4.0
// true

Int.toFloat

Int.toFloat : int -> float

Converts an integer to a float. Values beyond the exact integer range of a double may lose precision.

Int.toFloat 3
// 3

Float.tryToInt

Float.tryToInt : float -> int option

Truncates toward zero and returns Some int. NaN, infinity, or an input outside [-2^63, 2^63) returns None.

Float.tryToInt (0.0 - 2.9)
// Some -2