dentaku 2.0.2 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 321fc84e833503ab21de4fdb06254666376ebe85
4
- data.tar.gz: aa1d2820110ba82ff68219e6b5b0681224bfd44d
3
+ metadata.gz: 34a03b8d224b161ba7b6cdbfd0d5f1fd344f932c
4
+ data.tar.gz: 20e97d8a754981e1597748723afd4f54285a0e19
5
5
  SHA512:
6
- metadata.gz: d5929679dbe4e6caea26ba61f4c920fe9a1d9e3b4880cc21c59a57e33109dc0ae9896f1c47ad4baadfe3ad1e2a0ac20d588b2266b63c8b3fcf4f535504871c95
7
- data.tar.gz: aae2e18c9cad2e3452e2c8193c93be0754ee09628095119fdff91db9de613526cba57f9f4b3f5ff0c0eee6935298c754075ded6cd75b86c96c3aa1000cc29add
6
+ metadata.gz: 510851224ae2e3e6b6a19b229cfe6095a28eaf9c6ac978edd6ea40295f4dc9a37b2fdb03d6f0072ef79b9fcfe1bf34f715f3f910d21b79491ce72643e66cddaf
7
+ data.tar.gz: b27e2dfc0c821f04475310eb47e6d30d908c02a77ba7da7512ed703a21b99c62e6a85a21be25d34db8540d9e10d131971ca45dfd54be554aed0a29e71e9b1b50
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- ## [v2.0.2] 2015-08-20
3
+ ## [v2.0.3] 2015-08-25
4
4
  - bug fixes
5
5
  - performance enhancements
6
6
  - code cleanup
@@ -80,7 +80,7 @@
80
80
  ## [v0.1.0] 2012-01-20
81
81
  - initial release
82
82
 
83
- [v2.0.2]: https://github.com/rubysolo/dentaku/compare/v2.0.1...v2.0.2
83
+ [v2.0.3]: https://github.com/rubysolo/dentaku/compare/v2.0.1...v2.0.3
84
84
  [v2.0.1]: https://github.com/rubysolo/dentaku/compare/v2.0.0...v2.0.1
85
85
  [v2.0.0]: https://github.com/rubysolo/dentaku/compare/v1.2.6...v2.0.0
86
86
  [v1.2.6]: https://github.com/rubysolo/dentaku/compare/v1.2.5...v1.2.6
@@ -1,4 +1,5 @@
1
1
  require_relative './operation'
2
+ require 'bigdecimal'
2
3
 
3
4
  module Dentaku
4
5
  module AST
@@ -51,7 +52,22 @@ module Dentaku
51
52
 
52
53
  class Division < Arithmetic
53
54
  def value(context={})
54
- left.value(context) / right.value(context)
55
+ r = BigDecimal.new(right.value(context))
56
+ raise ZeroDivisionError if r.zero?
57
+
58
+ v = BigDecimal.new(left.value(context)) / r
59
+ v = v.to_i if v.frac.zero?
60
+ v
61
+ end
62
+
63
+ def self.precedence
64
+ 20
65
+ end
66
+ end
67
+
68
+ class Modulo < Arithmetic
69
+ def value(context={})
70
+ left.value(context) % right.value(context)
55
71
  end
56
72
 
57
73
  def self.precedence
@@ -114,6 +114,7 @@ module Dentaku
114
114
  divide: AST::Division,
115
115
  pow: AST::Exponentiation,
116
116
  negate: AST::Negation,
117
+ mod: AST::Modulo,
117
118
 
118
119
  lt: AST::LessThan,
119
120
  gt: AST::GreaterThan,
@@ -1,3 +1,3 @@
1
1
  module Dentaku
2
- VERSION = "2.0.2"
2
+ VERSION = "2.0.3"
3
3
  end
@@ -17,7 +17,7 @@ describe Dentaku::AST::Addition do
17
17
  it 'requires numeric operands' do
18
18
  expect {
19
19
  described_class.new(five, t)
20
- }.to raise_error
20
+ }.to raise_error(RuntimeError, /requires numeric operands/)
21
21
 
22
22
  expression = Dentaku::AST::Multiplication.new(five, five)
23
23
  group = Dentaku::AST::Grouping.new(expression)
@@ -17,7 +17,7 @@ describe Dentaku::AST::And do
17
17
  it 'requires logical operands' do
18
18
  expect {
19
19
  described_class.new(t, five)
20
- }.to raise_error
20
+ }.to raise_error(RuntimeError, /requires logical operands/)
21
21
 
22
22
  expression = Dentaku::AST::LessThanOrEqual.new(five, five)
23
23
  expect {
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+ require 'dentaku/ast/arithmetic'
3
+
4
+ require 'dentaku/token'
5
+
6
+ describe Dentaku::AST::Division do
7
+ let(:five) { Dentaku::AST::Logical.new Dentaku::Token.new(:numeric, 5) }
8
+ let(:six) { Dentaku::AST::Logical.new Dentaku::Token.new(:numeric, 6) }
9
+
10
+ let(:t) { Dentaku::AST::Numeric.new Dentaku::Token.new(:logical, true) }
11
+
12
+ it 'performs division' do
13
+ node = described_class.new(five, six)
14
+ expect(node.value.round(4)).to eq 0.8333
15
+ end
16
+
17
+ it 'requires numeric operands' do
18
+ expect {
19
+ described_class.new(five, t)
20
+ }.to raise_error(RuntimeError, /requires numeric operands/)
21
+
22
+ expression = Dentaku::AST::Multiplication.new(five, five)
23
+ group = Dentaku::AST::Grouping.new(expression)
24
+
25
+ expect {
26
+ described_class.new(group, five)
27
+ }.not_to raise_error
28
+ end
29
+ end
@@ -7,7 +7,7 @@ describe Dentaku::AST::Function do
7
7
  end
8
8
 
9
9
  it 'raises an exception when trying to access an undefined function' do
10
- expect { described_class.get("flarble") }.to raise_error
10
+ expect { described_class.get("flarble") }.to raise_error(RuntimeError, /undefined function/i)
11
11
  end
12
12
 
13
13
  it 'registers a custom function' do
@@ -27,6 +27,8 @@ describe Dentaku::Calculator do
27
27
  expect(calculator.evaluate('0 * 10 ^ -5')).to eq(0)
28
28
  expect(calculator.evaluate('3 + 0 * -3')).to eq(3)
29
29
  expect(calculator.evaluate('3 + 0 / -3')).to eq(3)
30
+ expect(calculator.evaluate('15 % 8')).to eq(7)
31
+ expect(calculator.evaluate('(((695759/735000)^(1/(1981-1991)))-1)*1000').round(4)).to eq(5.5018)
30
32
  end
31
33
 
32
34
  describe 'memory' do
@@ -81,7 +83,7 @@ describe Dentaku::Calculator do
81
83
  it "lets you know about a cycle if one occurs" do
82
84
  expect do
83
85
  calculator.solve!(health: "happiness", happiness: "health")
84
- end.to raise_error (TSort::Cyclic)
86
+ end.to raise_error(TSort::Cyclic)
85
87
  end
86
88
 
87
89
  it 'is case-insensitive' do
@@ -82,7 +82,7 @@ describe Dentaku::Parser do
82
82
  x = Dentaku::Token.new(:identifier, :x)
83
83
 
84
84
  node = described_class.new([five, times, x]).parse
85
- expect { node.value }.to raise_error
85
+ expect { node.value }.to raise_error(Dentaku::UnboundVariableError)
86
86
  expect(node.value(x: 3)).to eq 15
87
87
  end
88
88
 
@@ -142,8 +142,8 @@ describe Dentaku::Tokenizer do
142
142
  end
143
143
 
144
144
  it 'detects unbalanced parentheses' do
145
- expect { tokenizer.tokenize('(5+3') }.to raise_error
146
- expect { tokenizer.tokenize(')') }.to raise_error
145
+ expect { tokenizer.tokenize('(5+3') }.to raise_error(RuntimeError, /too many opening parentheses/)
146
+ expect { tokenizer.tokenize(')') }.to raise_error(RuntimeError, /too many closing parentheses/)
147
147
  end
148
148
 
149
149
  it 'recognizes identifiers that share initial substrings with combinators' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dentaku
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Solomon White
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-20 00:00:00.000000000 Z
11
+ date: 2015-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -105,6 +105,7 @@ files:
105
105
  - lib/dentaku/version.rb
106
106
  - spec/ast/addition_spec.rb
107
107
  - spec/ast/and_spec.rb
108
+ - spec/ast/division_spec.rb
108
109
  - spec/ast/function_spec.rb
109
110
  - spec/ast/node_spec.rb
110
111
  - spec/ast/numeric_spec.rb
@@ -146,6 +147,7 @@ summary: A formula language parser and evaluator
146
147
  test_files:
147
148
  - spec/ast/addition_spec.rb
148
149
  - spec/ast/and_spec.rb
150
+ - spec/ast/division_spec.rb
149
151
  - spec/ast/function_spec.rb
150
152
  - spec/ast/node_spec.rb
151
153
  - spec/ast/numeric_spec.rb