ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 25 Jun 2024 18:58:06 +0200Use of 'mod' versus '%' in Sage.https://ask.sagemath.org/question/77980/use-of-mod-versus-in-sage/ I would like to better understand the use of 'mod' in Sage.
The function 'mod', as defined in sage/rings/finite_rings/integer_mod, returns mod(a, 0) = a, which is fine.
On the other hand, in sage/arith/misc, the function 'quadratic_residues' is defined as
sorted(set(ZZ((a*a) % n) for a in range(n // 2 + 1)))
Thus, if n = 0, a ZeroDivisionError is thrown. If, on the other hand, the function had been implemented with 'mod', there would be no error and 0 would be returned. This variant looks more advantageous for me.
Test:
for n in range(0, 11):
print(sorted(set(ZZ(mod((a*a), n)) for a in range(n // 2 + 1))))
Mon, 24 Jun 2024 18:25:24 +0200https://ask.sagemath.org/question/77980/use-of-mod-versus-in-sage/Comment by Peter Luschny for <p>I would like to better understand the use of 'mod' in Sage.</p>
<p>The function 'mod', as defined in sage/rings/finite_rings/integer_mod, returns mod(a, 0) = a, which is fine.</p>
<p>On the other hand, in sage/arith/misc, the function 'quadratic_residues' is defined as </p>
<pre><code> sorted(set(ZZ((a*a) % n) for a in range(n // 2 + 1)))
</code></pre>
<p>Thus, if n = 0, a ZeroDivisionError is thrown. If, on the other hand, the function had been implemented with 'mod', there would be no error and 0 would be returned. This variant looks more advantageous for me. </p>
<p>Test:</p>
<pre><code>for n in range(0, 11):
print(sorted(set(ZZ(mod((a*a), n)) for a in range(n // 2 + 1))))
</code></pre>
https://ask.sagemath.org/question/77980/use-of-mod-versus-in-sage/?comment=78001#post-id-78001Thanks Emmanuel, I might do that. Is there a general recommendation in which cases to use AskSage and in which cases sage-support?Tue, 25 Jun 2024 18:58:06 +0200https://ask.sagemath.org/question/77980/use-of-mod-versus-in-sage/?comment=78001#post-id-78001Comment by Emmanuel Charpentier for <p>I would like to better understand the use of 'mod' in Sage.</p>
<p>The function 'mod', as defined in sage/rings/finite_rings/integer_mod, returns mod(a, 0) = a, which is fine.</p>
<p>On the other hand, in sage/arith/misc, the function 'quadratic_residues' is defined as </p>
<pre><code> sorted(set(ZZ((a*a) % n) for a in range(n // 2 + 1)))
</code></pre>
<p>Thus, if n = 0, a ZeroDivisionError is thrown. If, on the other hand, the function had been implemented with 'mod', there would be no error and 0 would be returned. This variant looks more advantageous for me. </p>
<p>Test:</p>
<pre><code>for n in range(0, 11):
print(sorted(set(ZZ(mod((a*a), n)) for a in range(n // 2 + 1))))
</code></pre>
https://ask.sagemath.org/question/77980/use-of-mod-versus-in-sage/?comment=77984#post-id-77984This is a suggestion to amend Sage's behaviour. May I suggest to discuss it on [sage-support](https://groups.google.com/g/sage-support) ?Mon, 24 Jun 2024 21:04:19 +0200https://ask.sagemath.org/question/77980/use-of-mod-versus-in-sage/?comment=77984#post-id-77984