Integer division in float operation

I am porting some code from JavaScript to Go 1.11.5. Plenty of lines like:
Ma = (1 + n + (5/4)*n2 + (5/4)*n3) * (φ - φ0)

If variables are involved then Go will detect mismatched types int and float64 but with digits the code compiles silently.

(5/4)*2.0 == 2.0

In Go (5/4) will evaluate to 1. For my purposes I can easily add a decimal point (5.0/4)

(5.0/4)*2.0 == 2.5

Is there a tool that will automatically detect this mismatch ?

Nothing build into go. You could find all occurances of integers in division with the following regular expression ([^0-9.])(\d+)(\s*/) and replace it with $1$2.0$3 This is if you uses Visual Studio Code.

That will work. Thanks.

I am still worried that there are some other silent conversions that I may have missed. Maybe I will have to translate the tests too :see_no_evil:

For all the hoopla made about preventing C-style type mismatch bugs it seems strange that some still lurk in dark corners to catch the unwary.

Having tests is a good thing.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.