$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · easy · #242

Which fix removes the bug?

sum := 7
avg := sum / 2
fmt.Println(avg) // expected 3.5
  1. Write avg := sum / 2.0
  2. Convert: float64(sum) / 2
  3. Use math.Round(sum / 2)
  4. Declare var avg float64 first
Dividing two ints truncates. 2.0 is an untyped constant that becomes an int here, so it still prints 3; converting sum to float64 does real division.
Play Problem Solving ▸