github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/util/smallFloat.go (about) 1 package util 2 3 import ( 4 "math" 5 ) 6 7 // util/SmallFloat.java 8 9 /* 10 floatToByte(b, mantissaBits=3, zeroExponent=15) 11 smallest non-zero value = 5.820766E-10 12 largest value = 7.5161928E9 13 epsilon = 0.125 14 */ 15 func FloatToByte315(f float32) int8 { 16 bits := math.Float32bits(f) 17 smallfloat := bits >> (24 - 3) 18 if smallfloat <= ((63 - 15) << 3) { 19 if bits <= 0 { 20 return 0 21 } 22 return 1 23 } 24 if smallfloat >= ((63-15)<<3)+0x100 { 25 return -1 26 } 27 return int8(smallfloat - ((63 - 15) << 3)) 28 }