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.Sat, 12 Feb 2011 10:00:49 +0100modulo of a non-integerhttps://ask.sagemath.org/question/7940/modulo-of-a-non-integer/Does Sage support modulo of a non-integer? for example:
var("m b")
m=2*pi,
b=3.5*pi,
b.mod(m),
This returns `3.5*pi` instead of the expected `1.5*pi` or
M=2.2,
B=7.7,
B.mod(M)
for which I expect 1.1.
I can calculate it explicitly with `b-m*int(b/m)`, but I cannot find the mod version in sage. Which seems surprising.Fri, 11 Feb 2011 16:10:42 +0100https://ask.sagemath.org/question/7940/modulo-of-a-non-integer/Answer by niles for <p>Does Sage support modulo of a non-integer? for example:</p>
<pre><code>var("m b")
m=2*pi,
b=3.5*pi,
b.mod(m),
</code></pre>
<p>This returns <code>3.5*pi</code> instead of the expected <code>1.5*pi</code> or </p>
<pre><code>M=2.2,
B=7.7,
B.mod(M)
</code></pre>
<p>for which I expect 1.1.</p>
<p>I can calculate it explicitly with <code>b-m*int(b/m)</code>, but I cannot find the mod version in sage. Which seems surprising.</p>
https://ask.sagemath.org/question/7940/modulo-of-a-non-integer/?answer=12098#post-id-12098I think there is no such function in Sage right now. I did see that the [Octave `rem()`](http://www.network-theory.co.uk/docs/octave3/octave_189.html) function works for non-integers as you are expecting. There is an [interface for Octave](http://www.sagemath.org/doc/reference/sage/interfaces/octave.html) in Sage, but note that it requires installing Octave separately:
> The commands in this section only work
> if you have the optional octave
> interpreter installed and available in
> your PATH. Its not necessary to
> install any special Sage packages.
I've also seen some indication that the JavaScript modulus operator works this way for non-integers too, but I can't find a good reference for it.
If you really want this feature to be added to sage, you should ask about it on the [sage-devel](http://groups.google.com/group/sage-devel) list, and file a [Trac ticket](http://trac.sagemath.org/) for it. Then you should [write a patch](http://www.sagemath.org/doc/developer/index.html) to do what you want!Sat, 12 Feb 2011 09:03:09 +0100https://ask.sagemath.org/question/7940/modulo-of-a-non-integer/?answer=12098#post-id-12098Answer by DSM for <p>Does Sage support modulo of a non-integer? for example:</p>
<pre><code>var("m b")
m=2*pi,
b=3.5*pi,
b.mod(m),
</code></pre>
<p>This returns <code>3.5*pi</code> instead of the expected <code>1.5*pi</code> or </p>
<pre><code>M=2.2,
B=7.7,
B.mod(M)
</code></pre>
<p>for which I expect 1.1.</p>
<p>I can calculate it explicitly with <code>b-m*int(b/m)</code>, but I cannot find the mod version in sage. Which seems surprising.</p>
https://ask.sagemath.org/question/7940/modulo-of-a-non-integer/?answer=12100#post-id-12100If you only care about numerical values, you can use %:
<code><pre>sage: float(7.7) % float(2.2)
1.0999999999999996
sage: 7.7 % 2.2
1.10000000000000
</pre></code>
but these won't work for symbolic expressions, unless you play subclassing games:
<pre><code>class ModExp(Expression):
def __init__(self, value):
Expression.__init__(self, SR, value)
def __mod__(self, other):
return self-floor(self/other)*other
def mod(self, other):
return self.__mod__(other)
sage: pi = ModExp(pi)
sage: pi % (pi*3/4)
1/4*pi
sage: pi.mod(pi*3/4)
1/4*pi
</code></pre>
Sat, 12 Feb 2011 09:46:01 +0100https://ask.sagemath.org/question/7940/modulo-of-a-non-integer/?answer=12100#post-id-12100Comment by niles for <p>If you only care about numerical values, you can use %:</p>
<p><code></code></p><pre><code>sage: float(7.7) % float(2.2)
1.0999999999999996
sage: 7.7 % 2.2
1.10000000000000
</code></pre><p></p>
<p>but these won't work for symbolic expressions, unless you play subclassing games:</p>
<pre><code>class ModExp(Expression):
def __init__(self, value):
Expression.__init__(self, SR, value)
def __mod__(self, other):
return self-floor(self/other)*other
def mod(self, other):
return self.__mod__(other)
sage: pi = ModExp(pi)
sage: pi % (pi*3/4)
1/4*pi
sage: pi.mod(pi*3/4)
1/4*pi
</code></pre>
https://ask.sagemath.org/question/7940/modulo-of-a-non-integer/?comment=22129#post-id-22129well, this is a *much* better answer :)Sat, 12 Feb 2011 10:00:49 +0100https://ask.sagemath.org/question/7940/modulo-of-a-non-integer/?comment=22129#post-id-22129