2018-03-12 16:27:01 +00:00
|
|
|
#! /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
|
|
|
|
"""
|
|
|
|
|
2018-03-17 07:15:46 +00:00
|
|
|
from multipledispatch import Dispatcher
|
2018-03-13 11:43:48 +00:00
|
|
|
from ..MO.mo import MO, MOnumber
|
2018-03-12 16:27:01 +00:00
|
|
|
from ..MO.fraction import MOFraction
|
|
|
|
from .exceptions import MinusError
|
|
|
|
|
2018-03-17 07:15:46 +00:00
|
|
|
minus_doc = """ Opposite of a MO
|
2018-03-12 16:27:01 +00:00
|
|
|
|
2018-03-17 07:15:46 +00:00
|
|
|
:param left: None
|
|
|
|
:param right: right MO
|
|
|
|
:returns: Tree or MO
|
2018-03-12 16:27:01 +00:00
|
|
|
|
2018-03-17 07:15:46 +00:00
|
|
|
"""
|
2018-03-12 16:27:01 +00:00
|
|
|
|
2018-03-17 07:15:46 +00:00
|
|
|
minus = Dispatcher("minus", doc=minus_doc)
|
2018-03-12 16:27:01 +00:00
|
|
|
|
2018-03-17 07:15:46 +00:00
|
|
|
@minus.register(type(None), MOnumber)
|
|
|
|
def monumber(left, right):
|
2018-03-12 16:27:01 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
>>> a = MOnumber(4)
|
2018-03-17 07:15:46 +00:00
|
|
|
>>> minus(None, a)
|
2018-03-12 16:27:01 +00:00
|
|
|
<MOnumber - 4>
|
|
|
|
|
|
|
|
"""
|
|
|
|
return MO.factory(- right.value)
|
|
|
|
|
2018-03-17 07:15:46 +00:00
|
|
|
@minus.register(type(None), MOFraction)
|
|
|
|
def mofraction(left, right):
|
|
|
|
""" 4 differents cases
|
2018-03-12 16:27:01 +00:00
|
|
|
|
2018-03-17 07:15:46 +00:00
|
|
|
Either fraction , numerator or denominator is negative
|
2018-03-12 16:27:01 +00:00
|
|
|
|
|
|
|
>>> a = MOFraction(6, 5)
|
2018-03-17 07:15:46 +00:00
|
|
|
>>> print(minus(None, a))
|
2018-03-12 16:27:01 +00:00
|
|
|
-
|
|
|
|
> None
|
|
|
|
> /
|
|
|
|
| > 6
|
|
|
|
| > 5
|
2018-03-17 07:15:46 +00:00
|
|
|
|
|
|
|
The fraction is negative
|
|
|
|
|
2018-03-12 16:27:01 +00:00
|
|
|
>>> a = MOFraction(6, 5, True)
|
2018-03-17 07:15:46 +00:00
|
|
|
>>> print(minus(None, a))
|
|
|
|
/
|
|
|
|
> 6
|
|
|
|
> 5
|
|
|
|
|
|
|
|
Numerator is negative
|
|
|
|
|
|
|
|
>>> a = MOFraction(-6, 5)
|
|
|
|
>>> print(minus(None, a))
|
|
|
|
/
|
|
|
|
> 6
|
|
|
|
> 5
|
|
|
|
|
|
|
|
Denominators is negative
|
|
|
|
|
|
|
|
>>> a = MOFraction(6, -5)
|
|
|
|
>>> print(minus(None, a))
|
2018-03-12 16:27:01 +00:00
|
|
|
/
|
|
|
|
> 6
|
|
|
|
> 5
|
|
|
|
"""
|
|
|
|
if right.negative:
|
|
|
|
return MOFraction(right._numerator, right._denominator)
|
|
|
|
|
|
|
|
try:
|
2018-11-13 08:14:50 +00:00
|
|
|
if right._numerator.value < 0:
|
|
|
|
return MOFraction(-right._numerator.value, right._denominator)
|
2018-03-12 16:27:01 +00:00
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
|
2018-11-13 08:14:50 +00:00
|
|
|
if right._denominator.value < 0:
|
|
|
|
return MOFraction(right._numerator, -right._denominator.value)
|
2018-03-12 16:27:01 +00:00
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return MOFraction(right._numerator, right._denominator, True)
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|