Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions types/float.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package types

import (
"fmt"
"hash/fnv"
"reflect"
"unsafe"
)

type Float64 float64
type Float32 float32

func (self Float32) Equals(other Equatable) bool {
if o, ok := other.(Float32); ok {
return self == o
} else {
return false
}
}

func (self Float32) Less(other Sortable) bool {
if o, ok := other.(Float32); ok {
return self < o
} else {
return false
}
}

func (self Float32) Hash() int {
f := float32(self)
s := &reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&f)),
Len: 4,
Cap: 4,
}
b := (*[]byte)(unsafe.Pointer(s))
h := fnv.New64a()
_, err := h.Write(*b)
if err != nil {
// should never happen...
panic(fmt.Errorf("could not write to hash %v", err))
}
return int(h.Sum64())
}

func (self Float64) Equals(other Equatable) bool {
if o, ok := other.(Float64); ok {
return self == o
} else {
return false
}
}

func (self Float64) Less(other Sortable) bool {
if o, ok := other.(Float64); ok {
return self < o
} else {
return false
}
}

func (self Float64) Hash() int {
f := float64(self)
s := &reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&f)),
Len: 8,
Cap: 8,
}
b := (*[]byte)(unsafe.Pointer(s))
h := fnv.New64a()
_, err := h.Write(*b)
if err != nil {
// should never happen...
panic(fmt.Errorf("could not write to hash %v", err))
}
return int(h.Sum64())
}