float16

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2023 License: MIT Imports: 5 Imported by: 5

README

float16

Go Reference test

The float16 package provides types for handling half-precision floating-point numbers.

Synopsis

import "github.com/shogo82148/float16"

func main() {
    a := float16.FromFloat64(1.0)
    b := float16.FromFloat64(2.0)

    fmt.Printf("%f + %f = %f", a.Add(b))
    fmt.Printf("%f - %f = %f", a.Sub(b))
    fmt.Printf("%f * %f = %f", a.Mul(b))
    fmt.Printf("%f / %f = %f", a.Div(b))
}

Correctness

The package passes tests generated by Berkeley TestFloat.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Float16

type Float16 uint16

Float16 represents a 16-bit floating point number.

func FMA added in v0.3.0

func FMA(x, y, z Float16) Float16

FMA returns x * y + z, computed with only one rounding. (That is, FMA returns the fused multiply-add of x, y, and z.)

func FromBits

func FromBits(b uint16) Float16

FromBits returns the floating point number corresponding the IEEE 754 binary representation b.

func FromFloat32

func FromFloat32(f float32) Float16

FromFloat32 returns the floating point number corresponding to the IEEE 754 binary representation of f.

func FromFloat64

func FromFloat64(f float64) Float16

FromFloat64 returns the floating point number corresponding to the IEEE 754 binary representation of f.

func Inf

func Inf(sign int) Float16

Inf returns positive infinity if sign >= 0, negative infinity if sign < 0.

func NaN

func NaN() Float16

NaN returns an IEEE 754 “not-a-number” value.

func Parse added in v0.5.0

func Parse(s string) (Float16, error)

func (Float16) Add

func (a Float16) Add(b Float16) Float16

Add returns the IEEE 754 binary64 sum of a and b.

func (Float16) Append added in v0.4.0

func (x Float16) Append(buf []byte, fmt byte, prec int) []byte

func (Float16) Bits

func (f Float16) Bits() uint16

Bits returns the IEEE 754 binary representation of f.

func (Float16) Compare added in v0.2.0

func (a Float16) Compare(b Float16) int

Compare compares x and y and returns:

-1 if x <  y
 0 if x == y (incl. -0 == 0, -Inf == -Inf, and +Inf == +Inf)
+1 if x >  y

a NaN is considered less than any non-NaN, and two NaNs are equal.

func (Float16) Eq added in v0.2.0

func (a Float16) Eq(b Float16) bool

Eq returns a == b. NaNs are not equal to anything, including NaN.

func (Float16) Float32

func (f Float16) Float32() float32

Float32 returns the float32 representation of f.

func (Float16) Float64

func (f Float16) Float64() float64

Float64 returns the float64 representation of f.

func (Float16) Format added in v0.4.0

func (x Float16) Format(s fmt.State, verb rune)

Format implements fmt.Formatter.

func (Float16) Ge added in v0.2.0

func (a Float16) Ge(b Float16) bool

Ge returns a >= b.

Special cases are:

Ge(x, NaN) == false
Ge(NaN, x) == false

func (Float16) Gt added in v0.2.0

func (a Float16) Gt(b Float16) bool

Gt returns a > b.

Special cases are:

Gt(x, NaN) == false
Gt(NaN, x) == false

func (Float16) IsInf

func (f Float16) IsInf(sign int) bool

IsInf reports whether f is an infinity, according to sign. If sign > 0, IsInf reports whether f is positive infinity. If sign < 0, IsInf reports whether f is negative infinity. If sign == 0, IsInf reports whether f is either infinity.

func (Float16) IsNaN

func (f Float16) IsNaN() bool

IsNaN reports whether f is an IEEE 754 “not-a-number” value.

func (Float16) Le added in v0.2.0

func (a Float16) Le(b Float16) bool

Le returns a <= b.

Special cases are:

Le(x, NaN) == false
Le(NaN, x) == false

func (Float16) Lt added in v0.2.0

func (a Float16) Lt(b Float16) bool

Lt returns a < b.

Special cases are:

Lt(NaN, x) == false
Lt(x, NaN) == false

func (Float16) Mul

func (a Float16) Mul(b Float16) Float16

Mul returns the IEEE 754 binary64 product of a and b.

func (Float16) Ne added in v0.2.0

func (a Float16) Ne(b Float16) bool

Ne returns a != b. NaNs are not equal to anything, including NaN.

func (Float16) Quo

func (a Float16) Quo(b Float16) Float16

Quo returns the IEEE 754 binary64 quotient of a and b.

func (Float16) Sqrt added in v0.4.0

func (x Float16) Sqrt() Float16

Sqrt returns the square root of x.

Special cases are:

Sqrt(+Inf) = +Inf
Sqrt(±0) = ±0
Sqrt(x < 0) = NaN
Sqrt(NaN) = NaN

func (Float16) String added in v0.4.0

func (x Float16) String() string

func (Float16) Sub

func (a Float16) Sub(b Float16) Float16

Sub returns the IEEE 754 binary64 difference of a and b.

func (Float16) Text added in v0.4.1

func (x Float16) Text(fmt byte, prec int) string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL