struct

UFixed(N)

Inherits Comparable < Comparable < Comparable < FixedDecimal::Value < Struct < Value < Object

An unsigned binary fixed-point number stored in a UInt128.

The low N bits hold the fraction and the high 128 - N bits hold the integer part, so the value is raw / 2**N. N must be in 0..128.

ratio = UFixed(128).new("0.75") # all 128 bits are fraction
ratio.to_f64                    # => 0.75

Constructors

epsilon

The smallest positive value, one unit in the last place.

Source
from_sign_magnitude(negative : Bool, magnitude : UInt128 | Nil) : self

Builds a value from a sign and a magnitude in units of the least significant bit. Raises OverflowError when out of range, or when magnitude is nil (the convention for an intermediate overflow).

Source
max

The largest value, 2**(128 - N) - 2**-N.

Source
min

Zero.

Source
new(value : BigInt) : self

Converts exactly. Raises OverflowError if it is out of range.

Source
new(value : Int)

Converts an integer exactly. Raises OverflowError if it is out of range.

Source
new(value : BigFloat) : self

Converts, rounding half-to-even. Raises OverflowError if it is out of range.

Source
new(value : Float)

Converts a float, rounding half-to-even. Raises ArgumentError for NaN or infinity and OverflowError if it is out of range.

Source
new(value : String)

Parses a decimal string; see .parse.

Source
new(value : FixedDecimal::Value)

Converts from any Fixed or UFixed, rounding half-to-even when fraction bits are lost. Raises OverflowError if it is out of range.

Source
new(value : BigRational) : self

Converts, rounding half-to-even. Raises OverflowError if it is out of range.

Source
new(value : BigDecimal) : self

Converts, rounding half-to-even. Raises OverflowError if it is out of range.

Source
new(*, raw : UInt128)

Wraps raw storage directly.

Source
one

One. Raises OverflowError when one is not representable.

Source
parse(string : String, *, whitespace : Bool = true) : self

Parses a decimal string such as "-12.375", "1_000.5", ".5" or "6.02e23", rounding half-to-even to the nearest representable value.

Raises ArgumentError if string is not a valid decimal number and OverflowError if it is out of range. Leading and trailing whitespace (ASCII only, as with String#to_i) is ignored when whitespace is true.

Source
zero

Zero.

Source

Class methods

fraction_bits

The number of fraction bits, N.

Source
integer_bits

The number of integer bits, including the sign bit if there is one.

Source
parse?(string : String, *, whitespace : Bool = true) : self | Nil

Like .parse, but returns nil instead of raising.

Source

Instance methods

raw

The storage; the value is raw / 2**N.

Source
sign_magnitude

The value as {negative, magnitude}; negative is always false.

Source