Esempio n. 1
0
    def fneg(ctx, x, **kwargs):
        """
        Negates the number *x*, giving a floating-point result, optionally
        using a custom precision and rounding mode.

        See the documentation of :func:`~mpmath.fadd` for a detailed description
        of how to specify precision and rounding.

        **Examples**

        An mpmath number is returned::

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = False
            >>> fneg(2.5)
            mpf('-2.5')
            >>> fneg(-5+2j)
            mpc(real='5.0', imag='-2.0')

        Precise control over rounding is possible::

            >>> x = fadd(2, 1e-100, exact=True)
            >>> fneg(x)
            mpf('-2.0')
            >>> fneg(x, rounding='f')
            mpf('-2.0000000000000004')

        Negating with and without roundoff::

            >>> n = 200000000000000000000001
            >>> print int(-mpf(n))
            -200000000000000016777216
            >>> print int(fneg(n))
            -200000000000000016777216
            >>> print int(fneg(n, prec=log(n,2)+1))
            -200000000000000000000001
            >>> print int(fneg(n, dps=log(n,10)+1))
            -200000000000000000000001
            >>> print int(fneg(n, prec=inf))
            -200000000000000000000001
            >>> print int(fneg(n, dps=inf))
            -200000000000000000000001
            >>> print int(fneg(n, exact=True))
            -200000000000000000000001

        """
        prec, rounding = ctx._parse_prec(kwargs)
        x = ctx.convert(x)
        if hasattr(x, '_mpf_'):
            return ctx.make_mpf(mpf_neg(x._mpf_, prec, rounding))
        if hasattr(x, '_mpc_'):
            return ctx.make_mpc(mpc_neg(x._mpc_, prec, rounding))
        raise ValueError("Arguments need to be mpf or mpc compatible numbers")
Esempio n. 2
0
    def fneg(ctx, x, **kwargs):
        """
        Negates the number *x*, giving a floating-point result, optionally
        using a custom precision and rounding mode.

        See the documentation of :func:`fadd` for a detailed description
        of how to specify precision and rounding.

        **Examples**

        An mpmath number is returned::

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = False
            >>> fneg(2.5)
            mpf('-2.5')
            >>> fneg(-5+2j)
            mpc(real='5.0', imag='-2.0')

        Precise control over rounding is possible::

            >>> x = fadd(2, 1e-100, exact=True)
            >>> fneg(x)
            mpf('-2.0')
            >>> fneg(x, rounding='f')
            mpf('-2.0000000000000004')

        Negating with and without roundoff::

            >>> n = 200000000000000000000001
            >>> print int(-mpf(n))
            -200000000000000016777216
            >>> print int(fneg(n))
            -200000000000000016777216
            >>> print int(fneg(n, prec=log(n,2)+1))
            -200000000000000000000001
            >>> print int(fneg(n, dps=log(n,10)+1))
            -200000000000000000000001
            >>> print int(fneg(n, prec=inf))
            -200000000000000000000001
            >>> print int(fneg(n, dps=inf))
            -200000000000000000000001
            >>> print int(fneg(n, exact=True))
            -200000000000000000000001

        """
        prec, rounding = ctx._parse_prec(kwargs)
        x = ctx.convert(x)
        if hasattr(x, '_mpf_'):
            return ctx.make_mpf(mpf_neg(x._mpf_, prec, rounding))
        if hasattr(x, '_mpc_'):
            return ctx.make_mpc(mpc_neg(x._mpc_, prec, rounding))
        raise ValueError("Arguments need to be mpf or mpc compatible numbers")
Esempio n. 3
0
 def __neg__(s):
     cls, new, (prec, rounding) = s._ctxdata
     v = new(cls)
     v._mpc_ = mpc_neg(s._mpc_, prec, rounding)
     return v
Esempio n. 4
0
 def __neg__(s):
     cls, new, (prec, rounding) = s._ctxdata
     v = new(cls)
     v._mpc_ = mpc_neg(s._mpc_, prec, rounding)
     return v