Spot the bug, try one: Percent change between two values
The task given to the agent
Return the percent change from prev to next, rounded to one decimal; false if prev is 0 or either value isn't finite.
Write PercentChange(prev, next), which returns the change from prev to next as a percentage of the size of prev (its absolute value) and true: 200 to 250 is 25, 200 to 150 is -25, and -50 to -25 is 50. Round the result to the nearest tenth, halves away from zero, so 3 to 4 is 33.3 and 3 to 2 is -33.3. When prev is 0, or either value is NaN or an infinity, it returns 0 and false instead.
The code it wrote
One of these lines is wrong.
func PercentChange(prev, next float64) (float64, bool) { if !isFinite(prev) || !isFinite(next) || prev == 0 { return 0, false } pct := (next - prev) / math.Abs(prev) * 100 return math.Round(pct*10) / 10, true}func isFinite(x float64) bool { return !math.IsNaN(x) && math.IsInf(x, 0)}