On Wed, Feb 27, 2013 at 5:46 PM, G B <[email protected]> wrote: > I'm not sure it matters in the end, but it looks like > Mod(Rational(15,7),1).n() works ok too. I think what's happening is that > when pi isn't in the argument list, it basically gets simplified when the > class construction calls Mod.eval but Mod.eval doesn't know how to simplify > with pi. > > I've patched this into my Mod class: > > def _eval_evalf(self,prec): > return N(mpmath.fmod(*[N(x,prec) for x in self.args]),prec)
You're passing SymPy Floats to the mpmath function, which is not really correct (though I guess it works). Why not just convert each arg to Float and use %, and leave the details up to Float.__mod__. > > It solves my immediate problem, but given how complex the evalf functions > are for everything else, I get the feeling I've done something horribly, > inexcusably naive here. Am I missing anything? > > I tried Chris' examples and Mod(1e-40,1) returns 0 with my method even > though mpmath.fmod(1e-40,1) returns 1e-40. It also returns zero if I write > it as Mod(Float(1e-40),S(1)). I get the proper results with > Mod(1e-40,1,evaluate=False).n(). > > Again, the problem appears to be in Mod.eval, but I'm not sure where. > > The other thing I noticed going through EvalfMixin.evalf is that the options > parameter isn't passed to self._eval_evalf, which means that subs parameters > won't get passed unless _eval_evalf is in the evalf_table. I don't know if > this was intentional or an oversight. I may not be clear what you're saying, but _eval_evalf only takes two arguments, self and prec. So where would subs be passed to? Aaron Meurer > > Cheers-- > Greg > > > > On Wednesday, February 27, 2013 3:40:57 PM UTC-8, Aaron Meurer wrote: >> >> On Wed, Feb 27, 2013 at 1:41 PM, Chris Smith <[email protected]> wrote: >> >> It looks like Mod doesn't implement evalf at all, and it doesn't work >> >> automatically (that only happens if the function name is the same as >> >> the mpmath function name). It should be easy. Just evaluate the >> >> arguments, and then take the mod of them. >> > >> > In general this is not going to work: >> > >> > ``` >> >>>> Mod(1e-30,3) >> > 1.00000000000000e-30 >> >>>> Mod(1e-40,3) >> > 0.0 >> > ``` >> > >> > whereas >> > >> > ``` >> >>>> 1e-40 % 3 >> > 9.9999999999999993e-41 >> > ``` >> >> Isn't that just a roundoff error. If you use Float, you get 1e-40. >> >> Aaron Meurer >> >> > >> > Something like this might work when a > b (when a < b the answer is >> > `a`): >> > >> > ``` >> >>>> a,b=pi**3,S(3) >> >>>> (a - round(a/b)*b).n() >> > 1.00627668029982 >> >>>> a.n() % b.n() >> > 1.00627668029982 >> > ``` >> > >> > -- >> > You received this message because you are subscribed to the Google >> > Groups "sympy" group. >> > To unsubscribe from this group and stop receiving emails from it, send >> > an email to [email protected]. >> > To post to this group, send email to [email protected]. >> > Visit this group at http://groups.google.com/group/sympy?hl=en. >> > For more options, visit https://groups.google.com/groups/opt_out. >> > >> > > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/sympy?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sympy?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
