On 9 November 2011 06:17, Case Van Horsen
Currently the functions round(), math.ceil(), math.floor(), and math.trunc() all check for the existence of a special method (__round__, __ceil__, __floor__, and __trunc__). Would it be possible to enhance the math and cmath modules to check for the existence of a special method for (almost) functions? For example, math.sin(obj) would first check for obj.__sin__.
Rationale
I'm in the final stages of adding support for the MPFR (multiple-precision floating point) and MPC (multiple-precision complex) libraries to the next-generation of gmpy, currently known as gmpy2. If the special method checks are added to the math and cmath modules, then the new mpfr() and mpc() types can easily substitute for the existing float/complex types in code that uses the math or cmath module.
Thoughts?
I have been faced with a very similar situation recently. I am adding Python scripting capability to a Dynamic Geometry application called GeoGebra (www.geogebra.org). GeoGebra has its own numeric types. I have wrapped them in Python classes so that all arithmetic operations work correctly on them but it would be a big improvement if the standard analytic functions in the math module could work on them as well. So this would be a welcome addition for me (although, as GeoGebra is a Java application, I am using Jython, so I would have to wait a while to see this coming my way :). -- Arnaud