Added scientific notation and dice expressions (cheaply as operators)

Mon, 04 May 2015 02:19:21 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Mon, 04 May 2015 02:19:21 +0300
changeset 134
7316dc5f61ef
parent 133
06808909d694
child 135
ad27ab7e6fb6

Added scientific notation and dice expressions (cheaply as operators)

calc.py file | annotate | diff | comparison | revisions
--- a/calc.py	Mon May 04 00:50:18 2015 +0300
+++ b/calc.py	Mon May 04 02:19:21 2015 +0300
@@ -99,9 +99,22 @@
 def intf (func):
 	return lambda *args: do_intf (func, *args)
 
+def dice (numRolls, maxValue):
+	sumValue = 0
+
+	for i in range (0, numRolls):
+		sumValue += int (random.random() * maxValue) + 1
+
+	return sumValue
+
+def scientific (mantissa, exp):
+	return mantissa * 10 ** exp
+
 Operators = []
 OperatorData = {
-	'lneg':		{ 'symbol': '!',  'operands': 1, 'priority': 5, 'function': lambda x: not x },
+	'sci':		{ 'symbol': 'e',  'operands': 2, 'priority': 1, 'function': intf (scientific) },
+	'dice':		{ 'symbol': 'd',  'operands': 2, 'priority': 2, 'function': intf (dice) },
+	'not':		{ 'symbol': '!',  'operands': 1, 'priority': 5, 'function': lambda x: not x },
 	'compl':	{ 'symbol': '~',  'operands': 1, 'priority': 5, 'function': intf (operator.inv) },
 	'neg':		{ 'symbol': '-',  'operands': 1, 'priority': 5, 'function': lambda x: -x },
 	'pow':		{ 'symbol': '**', 'operands': 2, 'priority': 10, 'function': lambda x, y: x ** y },
@@ -287,7 +300,7 @@
 		if base != 10:
 			if not self.preferred_base:
 				self.preferred_base = base
-		
+
 			# Skip leading zeroes
 			while i < len (expr) and expr[i] == '0':
 				i += 1

mercurial