Last active
October 8, 2017 15:38
-
-
Save inancgumus/344a78d4af7fb753e29ee5ce3da722ad to your computer and use it in GitHub Desktop.
high precision go consts
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 "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