Last active
August 29, 2015 14:19
-
-
Save peculater/1d81fe0b30898ed02c2d to your computer and use it in GitHub Desktop.
Test byte comparison of float64
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "encoding/binary" | |
import "math/rand" | |
import "bytes" | |
import "time" | |
import "fmt" | |
func main() { | |
var x float64 | |
var y float64 | |
rand.Seed(time.Now().Unix()) | |
for i := 0; i < 500; i++ { | |
x = rand.Float64() * float64(rand.Intn(1000)) | |
y = rand.Float64() * float64(rand.Intn(1000)) | |
var floatresult int | |
if x < y { floatresult = -1 } | |
if x > y { floatresult = 1 } | |
if x == y { floatresult = 0 } | |
bytes1 := new(bytes.Buffer) | |
binary.Write(bytes1, binary.BigEndian, x) | |
bytearray1 := bytes1.Bytes() | |
bytes2 := new(bytes.Buffer) | |
binary.Write(bytes2, binary.BigEndian, y) | |
bytearray2 := bytes2.Bytes() | |
var result int | |
result = bytes.Compare(bytearray1, bytearray2) | |
fmt.Print(x) | |
fmt.Print(" ") | |
fmt.Println(y) | |
if floatresult == result { | |
fmt.Println("OK") | |
}else{ | |
fmt.Println(bytearray1) | |
fmt.Println(bytearray2) | |
fmt.Print("Float comparison ") | |
fmt.Print(floatresult) | |
fmt.Print(" == ") | |
fmt.Print(result) | |
fmt.Println(" Bytearry comparison") | |
fmt.Println("ERROR") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment