Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
Created August 10, 2019 20:35
Show Gist options
  • Save vlad-bezden/79f05513e8a2899a56e23692920f159d to your computer and use it in GitHub Desktop.
Save vlad-bezden/79f05513e8a2899a56e23692920f159d to your computer and use it in GitHub Desktop.
To minimize rounding errors, perform multiplication before division. The result tends to be more accurate that way.
package main
import "fmt"
func main() {
fmt.Println("1. Division First")
celsius := 21.0
fmt.Print((celsius/5.0*9.0)+32, "° F\n")
fmt.Print((9.0/5.0*celsius)+32, "° F\n\n")
fmt.Println("2. Multiplication First")
fahrenheit := (celsius * 9.0 / 5.0) + 32.0
fmt.Print(fahrenheit, "° F")
}
/*
OUTPUT
1. Division First
69.80000000000001° F
69.80000000000001° F
2. Multiplication First
69.8° F
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment