currently working % operator in java. thought understood after coming across question confused. know 10 % 3 = remainder 1. question why 3 % 8 = remainder 3 thought instead equal 0 because 3 goes 8 0 times? example 2 % 8 = remainder 2 why ones remainder not 0 also? if explain why awesome! thank you!
this correct behavior, mathematically speaking.
hopefully people indulge me digression, integers mod n of integers 0 n - 1
. form multiplicative group that's important number theory , cryptography. in fact, if do
for (int = 0; < x; i++) { console.writeline(i % n); }
this give numbers 0 (n - 1) on , on again, group.
without going details, mathematical group set operation behaves more or less you'd expect "ordinary" arithmetic. example, if set forms group under multiplication, means multiplication "works" set.
if n
power of prime (e.g. 32), integers mod n form finite field, meaning behaves quite bit ordinary arithmetic.
tl;dr behavior design. it's mathematically correct well.
Comments
Post a Comment