Skip to content

Commit

Permalink
Merge pull request #25 from MiguelGuthridge/fix-leading-negative-equals
Browse files Browse the repository at this point in the history
Fix leading negative equals
  • Loading branch information
MaddyGuthridge authored Jul 10, 2021
2 parents f35e4e1 + 6652f6e commit 9a3d62d
Show file tree
Hide file tree
Showing 16 changed files with 266 additions and 74 deletions.
1 change: 1 addition & 0 deletions equator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def printOutput(out):
# No arguments, enter interpreter mode
if len(sys.argv) == 1:
print(f"{consts.NAME} (v{consts.VERSION})")
print(f"by {consts.AUTHOR}")
print("Interpreter Mode")
print("Press Ctrl+C to quit")
try:
Expand Down
8 changes: 5 additions & 3 deletions lib/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import decimal

NAME = "Equator"
VERSION = "0.5.0"
VERSION = "0.5.1"
AUTHOR = "Miguel Guthridge"

# Get 15 decimal places of precision - the max given by sympy
MAX_PRECISION = 14
FRACTION_DENOM_LIMITER = 1_000_000_000

# The minimum of abs(log_10(d)) for exponent to be presented using
# exponent notation
# exponent notation by default
MIN_EXP_LOG = 9

# Operators used to split string
Expand All @@ -27,7 +28,8 @@

NEGATE = "neg"

CONSTANTS = {
# Numeric constants
NUM_CONSTANTS = {
"pi": decimal.Decimal(math.pi),
"e": decimal.Decimal(math.e),
"oo": decimal.Decimal("inf"),
Expand Down
59 changes: 54 additions & 5 deletions lib/operation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"""Contains functions for doing operations on things.
Mostly used inside the evaluate functions of segments and the like
Author: Miguel Guthridge (hdsq@outlook.com.au)
"""

import math
import sympy as sym
from decimal import Decimal
Expand All @@ -8,7 +14,15 @@
FUNCTION_OPERATOR_PRECEDENCE = 10
NO_OPERATION_PRECEDENCE = 10

def operatorPrecedence(op: str):
def operatorPrecedence(op: str) -> int:
"""Returns an int representing the precedence of an operation
Args:
op (str): operator
Returns:
int: precedence
"""
if op in ['^']: return 3
elif op in ['*', '/']: return 2
elif op in ['+', '-']: return 1
Expand Down Expand Up @@ -49,6 +63,21 @@ def zeroRound(num):
return num

def doOperation(operator: str, a, b):
"""Function for handling the logic of an operation
In the future we may move to a method where operation token types implement
their own operate function, to remove nasty things like this
Args:
operator (str): operation to do
a (Operatable): left
b (Operatable): right
Raises:
ValueError: unrecognised operation (something is horribly wrong)
Returns:
Operatable: result
"""
a = conditionalDecimal(a)
b = conditionalDecimal(b)
if operator == '^':
Expand All @@ -68,6 +97,17 @@ def doOperation(operator: str, a, b):
return res

def doFunction(func: str, a):
"""Function for handling logic of evaluating functions.
In the future this may be moved into subclasses for each function, in the
hope of tidying things up
Args:
func (str): function to do
a (Operatable): thing to operate on
Returns:
Operatable: result
"""
if func == "sqrt":
return sym.sqrt(a)
elif func == "sin":
Expand All @@ -85,9 +125,9 @@ def doFunction(func: str, a):
elif func == "abs":
return sym.Abs(a)
elif func == "deg":
return a * 180 / consts.CONSTANTS["pi"]
return a * 180 / consts.NUM_CONSTANTS["pi"]
elif func == "rad":
return a / 180 * consts.CONSTANTS["pi"]
return a / 180 * consts.NUM_CONSTANTS["pi"]
elif func == consts.NEGATE:
return -a
elif func == "exp":
Expand All @@ -101,8 +141,17 @@ def doFunction(func: str, a):
return sym.log(a, base)

def getConstant(const: str):
if const in consts.CONSTANTS:
return str(consts.CONSTANTS[const])
"""Returns the representation of a constant as a stringified decimal
r the original string if it isn't a constant
Args:
const (str): potential constant to replace
Returns:
str: representation of constant if applicable otherwise original str
"""
if const in consts.NUM_CONSTANTS:
return str(consts.NUM_CONSTANTS[const])
else:
return const

Expand Down
2 changes: 1 addition & 1 deletion lib/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def parseToken(word: str, unwrap_symbols=True):
return tokens.Operator(word)
else:
# Parse symbols and constants
if word in consts.CONSTANTS:
if word in consts.NUM_CONSTANTS:
return tokens.Constant(word)
else:
return tokens.Symbol(word)
Expand Down
Loading

0 comments on commit 9a3d62d

Please sign in to comment.