« Back to Index

Integer precision accuracy vs float

View original Gist on GitHub

Tags: #go

Golang integer precision accuracy vs float.go

package main

import (
	"fmt"
	"time"
)

// nsToMs converts time duration to milliseconds with greater precision than
// calling .Millisecond() which would only yield integer precision.
func nsToMs(d time.Duration) float64 {
	return float64(d.Nanoseconds()) * float64(1e-6)
}

func main() {
	// Define example that demonstrates integer precision
	s := time.Now()
	time.Sleep(3 * time.Second)
	e := time.Now().Sub(s)
	fmt.Printf("%+v (%T) %+v\n\n", e, e, e.Milliseconds())

	// Define a float value and see which of the two approachs is more accurate
	u, _ := time.ParseDuration("3.7ms")

	// Approach 1.
	fmt.Printf("3.7ms parsed value in nanoseconds: %d\n", u.Nanoseconds())
	fmt.Printf("3.7ms parsed value in milliseconds (integer precision): %d\n", u.Milliseconds())

	// Approach 2.
	milliseconds := float64(u.Nanoseconds()) * float64(1e-6)
	fmt.Printf("3.7ms parsed value in milliseconds (using float): %+v\n", milliseconds)

  	// abstraction for above float precision conversion
	fmt.Printf("%+v\n", nsToMs(u))
}