Sometimes it is useful to run a math problem with multiple inputs. The goal of this challenge is to make a program that eases this task.
Number-generating expressions
You must support 3 types of expression:
- Single number generator: Very simple, just a literal number
- Multi-number generator: A tad more complicated. Thees are surrounded by square brackets (
[]
). Numbers are comma (,
) separated in the expression. Example[-1,2,3.26]
. - Range generator: This one is surrounded by curly braces (
{}
). It will have 3 numbers separated by a comma. The format of this expression is{start,stop,step}
.start
andstop
are inclusive.
Rules for evaluation
- You must support the order of operations. (https://en.wikipedia.org/wiki/Order_of_operations#Definition)
- You don't need to support parenthesis.
- Any number of spaces may occur in the expression.
- You must support floating point numbers (whatever precision your language defaults to is fine).
- Division by
0
results inNaN
(not a number).
Your program must support multiplication (*
), division (/
), addition (+
) and subtraction (-
).
Output
Each line of output is one of the combinations of the generators. The format is the expression (with the real numbers substituted into it) followed by a equals sign (=
) and the result of evaluation. All combinations of the generators must be represented in the output.
Examples
(>>>
denotes input)
>>>3 * [3,2]
3 * 3 = 9
3 * 2 = 6
>>>{1,2,3}
1 = 1 <-- this is because 1 + 3 > the end
>>>{0,2,1} + {0,1,1}
0 + 0 = 0
1 + 0 = 1
2 + 0 = 2
0 + 1 = 1
1 + 1 = 2
2 + 1 = 3
>>>6/[2,3]
6/2 = 3
6/3 = 2
>>>{1.5,2.5,0.5}
1.5 = 1.5
2 = 2
2.5 = 2.5
>>>3-{6,5,-1}
3-6 = -3
3-5 = -2
>>>5/{-1,1,1}
5/-1 = -5
5/0 = NaN
5/1 = 5
>>>4.4 / [1,2.2] + {0,2,1}
4.4 / 1 + 0 = 4.4
4.4 / 1 + 1 = 5.4
4.4 / 1 + 2 = 6.4
4.4 / 2.2 + 0 = 2
4.4 / 2.2 + 1 = 3
4.4 / 2.2 + 2 = 4
>>> [1,2] / 0 + 5
1 / 0 + 5 = NaN
2 / 0 + 5 = NaN
The program needs to be short so I can memorizes it and use it anywhere.
Thanks to @PeterTaylor and @geokavel for helping me with this post in the sandbox
x/0
result in instant evaluation to NaN, or do I have to treat NaN as a value? \$\endgroup\$