C | Python> Negative value modulo (%) operation> C: Returns minus | Python: Returns plus

Execute processing in Python at regular intervals I thought that the following modulo (%) operation would return a negative value in the implementation of.

next_time = ((base_time - time.time()) % interval) or interval

C

https://ideone.com/cgMjRc

#include <stdio.h>

int main(void) {
	int val = -3;
	printf("%d\n", val % 5);
	return 0;
}

run


-3

Python 3.5

https://ideone.com/CMgBPa

val=-3
print(val % 5)

run


2

Remarks

Those who use Python a lot may know it.

https://docs.python.org/3/reference/expressions.html The red letters below are colored for emphasis.

The % (modulo) operator yields the remainder from the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the ZeroDivisionError exception. The arguments may be floating point numbers, e.g., 3.14%0.7 equals 0.34 (since 3.14 equals 4*0.7 + 0.34.) The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand

Recommended Posts

C | Python> Negative value modulo (%) operation> C: Returns minus | Python: Returns plus
python Boolean operation does not return a Boolean value