V: Negative modulo is computing wrong

Created on 16 Jul 2019  路  6Comments  路  Source: vlang/v

V version: 0.1.11
OS: Parrot/Debian

What did you do?
Tried to use modulo to stay in positive boundaries f.e. for arrays
a := -5 %3

What did you expect to see?
a := -5 %3
print('$a')

1

What did you see instead?
a := -5 %3
print('$a')

-2

I guess it just computed the number as it were positive.

Bug

Most helpful comment

Since C99, '%' clearly means 'remainder', not 'modulo'.

-5 / 3 yields -1, because division is truncated towards zero, not towards the smaller integer.

-5 % 3 yields -2, because (-5) - (3*(-1)) = -2

In Python, '%' means 'modulo'.

All 6 comments

Interesting, running it with C/Go returns -2, running it with Python returns 1.

So I guess different languages have different ways to handle negative modulos.

Since C99, '%' clearly means 'remainder', not 'modulo'.

-5 / 3 yields -1, because division is truncated towards zero, not towards the smaller integer.

-5 % 3 yields -2, because (-5) - (3*(-1)) = -2

In Python, '%' means 'modulo'.

2019-07-16-150820_484x80_scrot

Ah, yes, for modulo there is a library function called 'modf()' in C.

V's behavior will be the same as in Go and C.

math.modf will be added once functions can return multiple values.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shouji-kazuo picture shouji-kazuo  路  3Comments

XVilka picture XVilka  路  3Comments

penguindark picture penguindark  路  3Comments

choleraehyq picture choleraehyq  路  3Comments

vtereshkov picture vtereshkov  路  3Comments