Search code examples
floating-pointgodivisionmodulo

math.Mod in Go returns integer part instead of floating-point remainder


Golang's math.Mod(10, 4) returns 2 -- ie. the integer part of division result 2.5 -- but shouldn't it be "the floating point remainder", that is, 0.5?


Solution

  • The result is correct. math.Mod returns the remainder, which really is 2 in this case. It's equivalent to the % operator, but for floating point numbers.