maths 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/maths +2 -11
- data/lib/maths/calc.y +14 -3
- data/lib/maths/formatter.rb +22 -0
- data/lib/maths/percentage.rb +14 -0
- data/lib/maths/version.rb +1 -1
- data/lib/maths.rb +3 -1
- metadata +3 -1
data/bin/maths
CHANGED
@@ -31,17 +31,8 @@ while command = Readline.readline(prompt, true)
|
|
31
31
|
else
|
32
32
|
begin
|
33
33
|
result = parser.parse(command)
|
34
|
-
|
35
|
-
|
36
|
-
output = "Undefined"
|
37
|
-
elsif !result.is_a?(Numeric)
|
38
|
-
output = result.to_s
|
39
|
-
elsif result.frac == 0
|
40
|
-
output = result.to_i.to_s
|
41
|
-
else
|
42
|
-
output = result.to_s("F")
|
43
|
-
end
|
44
|
-
puts "= #{output}"
|
34
|
+
|
35
|
+
puts "= #{Maths::Formatter.format(result)}"
|
45
36
|
rescue ParseError
|
46
37
|
puts $!
|
47
38
|
end
|
data/lib/maths/calc.y
CHANGED
@@ -3,8 +3,9 @@ class Calculator
|
|
3
3
|
prechigh
|
4
4
|
nonassoc UMINUS
|
5
5
|
nonassoc FUNC
|
6
|
-
left
|
7
|
-
left
|
6
|
+
left "^" "*" '/' '%'
|
7
|
+
left "+" "-"
|
8
|
+
right PERCENTAGE_OF
|
8
9
|
right "="
|
9
10
|
preclow
|
10
11
|
rule
|
@@ -25,7 +26,8 @@ rule
|
|
25
26
|
| NUMBER
|
26
27
|
| FLOAT
|
27
28
|
| FUNC exp { result = Maths::Functions.exec(val[0], val[1]) }
|
28
|
-
|
29
|
+
| exp PERCENTAGE_OF exp { result = Percentage.build(val[0], val[2]) }
|
30
|
+
|
29
31
|
assign: VARIABLE "=" exp { result = Brain.assign(val[0], val[2]) }
|
30
32
|
| VARIABLE "=" assign { result = Brain.assign(val[0], val[2]) }
|
31
33
|
|
@@ -41,6 +43,15 @@ module Maths
|
|
41
43
|
until str.empty?
|
42
44
|
case str
|
43
45
|
when /\A\s+/
|
46
|
+
when /\Aas a percentage of/
|
47
|
+
@q.push [:PERCENTAGE_OF, $&]
|
48
|
+
when /\Aas a % of/
|
49
|
+
@q.push [:PERCENTAGE_OF, $&]
|
50
|
+
when /\Aas % of/
|
51
|
+
@q.push [:PERCENTAGE_OF, $&]
|
52
|
+
when /\Aas % of/
|
53
|
+
@q.push [:PERCENTAGE_OF, $&]
|
54
|
+
|
44
55
|
when /\A[A-Za-z]+[0-9]*/
|
45
56
|
if Maths::Functions::FUNCTIONS.has_key?($&)
|
46
57
|
@q.push [:FUNC, $&]
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Maths
|
2
|
+
class Formatter
|
3
|
+
# Public: Formats values for us to output.
|
4
|
+
def self.format(val)
|
5
|
+
if val.nil?
|
6
|
+
output = "Undefined"
|
7
|
+
elsif !val.is_a?(Numeric)
|
8
|
+
output = val.to_s
|
9
|
+
elsif val.frac == 0
|
10
|
+
output = val.to_i.to_s
|
11
|
+
else
|
12
|
+
output = val.to_s("F")
|
13
|
+
end
|
14
|
+
|
15
|
+
if val.is_a?(Percentage)
|
16
|
+
output += "%"
|
17
|
+
end
|
18
|
+
|
19
|
+
output
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Maths
|
2
|
+
# Public: Represents a number that should be formatted as a percentage
|
3
|
+
class Percentage < BigDecimal
|
4
|
+
# Pretty much idential to a BigDecimal
|
5
|
+
|
6
|
+
# Public: Builds out a fraction by the numerator and denominator
|
7
|
+
def self.build(numerator, denominator)
|
8
|
+
numerator = BigDecimal.new(numerator.to_s)
|
9
|
+
denominator = BigDecimal.new(denominator.to_s)
|
10
|
+
puts numerator, denominator
|
11
|
+
Percentage.new(((numerator / denominator) * 100).to_s)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/maths/version.rb
CHANGED
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.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -44,7 +44,9 @@ files:
|
|
44
44
|
- lib/maths.rb
|
45
45
|
- lib/maths/brain.rb
|
46
46
|
- lib/maths/calc.y
|
47
|
+
- lib/maths/formatter.rb
|
47
48
|
- lib/maths/functions.rb
|
49
|
+
- lib/maths/percentage.rb
|
48
50
|
- lib/maths/version.rb
|
49
51
|
- maths.gemspec
|
50
52
|
homepage: http://github.com/commondream/maths
|