Rounding and remainders v0.1.0

Source
Rounding to an integral value in a chosen direction, and the floating-point remainder.

Ceil

pub func Ceil(x: float64) -> float64
pub func Ceil(x: float32) -> float32

Rounds x toward positive infinity to an integral value. A negative value between zero and negative one becomes -0.0. A NaN or infinity passes through unchanged.

OverloadSummary
pub func Ceil(x: float64) -> float64Rounds x toward positive infinity to an integral value.
pub func Ceil(x: float32) -> float32Rounds x toward positive infinity at float32 precision.

Source

Parameters

NameTypeDescription
xfloat64 / float32value to round upward

Returns: the smallest integral value greater than or equal to x

Floor

pub func Floor(x: float64) -> float64
pub func Floor(x: float32) -> float32

Rounds x toward negative infinity to an integral value. The sign of zero is preserved. A NaN or infinity passes through unchanged.

OverloadSummary
pub func Floor(x: float64) -> float64Rounds x toward negative infinity to an integral value.
pub func Floor(x: float32) -> float32Rounds x toward negative infinity at float32 precision.

Source

Parameters

NameTypeDescription
xfloat64 / float32value to round downward

Returns: the largest integral value less than or equal to x

Mod

pub func Mod(x: float64, y: float64) -> float64
pub func Mod(x: float32, y: float32) -> float32

Computes the floating-point remainder of x / y, with the sign of x. The result is x - n * y, where n is x / y truncated toward zero. Its magnitude is less than |y|. A zero divisor, an infinite dividend, or a NaN operand returns a NaN; an infinite divisor returns x. The significands are reduced with integer long division so the intermediate product cannot discard the remainder.

OverloadSummary
pub func Mod(x: float64, y: float64) -> float64Computes the floating-point remainder of x / y, with the sign of x.
pub func Mod(x: float32, y: float32) -> float32Computes the floating-point remainder of x / y at float32 precision, with the sign of x.

Source

Parameters

NameTypeDescription
xfloat64 / float32dividend
yfloat64 / float32divisor

Returns: the exact remainder, or a NaN when no remainder is defined

Round

pub func Round(x: float64) -> float64
pub func Round(x: float32) -> float32

Rounds x to the nearest integral value, with halfway cases away from zero. Thus Round(2.5) is 3.0 and Round(-2.5) is -3.0. The sign of zero is preserved, and a NaN or infinity passes through unchanged. The fractional part is compared directly instead of adding one half, which avoids prematurely rounding the largest representable value below 0.5 up to 1.0.

OverloadSummary
pub func Round(x: float64) -> float64Rounds x to the nearest integral value, with halfway cases away from zero.
pub func Round(x: float32) -> float32Rounds x to the nearest integral value at float32 precision, with halfway cases away from zero.

Source

Parameters

NameTypeDescription
xfloat64 / float32value to round

Returns: the nearest integral value

RoundingMode

pub enum RoundingMode: int32 {
    TowardZero = 0,
    TowardNegative = 1,
    TowardPositive = 2,
    ToNearestTiesAway = 3,
    ToNearestTiesEven = 4
}

Which way a value that falls between two integers should go. Copy.

Source

Cases

NameValueDescription
TowardZero0Toward zero, discarding the fraction: Trunc.
TowardNegative1Downward, toward negative infinity: Floor.
TowardPositive2Upward, toward positive infinity: Ceil.
ToNearestTiesAway3To the nearest, with a halfway value going away from zero: Round. What most people mean by rounding, and what school teaches.
ToNearestTiesEven4To the nearest, with a halfway value going to the even neighbour. IEEE 754's default, and the one that keeps a long run of roundings from drifting: ties away from zero rounds up half the time and down never.

RoundTiesToEven

pub func RoundTiesToEven(value: float64) -> float64
pub func RoundTiesToEven(value: float32) -> float32

The nearest integer to value, with a halfway value going to the even neighbour. RoundTiesToEven(2.5) is 2 and RoundTiesToEven(3.5) is 4, where Round gives 3 and 4. The sign of a zero is preserved, and a NaN or infinity passes through unchanged.

OverloadSummary
pub func RoundTiesToEven(value: float64) -> float64The nearest integer to value, with a halfway value going to the even neighbour.
pub func RoundTiesToEven(value: float32) -> float32Rounds value to the nearest integral float32, with halfway cases going to the even neighbour.

Source

Parameters

NameTypeDescription
valuefloat64 / float32value to round

Returns: the nearest integral value, resolving halfway cases toward the even neighbour

RoundTo

pub func RoundTo(
    value: float64,
    mode: RoundingMode
) -> float64

pub func RoundTo(
    value: float32,
    mode: RoundingMode
) -> float32

Rounds value in the requested direction at float32 precision.

OverloadSummary
pub func RoundTo(value: float64, mode: RoundingMode) -> float64value rounded to an integer in the named direction.
pub func RoundTo(value: float32, mode: RoundingMode) -> float32Rounds value in the requested direction at float32 precision.

Source

Parameters

NameTypeDescription
valuefloat64 / float32value to round
modeRoundingModedirection and halfway rule to use

Returns: the integral value selected by mode

ToIntegerChecked<T>

pub func ToIntegerChecked<T>(
    value: float64,
    sample: T,
    mode: RoundingMode,
    result: *var T
) -> bool

value rounded in the named direction and converted to an integer, reporting whether it fits. sample is read by value and only its type matters, exactly as Core's MaximumOf reads one. Returns true when the rounded value is not representable in the integer type — because it is a NaN, an infinity, or simply out of range — and leaves result zero in that case. This is the only place a float becomes an integer, and it refuses rather than saturating, because a caller who has not thought about the out-of-range case is better served by a flag than by a plausible wrong number. result must be non-null and writable.

Source

Type parameters

NameDescription
Tdestination integer type

Parameters

NameTypeDescription
valuefloat64floating-point value to convert
sampleTvalue used only to select T
modeRoundingModerounding rule applied before conversion
result*var Treceives the converted integer or zero on failure

Returns: true when the rounded value is not representable as T

Trunc

pub func Trunc(x: float64) -> float64
pub func Trunc(x: float32) -> float32

Rounds x toward zero to an integral value at float32 precision. The wide result is exactly representable at the input width, so narrowing it back to float32 loses nothing.

OverloadSummary
pub func Trunc(x: float64) -> float64Rounds x toward zero to an integral value.
pub func Trunc(x: float32) -> float32Rounds x toward zero to an integral value at float32 precision.

Source

Parameters

NameTypeDescription
xfloat64 / float32value to truncate

Returns: the integral part of x