Rounding and remainders v0.1.0
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.
| Overload | Summary |
|---|---|
pub func Ceil(x: float64) -> float64 | Rounds x toward positive infinity to an integral value. |
pub func Ceil(x: float32) -> float32 | Rounds x toward positive infinity at float32 precision. |
Parameters
| Name | Type | Description |
|---|---|---|
x | float64 / float32 | value 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.
| Overload | Summary |
|---|---|
pub func Floor(x: float64) -> float64 | Rounds x toward negative infinity to an integral value. |
pub func Floor(x: float32) -> float32 | Rounds x toward negative infinity at float32 precision. |
Parameters
| Name | Type | Description |
|---|---|---|
x | float64 / float32 | value 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.
| Overload | Summary |
|---|---|
pub func Mod(x: float64, y: float64) -> float64 | Computes the floating-point remainder of x / y, with the sign of x. |
pub func Mod(x: float32, y: float32) -> float32 | Computes the floating-point remainder of x / y at float32 precision, with the sign of x. |
Parameters
| Name | Type | Description |
|---|---|---|
x | float64 / float32 | dividend |
y | float64 / float32 | divisor |
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.
| Overload | Summary |
|---|---|
pub func Round(x: float64) -> float64 | Rounds x to the nearest integral value, with halfway cases away from zero. |
pub func Round(x: float32) -> float32 | Rounds x to the nearest integral value at float32 precision, with halfway cases away from zero. |
Parameters
| Name | Type | Description |
|---|---|---|
x | float64 / float32 | value 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.
Cases
| Name | Value | Description |
|---|---|---|
TowardZero | 0 | Toward zero, discarding the fraction: Trunc. |
TowardNegative | 1 | Downward, toward negative infinity: Floor. |
TowardPositive | 2 | Upward, toward positive infinity: Ceil. |
ToNearestTiesAway | 3 | To the nearest, with a halfway value going away from zero: Round. What most people mean by rounding, and what school teaches. |
ToNearestTiesEven | 4 | To 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.
| Overload | Summary |
|---|---|
pub func RoundTiesToEven(value: float64) -> float64 | The nearest integer to value, with a halfway value going to the even neighbour. |
pub func RoundTiesToEven(value: float32) -> float32 | Rounds value to the nearest integral float32, with halfway cases going to the even neighbour. |
Parameters
| Name | Type | Description |
|---|---|---|
value | float64 / float32 | value 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.
| Overload | Summary |
|---|---|
pub func RoundTo(value: float64, mode: RoundingMode) -> float64 | value rounded to an integer in the named direction. |
pub func RoundTo(value: float32, mode: RoundingMode) -> float32 | Rounds value in the requested direction at float32 precision. |
Parameters
| Name | Type | Description |
|---|---|---|
value | float64 / float32 | value to round |
mode | RoundingMode | direction 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.
Type parameters
| Name | Description |
|---|---|
T | destination integer type |
Parameters
| Name | Type | Description |
|---|---|---|
value | float64 | floating-point value to convert |
sample | T | value used only to select T |
mode | RoundingMode | rounding rule applied before conversion |
result | *var T | receives 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.
| Overload | Summary |
|---|---|
pub func Trunc(x: float64) -> float64 | Rounds x toward zero to an integral value. |
pub func Trunc(x: float32) -> float32 | Rounds x toward zero to an integral value at float32 precision. |
Parameters
| Name | Type | Description |
|---|---|---|
x | float64 / float32 | value to truncate |
Returns: the integral part of x