Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 819
metaleap Avatar asked May 18 '12 09:05

metaleap


People also ask

Does mod return an integer?

The MOD function takes as arguments two real number operands, and returns the remainder from integer division of the integer part of the first argument (the dividend) by the integer part of the second argument (the divisor). The value returned is an INT data type (or INT8 for remainders outside the range of INT).

Does mod return the remainder?

Modulo or Remainder Operator returns the remainder of the two numbers after division. If you are provided with two numbers, say A and B, A is the dividend and B is the divisor, A mod B is there a remainder of the division of A and B.

Does the modulus operator work with floating-point numbers?

Answer. Yes, the Python modulo operator will work with floating point numbers. This will take precedence, so will be either zero or a float.

What types does the mod (%) work on?

3) modulus operator is not just applicable to integral types e.g. byte, short, int, long but also to floating-point types like float and double. 4) You can also use the remainder operator to check if a number is even or odd, or if a year is leap year.


1 Answers

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.

like image 109
Evan Shaw Avatar answered Nov 15 '22 06:11

Evan Shaw