☟☟ Upcoming C++ Best Practices – Tools online seminar, March 8th! ☟☟
Notes and code:
Previous Episode:
T-SHIRTS AVAILABLE!
► The best C++ T-Shirts anywhere!
WANT MORE JASON?
► My Training Classes:
► Follow me on twitter:
SUPPORT THE CHANNEL
► Patreon:
► Github Sponsors:
► Paypal Donation:
GET INVOLVED
► Video Idea List:
JASON’S BOOKS
► C++ Best Practices
Amazon Paperback:
Leanpub Ebook:
JASON’S PUZZLE BOOKS
► Object Lifetime Puzzlers Book 1
Amazon Paperback:
Leanpub Ebook:
► Object Lifetime Puzzlers Book 2
Amazon Paperback:
Leanpub Ebook:
► Object Lifetime Puzzlers Book 3
Leanpub Ebook:
► Copy and Reference Puzzlers Book 1
Amazon Paperback:
Leanpub Ebook:
► Copy and Reference Puzzlers Book 2
Amazon Paperback:
Leanpub Ebook:
► Copy and Reference Puzzlers Book 3
Leanpub Ebook:
► OpCode Puzzlers Book 1
Amazon Paperback:
Leanpub Ebook:
RECOMMENDED BOOKS
► Bjarne Stroustrup’s A Tour of C++ (now with C++20/23!):
AWESOME PROJECTS
► The C++ Starter Project – Gets you started with Best Practices Quickly –
► C++ Best Practices Forkable Coding Standards –
O’Reilly VIDEOS
► Inheritance and Polymorphism in C++ –
► Learning C++ Best Practices –
✅ Camiseta De Futbol Baratas Player vs Fan | camisetasfutboleses
🛒 Cómprala aquí: https://www.pidecamisetas.com/
📸 Siguenos en Instagram: https://www.instagram.com/msy_es/
📲 WhatsApp: +86 166 5930 6369
👏 ¡Muchas gracias por ver el vídeo! 👏
🔴 LIKE & SUBSCRIBE 🔴
🔴 Keywords:Camisetas ESPANYOL
__
🏷️ TAGS:
CAMISETA Real Madrid 22/23
camisetasfutboleses
camisetasfutboleses.com
CAMISETAS FUTBOL SPAIN
CAMISETAS DE FUTBOL
CAMISETAS FUTBOL Baratas
CAMISETAS DE FUTBOL 2020
CAMISETAS DE FUTBOL 2021
CAMISETAS DE FUTBOL 2022
CAMISETAS FUTBOL EQUIPOS
CAMISETAS FUTBOL SELECCIONES
CAMISETAS DE FUTBOL BARATAS
MEJORES CAMISETAS DE FUTBOL
EQUIPACIONES FUTBOL
EQUIPACIONES DE FUTBOL
Otros sitios web de nuestra empresa para que los visite:
With the c++ version, you can get the same result as python like (modulus) + (value % modulus) = index -> 4 – (-1 % 4) = 3, 4 – (-2 % 4) = 2, 4 – (-3 % 4) = 1, 4 – (-4 % 4) = 0
Python % is mathematically counter intuitive, but it suits its "range" key word usage case, while C/C++ % is mathematically correct.
Nice overview, thanks.
This ought to also tie into how integer division works in those languages because I’d like to think that they all respect the identity:
(a / b) * b + a % b = a
This was really interesting, and the python version feels suitably pythonic. Nice video!
I've always gone with (a%b+b)%b when a might be negative (and assuming b is positive). I've never looked at the assembly, but it should only be two integer instructions.
this is exactly why I always use std::abs for any modulo operator
That's a surprising number of different ways that one operation is done depending on language
Intersting result with euclidean(-8,-7).
4:21 "If we have -7 divided by 4, is the answer -3, or is it +3?"
Aren't the choices -3 and +1? You're either 3 behind one multiple, or 1 ahead of the other.
The (ambiguous) definition of Modulo (on math side) makes it complicated and its different (default-)implementation in different languages and libraries. And most language implementation docs do not have a clear statement/"warning" about it.
Great explanation! I stumbled upon that Wikipedia page before but got scared away 😅
That's the reason why you need to understand and know what the f*ck you are actually writing. The % Operator is just an operator, just like the + operator. And just like that "int a = b + c" will give you different results in C and Python with "a = b + c".
The modulo operator is just a factor space. And is commonly defined as Z/nZ. Some use N/nZ, others N/nZ. Each programming language is free to do what it wants.
I did not know they were different. Now I need to go and experiment to see where my intuition says the answer should be.
I prefer Euclidian modulo (i.e. the output for A % N is always between 0 and |N|). C++'s truncated modulo just seems wrong and I don't know why anyone would want it.
Great topic. – If you refer to the behavior in C and C++ of the remainder relating to negative operands, the older standards specified an implementation-defined behavior. Only since C99 and C++11 the behavior is well-defined.
So, if there was an implementation which calculates -5 / 3 to be -2, then -5 % 3 needs to be 1. This would be a valid implementation-defined C++03 behavior because (a / b) * b + (a % b) = a is fulfilled.
But since the newer standards enforce – 5 / 3 to be -1 (truncation toward zero), -5 % 3 must be -2. Here, also -1 * 3 + -2 = -5 is true.
Question:
Can (n-1)%n==-1? Would be useful for checking the boundaries of a matrix. This would also be mathematically correct
That's exactly why I added the `modulo_arithmetic` lint to Rust Clippy. It was a real pita to find that % behaves like this while chasing a bug in Python2Cpp migration I've been doing some time ago.
As an amateur I ran into this and in frustration I just wrote a wrap function:
int wrap(int ndx, int ndxsize)
{
while(ndx >= ndxsize)
ndx -= ndxsize;
while(ndx < 0)
ndx += ndxsize;
return ndx;
}
Seems kinda inefficient to put this in what is usually a loop but it has the advantage that it is clear and works. And since nobody is paying me for my work…
This doesn't account for the fact that within some languages it is also implementation defined… That even adds a bit more complexity to the behavior of the modulus operator.
I like and view so you get paid more