Import std.math.fixed
std.math.fixed
Fixed-point numbers, whose math gives the same bits on every OS and CPU.
Fixed32 and Fixed64: fixed-point numbers, 16.16 in an Int32 and 32.32 in an Int64, whose math is all integer math, so the same program gives the same bits on every OS and CPU, in debug builds and release, for lockstep multiplayer and replays. * rounds to the nearest, halves up, and / to the nearest, halves away from 0; Sqrt rounds to the nearest; Sin and Cos reduce their angle by Pi / 2 to 94 bits and sum their series in 62, then round, and where that lands too near halfway to tell which way, work it out again in 124 bits, from Pi / 2 to 160. Overflow traps in debug builds and wraps in release, as an Int's does, and dividing by 0 panics.
Examples
Local half = Fixed64.FromDouble(0.5)
Expect(half + half == Fixed64.FromInt(1))
Expect(Sqrt(Fixed64.FromInt(9)) == Fixed64.FromInt(3))
Expect(Sin(Fixed32.FromInt(0)) == Fixed32.FromInt(0))
Expect(IsClose(Cos(Fixed64.Pi).ToDouble(), -1.0, 0.0001))std/math/fixed.fb
Types
Fixed32
struct
Public Struct Fixed32 Implements Numeric, Comparable, Hashable, Bounded, Printable, Defaultable
A number with 16 bits for its whole part, its sign among them, and 16 after the point: from -32768 to 32767.99998, in steps of 1 / 65536.
Examples
Local three = Fixed32.FromInt(3)
Local four = Fixed32.FromInt(4)
Expect((three / four).ToDouble() == 0.75)
Expect(three * four == Fixed32.FromInt(12))
Expect(Fixed32.FromDouble(1.5).Raw == 98304)
Expect(Sqrt(Fixed32.FromInt(16)) == four)-
The Fixed32 these
bitsare, as Raw gives them. -
The whole number. One outside -32768 to 32767 panics.
-
The Double, rounded to the nearest Fixed32, halves away from 0. One outside a Fixed32's range, or NaN, panics.
-
The Float, rounded to the nearest Fixed32, halves away from 0. One outside a Fixed32's range, or NaN, panics.
-
The zero Fixed32, which adding leaves alone.
-
The Fixed32 1, raw 65536, which multiplying leaves alone.
-
The zero Fixed32.
-
The lowest Fixed32, -32768.
-
The highest Fixed32, about 32767.99998.
-
The
valueas a Double, which holds every one exactly. -
Pi, and a turn, 2 Pi, rounded to a Fixed32's steps.
-
A turn, 2 Pi, rounded to a Fixed32's steps.
-
The value's bits, the value times 65536.
-
The value as a Double, which holds it exactly.
-
The value as a Float, rounded to the nearest.
-
The whole part, toward 0, as As takes a Float's.
-
The value as a Fixed64, which holds it exactly.
-
Its value, as the Double it is shows.
-
aplusb. Panics in a debug build when it's past what a Fixed32 holds, and wraps in a release one. -
aminusb. Panics in a debug build when it's past what a Fixed32 holds, and wraps in a release one. -
anegated. -
Whether
ais less thanb. -
atimesb. Panics in a debug build when it's past what a Fixed32 holds, and wraps in a release one. Rounded to the nearest. -
atimesb. -
atimesb. -
adivided byb. Abof 0 panics, and a quotient past what it holds does as + does. -
adivided byb.
Fixed64
struct
Public Struct Fixed64 Implements Numeric, Comparable, Hashable, Bounded, Printable, Defaultable
A number with 32 bits for its whole part, its sign among them, and 32 after the point: from -2147483648 to 2147483647.9999999998, in steps of 1 / 4294967296.
-
The Fixed64 these
bitsare, as Raw gives them. -
The whole number. One outside an Int's range panics.
-
The Double, rounded to the nearest Fixed64, halves away from 0. One outside a Fixed64's range, or NaN, panics.
-
The Float, rounded to the nearest Fixed64. One outside a Fixed64's range, or NaN, panics.
-
The zero Fixed64, which adding leaves alone.
-
The Fixed64 1, raw 4294967296, which multiplying leaves alone.
-
The zero Fixed64.
-
The lowest Fixed64, -2147483648.
-
The highest Fixed64, about 2147483647.9999999998.
-
The
valueas the nearest Double, which holds its 53 most significant bits. -
Pi, and a turn, 2 Pi, rounded to a Fixed64's steps.
-
A turn, 2 Pi, rounded to a Fixed64's steps.
-
The value's bits, the value times 4294967296.
-
The value as a Double, rounded to the nearest, which holds it exactly up to 2^21.
-
The value as a Float, rounded to the nearest: once, from the bits, since a Double of them can round already.
-
The whole part, toward 0, as As takes a Float's.
-
The value as a Fixed32, rounded to the nearest, halves up. One outside a Fixed32's range overflows, as Narrowed says.
-
The value in decimal, from its bits: its whole part, and the fewest digits after the point that are nearer it than any other Fixed64, so no two print alike and none prints past what it holds.
-
aplusb. Panics in a debug build when it's past what a Fixed64 holds, and wraps in a release one. -
aminusb. Panics in a debug build when it's past what a Fixed64 holds, and wraps in a release one. -
anegated. -
Whether
ais less thanb. -
atimesb. Panics in a debug build when it's past what a Fixed64 holds, and wraps in a release one. Rounded to the nearest. -
atimesb. -
atimesb. -
adivided byb. Abof 0 panics, and a quotient past what it holds does as + does. -
adivided byb.
Functions
Sqrt
function
@PanicsAtCaller Public Function Sqrt(value: Fixed32): Fixed32
The square root, rounded to the nearest. A negative value panics.
Sqrt
function
@PanicsAtCaller Public Function Sqrt(value: Fixed64): Fixed64
The square root, rounded to the nearest. A negative value panics.
Sin
function
Public Function Sin(angle: Fixed32): Fixed32
The sine of an angle in radians, the same bits everywhere.
Cos
function
Public Function Cos(angle: Fixed32): Fixed32
The cosine of an angle in radians, the same bits everywhere.
Sin
function
Public Function Sin(angle: Fixed64): Fixed64
The sine of an angle in radians, the same bits everywhere.
Cos
function
Public Function Cos(angle: Fixed64): Fixed64
The cosine of an angle in radians, the same bits everywhere.
Report an issue with this page or with FutureBlitz on GitHub.