Minus MO
This commit is contained in:
parent
98381f4512
commit
25f791bd81
@ -16,6 +16,9 @@ class ComputeError(Exception):
|
||||
class AddError(ComputeError):
|
||||
pass
|
||||
|
||||
class MinusError(ComputeError):
|
||||
pass
|
||||
|
||||
class MultiplyError(ComputeError):
|
||||
pass
|
||||
|
||||
|
102
mapytex/calculus/core/compute/minus.py
Normal file
102
mapytex/calculus/core/compute/minus.py
Normal file
@ -0,0 +1,102 @@
|
||||
#! /usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# vim:fenc=utf-8
|
||||
#
|
||||
# Copyright © 2017 lafrite <lafrite@Poivre>
|
||||
#
|
||||
# Distributed under terms of the MIT license.
|
||||
|
||||
"""
|
||||
Minus MO: take the opposit
|
||||
"""
|
||||
|
||||
from ..tree import Tree
|
||||
from ..operator import OPERATORS
|
||||
from ..MO.mo import MO, MOnumber, MOstr
|
||||
from ..MO.fraction import MOFraction
|
||||
from .exceptions import MinusError
|
||||
from .arithmetic import lcm
|
||||
|
||||
|
||||
def minus(left, right):
|
||||
""" Perform the minusition of left and right
|
||||
|
||||
:param left: left MO
|
||||
:param right: right MO
|
||||
:returns: Tree or MO
|
||||
|
||||
>>> a = MOnumber(4)
|
||||
>>> minus(None, a)
|
||||
<MOnumber - 4>
|
||||
>>> a = MOnumber(-4)
|
||||
>>> minus(None, a)
|
||||
<MOnumber 4>
|
||||
>>> b = MOnumber(0)
|
||||
>>> minus(None, b)
|
||||
<MOnumber 0>
|
||||
|
||||
"""
|
||||
if not left is None:
|
||||
raise MinusError(f"'-' is a 1 arity operator, left should be None (got {left})")
|
||||
|
||||
return MINUSFUNCTIONS[type(right)](right)
|
||||
|
||||
def monumber(right):
|
||||
""" Minusing monumber
|
||||
|
||||
:param right: right MOnumber
|
||||
:returns: MONumber
|
||||
|
||||
>>> a = MOnumber(4)
|
||||
>>> monumber(a)
|
||||
<MOnumber - 4>
|
||||
|
||||
"""
|
||||
return MO.factory(- right.value)
|
||||
|
||||
def mofraction(right):
|
||||
""" Minusing a mofraction
|
||||
|
||||
:param right: a mofraction
|
||||
:returns: Tree with the number converted into a mofraction
|
||||
|
||||
>>> a = MOFraction(6, 5)
|
||||
>>> print(mofraction(a))
|
||||
-
|
||||
> None
|
||||
> /
|
||||
| > 6
|
||||
| > 5
|
||||
>>> a = MOFraction(6, 5, True)
|
||||
>>> print(mofraction(a))
|
||||
/
|
||||
> 6
|
||||
> 5
|
||||
"""
|
||||
if right.negative:
|
||||
return MOFraction(right._numerator, right._denominator)
|
||||
|
||||
try:
|
||||
if right._numerator < 0:
|
||||
return MOFraction(-right._numerator, right._denominator)
|
||||
except TypeError:
|
||||
pass
|
||||
try:
|
||||
|
||||
if right._denominator < 0:
|
||||
return MOFraction(right._numerator, -right._denominator)
|
||||
except TypeError:
|
||||
pass
|
||||
|
||||
return MOFraction(right._numerator, right._denominator, True)
|
||||
|
||||
# TODO: Faire un décorateur pour un enregistrement automatique |dim. mars 11 18:24:32 EAT 2018
|
||||
MINUSFUNCTIONS = {
|
||||
MOnumber: monumber,
|
||||
MOFraction: mofraction,
|
||||
}
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
# cursor: 16 del
|
Loading…
Reference in New Issue
Block a user