David Klempfner
Feb 18, 2021

Hi.

The result of x%y is the numerator, of the fraction, which is the remainder of doing x/y. 1000%7=6 because 1000/7 = 142.857142857 = 142+0.857142857 = 142+6/7.

The thought process changes with negative numbers. To understand why (-1)&7=6, have a read of the answer by Peter Vanroose in https://www.quora.com/How-does-the-modulo-operation-work-with-negative-numbers-and-why

A way of thinking, which unifies positive and negative numbers, is to continuously minus, (or add, if it's a negative number), y from (or to, if it's a negative number) x until you get the lowest positive number. In 1000 % 7 = 6 = (-1) % 7, if you continuously minus 7 from 1000, you'll eventually get to 6. If you add 7 to -1, you get 6.

I hope that makes sense :)

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

David Klempfner
David Klempfner

Written by David Klempfner

I’m a software developer who is passionate about learning how things work behind the scenes.

No responses yet

Write a response