Documentation ¶
Overview ¶
Package vpnumber handles fixed point numbers, on 32 or 64 bits. It comes with basic conversions and mathematical operations.
Index ¶
- Constants
- func F32Div(f1 float32, f2 float32) float32
- func F32IsSimilar(f1 float32, f2 float32) bool
- func F32Round(f float32) float32
- func F64Div(f1 float64, f2 float64) float64
- func F64IsSimilar(f1 float64, f2 float64) bool
- func F64Round(f float64) float64
- func X32Exponent(x X32) int
- func X32IsSimilar(x1 X32, x2 X32) bool
- func X32ToF32(x X32) float32
- func X32ToF64(x X32) float64
- func X32ToI32(x X32) int32
- func X32ToI64(x X32) int64
- func X64Exponent(x X64) int
- func X64IsSimilar(x1 X64, x2 X64) bool
- func X64ToF32(x X64) float32
- func X64ToF64(x X64) float64
- func X64ToI32(x X64) int32
- func X64ToI64(x X64) int64
- type X32
- func F32ToX32(f float32) X32
- func F64ToX32(f float64) X32
- func I32ToX32(i int32) X32
- func I64ToX32(i int64) X32
- func X32Abs(x X32) X32
- func X32Ceil(x X32) X32
- func X32Div(x1 X32, x2 X32) X32
- func X32Divp(x1 X32, x2 X32) X32
- func X32Floor(x X32) X32
- func X32Mantis(x X32) X32
- func X32Max(x1 X32, x2 X32) X32
- func X32Min(x1 X32, x2 X32) X32
- func X32Mul(x1 X32, x2 X32) X32
- func X32Muln(x X32, xn ...X32) X32
- func X32Mulp(x1 X32, x2 X32) X32
- func X32Round(x X32) X32
- func X64ToX32(x X64) X32
- type X64
- func F32ToX64(f float32) X64
- func F64ToX64(f float64) X64
- func I32ToX64(i int32) X64
- func I64ToX64(i int64) X64
- func X32ToX64(x X32) X64
- func X64Abs(x X64) X64
- func X64Ceil(x X64) X64
- func X64Div(x1 X64, x2 X64) X64
- func X64Divp(x1 X64, x2 X64) X64
- func X64Floor(x X64) X64
- func X64Mantis(x X64) X64
- func X64Max(x1 X64, x2 X64) X64
- func X64Min(x1 X64, x2 X64) X64
- func X64Mul(x1 X64, x2 X64) X64
- func X64Muln(x X64, xn ...X64) X64
- func X64Mulp(x1 X64, x2 X64) X64
- func X64Round(x X64) X64
Constants ¶
const F32Const0 float32 = 0.0
F32Const0 contains 0 as a float32.
const F32Const1 float32 = 1.0
F32Const1 contains 1 as a float32.
const F64Const0 float64 = 0.0
F64Const0 contains 0 as a float64.
const F64Const1 float64 = 1.0
F64Const1 contains 1 as a float64.
const I32Const0 int32 = 0
I32Const0 contains 0 as an int32.
const I32Const1 int32 = 1
I32Const1 contains 1 as an int32.
const I64Const0 int64 = 0
I64Const0 contains 0 as an int64.
const I64Const1 int64 = 1
I64Const1 contains 1 as an int64.
const PackageCopyright = "Copyright (C) 2015, 2016 Christian Mauduit <ufoot@ufoot.org>" // PackageCopyright set by version.sh
PackageCopyright contains a short copyright notice.
const PackageEmail = "ufoot@ufoot.org" // PackageEmail set by version.sh
PackageEmail contains a contact email for the package.
const PackageLicense = "GNU GPL v3" // PackageLicense set by version.sh
PackageLicense contains a short license information.
const PackageName = "Vapor Toolkit" // PackageName set by version.sh
PackageName contains a readable name of the package, suitable for display.
const PackageTarname = "vapor" // PackageTarname set by version.sh
PackageTarname contains a short name of the package, suitable for a filename.
const PackageURL = "https://github.com/ufoot/vapor" // PackageURL set by version.sh
PackageURL contains the address of the project homepage.
const VersionMajor = 0 // VersionMajor set by version.sh
VersionMajor is the project major version.
const VersionMinor = 3 // VersionMinor set by version.sh
VersionMinor is the project minor version.
const VersionStamp = "c6a4298" // VersionStamp set by version.sh
VersionStamp is the project stamp, possibly changes for each build.
const X32Shift = 16
X32Shift tells how many bits must be shifted to convert a fixed point number on 32 bits to its int32 corresponding value.
const X64Shift = 32
X64Shift tells how many bits must be shifted to convert a fixed point number on 64 bits to its int64 corresponding value.
Variables ¶
This section is empty.
Functions ¶
func F32Div ¶
F32Div does a simple division, without raising any error on division by zero. Instead it will divide by the smallest value available, the results will probably be inconsistent but at least no panic around, program flow is not interrupted.
func F32IsSimilar ¶
F32IsSimilar returns true if args are approximatively the same. This is a workarround to ignore rounding errors.
func F32Round ¶
F32Round rounds to the nearest integer, while still returning a floating point value.
func F64Div ¶
F64Div does a simple division, without raising any error on division by zero. Instead it will divide by the smallest value available, the results will probably be inconsistent but at least no panic around, program flow is not interrupted.
func F64IsSimilar ¶
F64IsSimilar returns true if args are approximatively the same. This is a workarround to ignore rounding errors.
func F64Round ¶
F64Round rounds to the nearest integer, while still returning a floating point value.
func X32Exponent ¶
X32Exponent returns the exponent of a fixed point number on 32 bits. This means a logical value of 1 has an exponent of 0, and the smallest strictly positive integer has an exponent of -16. 0 returns 0.
func X32IsSimilar ¶
X32IsSimilar returns true if args are approximatively the same. This is a workarround to ignore rounding errors.
func X64Exponent ¶
X64Exponent returns the exponent of a fixed point number on 64 bits. This means a logical value of 1 has an exponent of 0, and the smallest strictly positive integer has an exponent of -32. 0 returns 0.
func X64IsSimilar ¶
X64IsSimilar returns true if args are approximatively the same. This is a workarround to ignore rounding errors.
Types ¶
type X32 ¶
type X32 int32
X32 is a fixed point number on 32 bits. The idea is to arbitrary consider that 2^16 is 1, the 16 strongest bits being the integer part, and the 16 weakest bits being the fractionnal part. This might sound useless as all modern systems do have floats but in some cases it's convenient to have accurate integer calculations representing decimal numbers.
const X32Const0 X32 = 0
X32Const0 contains 0 as a fixed point number on 32 bits.
const X32Const1 X32 = 0x10000
X32Const1 contains 1 as a fixed point number on 32 bits.
func X32Ceil ¶
X32Ceil rounds a fixed point number on 32 bits to the nearest integer value just above. Casted as an integer the result has the 16 lowest bits set to 0.
func X32Div ¶
X32Div divides 2 fixed point numbers on 32 bits. This is not equivalent to the standard * operator as values need be shifted so that they do not get artificially big. Does not raise any error on division by zero. Instead it will divide by the smallest value available, the results will probably be inconsistent but at least no panic around, program flow is not interrupted. Beware of rounding errors, might be very approximative on limit cases.
func X32Divp ¶
X32Divp divides 2 fixed point numbers on 32 bits. This is not equivalent to the standard * operator as values need be shifted so that they do not get artificially big. Does not raise any error on division by zero. Instead it will divide by the smallest value available, the results will probably be inconsistent but at least no panic around, program flow is not interrupted. Internally, this function uses 64 bits integers so that results have minimal rounding errors.
func X32Floor ¶
X32Floor rounds a fixed point number on 32 bits to the integer value just below. Casted as an integer the result has the 16 lowest bits set to 0.
func X32Mantis ¶
X32Mantis returns the mantis of a fixed point number on 32 bits. Note that on numbers greater than 2 (exponent greater than 1) there is a loss of information as bits are shifted to the right.
func X32Mul ¶
X32Mul multiplicates 2 fixed point numbers on 32 bits. This is not equivalent to the standard * operator as values need be shifted so that they do not get artificially big. Beware of rounding errors, might be very approximative on limit cases.
func X32Muln ¶
X32Muln multiplicates several fixed point numbers on 32 bits. Calls the 2 args mul function recursively. Beware of rounding errors, might be very approximative on limit cases.
func X32Mulp ¶
X32Mulp multiplicates 2 fixed point numbers on 32 bits. This is not equivalent to the standard * operator as values need be shifted so that they do not get artificially big. Internally, this function uses 64 bits integers so that results have minimal rounding errors.
func X32Round ¶
X32Round rounds a fixed point number on 32 bits to the nearest integer value. Casted as an integer the result has the 16 lowest bits set to 0.
func X64ToX32 ¶
X64ToX32 converts a fixed point number on 64 bits to a fixed point number on 32 bits.
func (*X32) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*X32) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
type X64 ¶
type X64 int64
X64 is as fixed point number on 64 bits, the idea is to arbitrary consider that 2^32 is 1, the 32 strongest bits being the integer part, and the 32 weakest bits being the fractionnal part. This might sound useless as all modern systems do have floats but in some cases it's convenient to have accurate integer calculations representing decimal numbers.
const X64Const0 X64 = 0
X64Const0 contains 0 as a fixed point number on 64 bits.
const X64Const1 X64 = 0x100000000
X64Const1 contains 1 as a fixed point number on 64 bits.
func X32ToX64 ¶
X32ToX64 converts a fixed point number on 32 bits to a fixed point number on 64 bits.
func X64Ceil ¶
X64Ceil rounds a fixed point number on 64 bits to the nearest integer value just above. Casted as an integer the result has the 32 lowest bits set to 0.
func X64Div ¶
X64Div divides 2 fixed point numbers on 64 bits. This is not equivalent to the standard * operator as values need be shifted so that they do not get artificially big. Does not raise any error on division by zero. Instead it will divide by the smallest value available, the results will probably be inconsistent but at least no panic around, program flow is not interrupted. Beware of rounding errors, might be very approximative on limit cases.
func X64Divp ¶
X64Divp divides 2 fixed point numbers on 64 bits. This is not equivalent to the standard * operator as values need be shifted so that they do not get artificially big. Does not raise any error on division by zero. Instead it will divide by the smallest value available, the results will probably be inconsistent but at least no panic around, program flow is not interrupted. Internally, this function uses 128 bits integers so that results have minimal rounding errors. As a consequence, it is quite slow.
func X64Floor ¶
X64Floor rounds a fixed point number on 64 bits to the integer value just below. Casted as an integer the result has the 32 lowest bits set to 0.
func X64Mantis ¶
X64Mantis returns the mantis of a fixed point number on 64 bits. Note that on numbers greater than 2 (exponent greater than 1) there is a loss of information as bits are shifted to the right.
func X64Mul ¶
X64Mul multiplicates 2 fixed point numbers on 64 bits. This is not equivalent to the standard * operator as values need be shifted so that they do not get artificially big. Beware of rounding errors, might be very approximative on limit cases.
func X64Muln ¶
X64Muln multiplicates several fixed point numbers on 64 bits. Calls the 2 args mul function recursively. Beware of rounding errors, might be very approximative on limit cases.
func X64Mulp ¶
X64Mulp multiplicates 2 fixed point numbers on 64 bits. This is not equivalent to the standard * operator as values need be shifted so that they do not get artificially big. Internally, this function uses 128 bits integers so that results have minimal rounding errors. As a consequence, it is quite slow.
func X64Round ¶
X64Round rounds a fixed point number on 64 bits to the nearest integer value. Casted as an integer the result has the 32 lowest bits set to 0.
func (*X64) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*X64) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.