maths 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +25 -1
- data/bin/maths +5 -1
- data/lib/maths/brain.rb +7 -1
- data/lib/maths/calc.y +9 -2
- data/lib/maths/functions.rb +11 -0
- data/lib/maths/version.rb +2 -2
- data/lib/maths.rb +2 -1
- metadata +2 -1
data/README.md
CHANGED
@@ -35,7 +35,7 @@ maths> 20 * 3
|
|
35
35
|
maths> 8 - 4
|
36
36
|
= 4
|
37
37
|
maths> 4.4 + 3.2
|
38
|
-
= 7.
|
38
|
+
= 7.6
|
39
39
|
```
|
40
40
|
|
41
41
|
Maths also supports assigning variables:
|
@@ -64,6 +64,30 @@ maths> exit
|
|
64
64
|
|
65
65
|
Variables persist in maths between runs of the command, so feel free to exit and reopen maths all you want - those variables will stick around between executions.
|
66
66
|
|
67
|
+
## Tell Us More, Bob
|
68
|
+
|
69
|
+
Maths can do all sorts of great math!
|
70
|
+
|
71
|
+
Absolute Values!
|
72
|
+
|
73
|
+
```
|
74
|
+
maths> |-5|
|
75
|
+
= 5
|
76
|
+
```
|
77
|
+
|
78
|
+
Square Roots!
|
79
|
+
|
80
|
+
```
|
81
|
+
maths> sqrt 25
|
82
|
+
= 5
|
83
|
+
```
|
84
|
+
|
85
|
+
Exponents!
|
86
|
+
```
|
87
|
+
maths> 2 ^ 4
|
88
|
+
= 16
|
89
|
+
```
|
90
|
+
|
67
91
|
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).
|
68
92
|
|
69
93
|
## Contributing
|
data/bin/maths
CHANGED
@@ -32,7 +32,11 @@ while command = Readline.readline(prompt, true)
|
|
32
32
|
begin
|
33
33
|
result = parser.parse(command)
|
34
34
|
|
35
|
-
if result.
|
35
|
+
if result.nil?
|
36
|
+
output = "Undefined"
|
37
|
+
elsif !result.is_a?(Numeric)
|
38
|
+
output = result.to_s
|
39
|
+
elsif result.frac == 0
|
36
40
|
output = result.to_i.to_s
|
37
41
|
else
|
38
42
|
output = result.to_s("F")
|
data/lib/maths/brain.rb
CHANGED
@@ -12,7 +12,13 @@ module Maths
|
|
12
12
|
|
13
13
|
# Public: Looks up data
|
14
14
|
def self.lookup(variable)
|
15
|
-
|
15
|
+
value = read[variable]
|
16
|
+
|
17
|
+
if value.nil?
|
18
|
+
nil
|
19
|
+
else
|
20
|
+
BigDecimal.new(read[variable])
|
21
|
+
end
|
16
22
|
end
|
17
23
|
|
18
24
|
# Public: The file we're using to store our memories
|
data/lib/maths/calc.y
CHANGED
@@ -2,7 +2,8 @@
|
|
2
2
|
class Calculator
|
3
3
|
prechigh
|
4
4
|
nonassoc UMINUS
|
5
|
-
|
5
|
+
nonassoc FUNC
|
6
|
+
left '^' '*' '/' '%'
|
6
7
|
left '+' '-'
|
7
8
|
right "="
|
8
9
|
preclow
|
@@ -16,12 +17,14 @@ rule
|
|
16
17
|
| exp '*' exp { result *= val[2] }
|
17
18
|
| exp '/' exp { result /= val[2] }
|
18
19
|
| exp '%' exp { result %= val[2] }
|
20
|
+
| exp '^' exp { result = result.power(val[2])}
|
19
21
|
| '(' exp ')' { result = val[1] }
|
20
22
|
| '|' exp '|' { result = val[1].abs }
|
21
23
|
| '-' NUMBER =UMINUS { result = -val[1] }
|
22
24
|
| VARIABLE { result = Brain.lookup(val[0])}
|
23
25
|
| NUMBER
|
24
26
|
| FLOAT
|
27
|
+
| FUNC exp { result = Maths::Functions.exec(val[0], val[1]) }
|
25
28
|
|
26
29
|
assign: VARIABLE "=" exp { result = Brain.assign(val[0], val[2]) }
|
27
30
|
| VARIABLE "=" assign { result = Brain.assign(val[0], val[2]) }
|
@@ -39,7 +42,11 @@ module Maths
|
|
39
42
|
case str
|
40
43
|
when /\A\s+/
|
41
44
|
when /\A[A-Za-z]+[0-9]*/
|
42
|
-
|
45
|
+
if Maths::Functions::FUNCTIONS.has_key?($&)
|
46
|
+
@q.push [:FUNC, $&]
|
47
|
+
else
|
48
|
+
@q.push [:VARIABLE, $&]
|
49
|
+
end
|
43
50
|
when /\A\d+\.\d+/
|
44
51
|
@q.push [:FLOAT, BigDecimal.new($&)]
|
45
52
|
when /\A\d+/
|
data/lib/maths/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
module Maths
|
2
|
-
VERSION = "0.0.
|
3
|
-
end
|
2
|
+
VERSION = "0.0.3"
|
3
|
+
end
|
data/lib/maths.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: maths
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -44,6 +44,7 @@ files:
|
|
44
44
|
- lib/maths.rb
|
45
45
|
- lib/maths/brain.rb
|
46
46
|
- lib/maths/calc.y
|
47
|
+
- lib/maths/functions.rb
|
47
48
|
- lib/maths/version.rb
|
48
49
|
- maths.gemspec
|
49
50
|
homepage: http://github.com/commondream/maths
|