Easiest way to get the machine epsilon in Go

It's not defined, I found the issue resolved as "working as intended" on the issue tracker:

https://code.google.com/p/go/issues/detail?id=966

The suggestion seems to be to use math.Nextafter to derive the value if you need it.

Specifically, the formula is math.Nextafter(1.0,2.0)-1.0 (the second argument can be any number greater than 1.0).


Equation to use

The above works for any binary floating point type (e.g. the Go types you are referring to.)

package main

import "fmt"

func main() {
    f32 := float32(7.)/3 - float32(4.)/3 - float32(1.)
    fmt.Println(f32)

    f64 := float64(7.)/3 - float64(4.)/3 - float64(1.)
    fmt.Println(f64)
}

gives:

-1.1920929e-07
2.220446049250313e-16

taking the absolute (unsigned) value of f32 yields the correct machine ε.

Edit: As mentioned below in a comment from SGJ, this would not work on decimal floating point types, but as far as I'm aware they have not been implemented yet in Golang.