maths 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +24 -0
- data/bin/maths +1 -1
- data/lib/maths/calc.y +16 -4
- data/lib/maths/functions.rb +21 -1
- data/lib/maths/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -56,6 +56,16 @@ maths> x = y = z = 25
|
|
56
56
|
= 25
|
57
57
|
```
|
58
58
|
|
59
|
+
There's even a special variable, `$`, that is always set to the result of
|
60
|
+
the last calculation:
|
61
|
+
|
62
|
+
```
|
63
|
+
maths> 5 + 5
|
64
|
+
= 10
|
65
|
+
maths> $
|
66
|
+
= 10
|
67
|
+
```
|
68
|
+
|
59
69
|
And of course, sometimes you'll want to close the app:
|
60
70
|
|
61
71
|
```
|
@@ -88,6 +98,20 @@ maths> 2 ^ 4
|
|
88
98
|
= 16
|
89
99
|
```
|
90
100
|
|
101
|
+
Percentages!
|
102
|
+
```
|
103
|
+
maths> 2 as a % of 4
|
104
|
+
= 50%
|
105
|
+
```
|
106
|
+
|
107
|
+
Maths also includes some constants:
|
108
|
+
|
109
|
+
Pi!
|
110
|
+
```
|
111
|
+
maths> pi
|
112
|
+
= 3.14159265358979323846264338327
|
113
|
+
```
|
114
|
+
|
91
115
|
Maths is still pretty new - I hacked the original version together on a flight, and it's still maturing. If you'd like to see something added, let me know in the [issue tracker](https://github.com/commondream/maths/issues).
|
92
116
|
|
93
117
|
## Contributing
|
data/bin/maths
CHANGED
@@ -16,7 +16,6 @@ Readline.completion_proc = Proc.new do |str|
|
|
16
16
|
# TODO: Get this working
|
17
17
|
end
|
18
18
|
|
19
|
-
|
20
19
|
prompt = "maths> "
|
21
20
|
while command = Readline.readline(prompt, true)
|
22
21
|
break if /exit/i =~ command
|
@@ -33,6 +32,7 @@ while command = Readline.readline(prompt, true)
|
|
33
32
|
result = parser.parse(command)
|
34
33
|
|
35
34
|
puts "= #{Maths::Formatter.format(result)}"
|
35
|
+
Maths::Brain.assign("$", result)
|
36
36
|
rescue ParseError
|
37
37
|
puts $!
|
38
38
|
end
|
data/lib/maths/calc.y
CHANGED
@@ -24,7 +24,6 @@ rule
|
|
24
24
|
| '-' NUMBER =UMINUS { result = -val[1] }
|
25
25
|
| VARIABLE { result = Brain.lookup(val[0])}
|
26
26
|
| NUMBER
|
27
|
-
| FLOAT
|
28
27
|
| FUNC exp { result = Maths::Functions.exec(val[0], val[1]) }
|
29
28
|
| exp PERCENTAGE_OF exp { result = Percentage.build(val[0], val[2]) }
|
30
29
|
|
@@ -42,7 +41,11 @@ module Maths
|
|
42
41
|
@q = []
|
43
42
|
until str.empty?
|
44
43
|
case str
|
44
|
+
|
45
|
+
# ignore whitespace
|
45
46
|
when /\A\s+/
|
47
|
+
|
48
|
+
# percentage handling
|
46
49
|
when /\Aas a percentage of/
|
47
50
|
@q.push [:PERCENTAGE_OF, $&]
|
48
51
|
when /\Aas a % of/
|
@@ -52,16 +55,25 @@ module Maths
|
|
52
55
|
when /\Aas % of/
|
53
56
|
@q.push [:PERCENTAGE_OF, $&]
|
54
57
|
|
58
|
+
# special terms
|
59
|
+
when /\Api/
|
60
|
+
@q.push [:NUMBER, BigDecimal.new("3.14159265358979323846264338327")]
|
61
|
+
|
62
|
+
# variables and functions
|
55
63
|
when /\A[A-Za-z]+[0-9]*/
|
56
64
|
if Maths::Functions::FUNCTIONS.has_key?($&)
|
57
65
|
@q.push [:FUNC, $&]
|
58
66
|
else
|
59
67
|
@q.push [:VARIABLE, $&]
|
60
68
|
end
|
61
|
-
when /\A
|
62
|
-
@q.push [:
|
63
|
-
|
69
|
+
when /\A\$/
|
70
|
+
@q.push [:VARIABLE, $&]
|
71
|
+
|
72
|
+
# numbers
|
73
|
+
when /\A\d+(\.\d+)?([Ee]\d+)?/
|
64
74
|
@q.push [:NUMBER, BigDecimal.new($&)]
|
75
|
+
# when /\A\d+/
|
76
|
+
# @q.push [:NUMBER, BigDecimal.new($&)]
|
65
77
|
when /\A.|\n/o
|
66
78
|
s = $&
|
67
79
|
@q.push [s, s]
|
data/lib/maths/functions.rb
CHANGED
@@ -1,7 +1,27 @@
|
|
1
1
|
module Maths
|
2
2
|
class Functions
|
3
3
|
FUNCTIONS = {
|
4
|
-
"sqrt" => Proc.new { |val| BigDecimal.new(Math.sqrt(val).to_s) }
|
4
|
+
"sqrt" => Proc.new { |val| BigDecimal.new(Math.sqrt(val).to_s) },
|
5
|
+
"cbrt" => Proc.new { |val| BigDecimal.new(Math.cbrt(val).to_s) },
|
6
|
+
"sin" => Proc.new { |val| BigDecimal.new(Math.sin(val).to_s) },
|
7
|
+
"asin" => Proc.new { |val| BigDecimal.new(Math.sin(val).to_s) },
|
8
|
+
"sinh" => Proc.new { |val| BigDecimal.new(Math.sinh(val).to_s) },
|
9
|
+
"asinh" => Proc.new { |val| BigDecimal.new(Math.asinh(val).to_s) },
|
10
|
+
"cos" => Proc.new { |val| BigDecimal.new(Math.cos(val).to_s) },
|
11
|
+
"acos" => Proc.new { |val| BigDecimal.new(Math.acos(val).to_s) },
|
12
|
+
"cosh" => Proc.new { |val| BigDecimal.new(Math.cosh(val).to_s) },
|
13
|
+
"acosh" => Proc.new { |val| BigDecimal.new(Math.acosh(val).to_s) },
|
14
|
+
"tan" => Proc.new { |val| BigDecimal.new(Math.tan(val).to_s) },
|
15
|
+
"atan" => Proc.new { |val| BigDecimal.new(Math.atan(val).to_s) },
|
16
|
+
"tanh" => Proc.new { |val| BigDecimal.new(Math.tanh(val).to_s) },
|
17
|
+
"atanh" => Proc.new { |val| BigDecimal.new(Math.atanh(val).to_s) },
|
18
|
+
"ln" => Proc.new { |val| BigDecimal.new(Math.log(val).to_s) },
|
19
|
+
"log" => Proc.new { |val| BigDecimal.new(Math.log10(val).to_s) },
|
20
|
+
"log2" => Proc.new { |val| BigDecimal.new(Math.log2(val).to_s) },
|
21
|
+
"floor" => Proc.new { |val| BigDecimal.new(val.floor.to_s) },
|
22
|
+
"ceil" => Proc.new { |val| BigDecimal.new(val.ceil.to_s) },
|
23
|
+
"ceiling" => Proc.new { |val| BigDecimal.new(val.ceil.to_s) },
|
24
|
+
"round" => Proc.new { |val| BigDecimal.new(val.round.to_s) }
|
5
25
|
}
|
6
26
|
|
7
27
|
def self.exec(function, value)
|
data/lib/maths/version.rb
CHANGED