Skip to content

Instantly share code, notes, and snippets.

@inancgumus
Last active October 8, 2017 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inancgumus/344a78d4af7fb753e29ee5ce3da722ad to your computer and use it in GitHub Desktop.
Save inancgumus/344a78d4af7fb753e29ee5ce3da722ad to your computer and use it in GitHub Desktop.
high precision go consts
package main
import "fmt"
func main() {
// ------------------------------------------------
// LOSS OF PRECISION
// ------------------------------------------------
// const Pi lives in the ideal numbers universe which has at least 256-bit high-precision
const Pi = 3.14159265358979323846264338327950288419716939937510582097494459
// when evaluated, it'll be converted into a float64
fmt.Printf("Pi : %.50g\n", Pi)
// ------------------------------------------------
// HIGH AND LOW PRECISION DIFFERENCE
// ------------------------------------------------
const uta, utb = 0.116, 45.0 // untyped constants
const ta, tb float64 = uta, utb // typed constants
highPrecision := uta * utb
lowPrecision := ta * tb
// high precision calculation - untyped constants - ideal numbers space
fmt.Printf("high-precision: %.50f\n", highPrecision)
// low precision calculation - typed constants
fmt.Printf("low precision : %.50f\n", lowPrecision)
fmt.Printf("difference : %.50f\n", highPrecision - lowPrecision)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment