Created
August 10, 2019 20:51
-
-
Save vlad-bezden/b908df125ed0ac2cb396e4b551b4b778 to your computer and use it in GitHub Desktop.
Rounding Errors example using addition in a loop versus multiplication
This file contains hidden or 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() { | |
piggyBank := 0.0 | |
for i := 0; i < 11; i++ { | |
piggyBank += 0.1 | |
} | |
fmt.Println("Using addition 11 times", piggyBank) | |
fmt.Println("Using multiplication", 0.1*11.0) | |
} | |
/* | |
OUTPUT | |
Using addition 11 times 1.0999999999999999 | |
Using multiplication 1.1 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment