go-tools icon indicating copy to clipboard operation
go-tools copied to clipboard

SA4015: more wrong float math

Open dominikh opened this issue 5 years ago • 2 comments

Seen this original code in the wild: int(math.Ceil(float64(gap / 2))) (for var gap int) – staticcheck pointed out that the call to math.Ceil was pointless, so the code got changed to int(float64(gap / 2)), but the code very likely was supposed to read int(math.Ceil(float64(gap) / 2)).

dominikh avatar Jan 28 '20 14:01 dominikh

@dominikh math.Ceil(1.2) is 2, while int(1.2) is 1, so ceiling is not pointless if the intention is to ceil. Results are completely different. Almost followed the suggestion but realised it will introduce a bug for my case. So I'd consider this issue as bug.

DenyVeyten avatar Jul 06 '25 09:07 DenyVeyten

It is pointless when you're ceiling the result of converting an integer to a floating point number. int(math.Ceil(float64(x))) where x is int is identical to x.

dominikh avatar Jul 06 '25 14:07 dominikh