Skip to content

Instantly share code, notes, and snippets.

@zhengkai
Last active July 11, 2018 10:21
Show Gist options
  • Save zhengkai/71c092586d83680990ebe458445d1d1f to your computer and use it in GitHub Desktop.
Save zhengkai/71c092586d83680990ebe458445d1d1f to your computer and use it in GitHub Desktop.
float problem
package main
import "fmt"
func main() {
testFloat((1+0.8)/2, float64(16))
testFloat((1+0.8)/2, float64(15))
}
func testFloat(x, y float64) {
z := x + y
fmt.Println()
fmt.Println(`x =`, x)
fmt.Println(`y =`, y)
fmt.Println(`z =`, z)
fmt.Println(`z - y =`, z-y)
fmt.Println(`(z-y) > x: `, (z-y) > x)
fmt.Println(`(z-y) == x: `, (z-y) == x)
fmt.Println(`(z-y) < x: `, (z-y) < x)
}
/*
# go version
go version go1.10.3 linux/amd64
# go run test.go
x = 0.9
y = 16
z = 16.9
z - y = 0.8999999999999986
(z-y) > x: false
(z-y) == x: false
(z-y) < x: true
x = 0.9
y = 15
z = 15.9
z - y = 0.9000000000000004
(z-y) > x: true
(z-y) == x: false
(z-y) < x: false
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment