Beispiel #1
0
def _real_str(y, exact_form, prec_offset):
    ''' Convert a Real number to a string, with nice display.
    Returns a tuple containing the string an a bool indicating whether changes
    have been made. The function works in floats and is cached for speed. '''
    small = 5 * 10**(-5)
    x = float(y)
    if abs(x - int(x)) < small:
        return (str(int(x)), False)

    if exact_form and abs(x) > small:
        # Show pi in full
        if abs(x - float(pi())) < small: return ('pi', True)

        # Display small fractions as such
        a, b = nm.to_fraction(x)
        if b < 50:            return ('{}/{}'.format(a,b), True) if b != 1\
     else (str(a), False)

        # Display small fractional coefficients of pi in exact form
        q = x / float(pi())
        a, b = nm.to_fraction(q)
        if b <= 12:
            return ((str(a) if a != 1 else '') + 'pi'\
                    + ('/' + str(b) if b != 1 else ''), True)

        # Sort out square roots
        for n in [2, 3, 5, 6, 7, 10, 11, 13]:
            q = x / n**0.5
            a, b = nm.to_fraction(q)
            if b <= 100:
                return ((str(a) + '*' if a != 1 else '') + str(n) + '^(1/2)'\
                    + ('/' + str(b) if b != 1 else ''), True)

    # Otherwise display as a decimal
    with localcontext():
        getcontext().prec -= prec_offset
        return (str(Decimal(y).normalize()), False)
def _real_str(y, exact_form, prec_offset):
    ''' Convert a Real number to a string, with nice display.
    Returns a tuple containing the string an a bool indicating whether changes
    have been made. The function works in floats and is cached for speed. '''
    small = 5 * 10**(-5)
    x = float(y)
    if abs(x - int(x)) < small:
        return (str(int(x)), False)

    if exact_form and abs(x) > small:
        # Show pi in full
        if abs(x - float(pi())) < small: return ('pi', True)

        # Display small fractions as such
        a, b = nm.to_fraction(x)
        if b < 50: return ('{}/{}'.format(a,b), True) if b != 1\
            else (str(a), False)

        # Display small fractional coefficients of pi in exact form
        q = x / float(pi()); a, b = nm.to_fraction(q);
        if b <= 12:
            return ((str(a) if a != 1 else '') + 'pi'\
                    + ('/' + str(b) if b != 1 else ''), True)

        # Sort out square roots
        for n in [2,3,5,6,7,10,11,13]:
            q = x / n ** 0.5
            a, b = nm.to_fraction(q)
            if b <= 100:
                return ((str(a) + '*' if a != 1 else '') + str(n) + '^(1/2)'\
                    + ('/' + str(b) if b != 1 else ''), True)

    # Otherwise display as a decimal
    with localcontext():
        getcontext().prec -= prec_offset
        return (str(Decimal(y).normalize()), False)
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# C1000 Intelligent Calculator.  If not, see <http://www.gnu.org/licenses/>.

# Third party modules
from dmath import e, pi

# Project modules
from .core import handle_type as ht
from .numerical_methods import romberg_integral

# Calculate constants
e = ht(e())
pi = ht(pi())

def factorial(x):
    ''' An iterative factorial function. '''
    if x < 0 or not isinstance(x, int):
        raise ValueError('The factorial of negative numbers in not defined')
    ans = x.__class__(1)
    while x > 0:
        ans *= x
        x -= 1
    return ans
    # return x * factorial(x - 1) if x > 0 else 1

def nCr(n, r):
    ''' n combinations of r. '''
    return factorial(n) / ( factorial(r) * factorial(n - r) )
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# C1000 Intelligent Calculator.  If not, see <http://www.gnu.org/licenses/>.

# Third party modules
from dmath import e, pi

# Project modules
from .core import handle_type as ht
from .numerical_methods import romberg_integral

# Calculate constants
e = ht(e())
pi = ht(pi())


def factorial(x):
    ''' An iterative factorial function. '''
    if x < 0 or not isinstance(x, int):
        raise ValueError('The factorial of negative numbers in not defined')
    ans = x.__class__(1)
    while x > 0:
        ans *= x
        x -= 1
    return ans
    # return x * factorial(x - 1) if x > 0 else 1


def nCr(n, r):