math_calculator 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +55 -0
- data/lib/math_calculator.rb +78 -0
- data/spec/math_calculator_spec.rb +79 -0
- data/spec/spec_helper.rb +13 -0
- metadata +62 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Anil Sharma
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
= math_calculator
|
2
|
+
|
3
|
+
Math calculator evaluates expressions containg terms supported bt the Ruby's Math module.
|
4
|
+
Although you could do the same with eval, MathCalulator makes sure that only valid Math
|
5
|
+
functions are evaluated.
|
6
|
+
|
7
|
+
== Install
|
8
|
+
|
9
|
+
Install math_calculator like any other ruby gem:
|
10
|
+
|
11
|
+
gem install math_calculator
|
12
|
+
|
13
|
+
== Overview of usage
|
14
|
+
|
15
|
+
math_calculator accepts a string containg the expression to be evaluated
|
16
|
+
and returns the value of the expression. It optionally accepts a hash of variable
|
17
|
+
values used in the expression. The options key must be a symbol representing
|
18
|
+
a variable, and the options value must be numeric, which would be used as the
|
19
|
+
value of the variable to evaluate the expression.
|
20
|
+
|
21
|
+
== Commands and Usage
|
22
|
+
|
23
|
+
Available Command:
|
24
|
+
|
25
|
+
str = "2+4"
|
26
|
+
MathCalculator.calculate(str) # 6
|
27
|
+
|
28
|
+
MathCalculator.calculate('(x+y)*2', :x => 2, :y => 4) # 12
|
29
|
+
|
30
|
+
str = "x+y"
|
31
|
+
options = {:x => 2, :y => 4}
|
32
|
+
MathCalculator.calculate(str, options) # 6
|
33
|
+
|
34
|
+
|
35
|
+
== Issues, Suggestions
|
36
|
+
|
37
|
+
* https://github.com/aksharma/math_calculator/issues/
|
38
|
+
* or email me directly at (sharma.rubyonrails)
|
39
|
+
* this address ( @ )
|
40
|
+
* (sharmail_dot_com )
|
41
|
+
|
42
|
+
== Contributing to math_calculator
|
43
|
+
|
44
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
45
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
46
|
+
* Fork the project.
|
47
|
+
* Start a feature/bugfix branch.
|
48
|
+
* Commit and push until you are happy with your contribution.
|
49
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
50
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
51
|
+
|
52
|
+
== Copyright
|
53
|
+
|
54
|
+
Copyright (c) 2012 Anil Sharma. See LICENSE for further details.
|
55
|
+
|
@@ -0,0 +1,78 @@
|
|
1
|
+
class MathCalculator
|
2
|
+
include Math
|
3
|
+
|
4
|
+
def self.calculate(str='', options={})
|
5
|
+
@@safe_formula = true
|
6
|
+
self.tokenize(str,options)
|
7
|
+
options.each {|k,v| @@safe_formula = false unless (k.is_a? Symbol and v.is_a? Numeric)}
|
8
|
+
if self.safe_formula?
|
9
|
+
options.each do |var, val|
|
10
|
+
str = "#{var.to_s} = #{val}; #{str}"
|
11
|
+
end
|
12
|
+
self.constants_list.each do |const|
|
13
|
+
str = "#{const.to_s.downcase} = #{const}; #{str}"
|
14
|
+
end
|
15
|
+
eval str
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def self.tokenize(str, options={})
|
22
|
+
new_str0 = str.gsub(/\s*/,'')
|
23
|
+
new_str1 = new_str0.gsub(/[a-zA-Z]+/,' \0 ')
|
24
|
+
new_str2 = new_str1.gsub(/\d*\.?\d+/,' \0 ')
|
25
|
+
arr1 = new_str2.split(' ')
|
26
|
+
new_arr = []
|
27
|
+
arr1.each do |token|
|
28
|
+
if (self.valid_tokens_list(options).include? token)
|
29
|
+
new_arr << token
|
30
|
+
elsif token.match(/\d*\.?\d+/) &&
|
31
|
+
(token.match(/\d*\.?\d+/)[0] == token)
|
32
|
+
new_arr << token
|
33
|
+
else
|
34
|
+
char_arr = token.split('')
|
35
|
+
char_arr.each do |char|
|
36
|
+
if self.chars_list.include? char
|
37
|
+
new_arr << char
|
38
|
+
else
|
39
|
+
@@safe_formula = false
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
return new_arr
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.valid_tokens_list(options)
|
48
|
+
(self.public_methods_list +
|
49
|
+
self.constants_list +
|
50
|
+
self.downcase_constants_list +
|
51
|
+
self.user_variables_list(options)
|
52
|
+
).map{|t| t.to_s}
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.user_variables_list(options)
|
56
|
+
options.keys.map(&:to_s)
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.public_methods_list
|
60
|
+
(Math.public_methods - Object.methods)
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.constants_list
|
64
|
+
(Math.constants - [:DomainError])
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.downcase_constants_list
|
68
|
+
(Math.constants - [:DomainError]).map{|c| c.to_s.downcase}
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.chars_list
|
72
|
+
%w[+ - / * ( ) ]
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.safe_formula?
|
76
|
+
@@safe_formula
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "Simple calculation" do
|
4
|
+
it "should do simple addition" do
|
5
|
+
str = "2+4"
|
6
|
+
MathCalculator.calculate(str).should == 6
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "Simple calculation with negative numbers" do
|
11
|
+
it "should do simple addition of negative numbers" do
|
12
|
+
str = "-2+(-4)"
|
13
|
+
MathCalculator.calculate(str).should == -6
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "Value of constant PI" do
|
18
|
+
it "should evaluate PI" do
|
19
|
+
str = 'PI'
|
20
|
+
MathCalculator.calculate(str).should == PI
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "Value of constant pi" do
|
25
|
+
it "should evaluate pi" do
|
26
|
+
str = 'pi'
|
27
|
+
MathCalculator.calculate(str).should == PI
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "Value of constant E" do
|
32
|
+
it "should evaluate E" do
|
33
|
+
str = 'E'
|
34
|
+
MathCalculator.calculate(str).should == E
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
describe "Value of a single Math function" do
|
40
|
+
it "should evaluate log(2)" do
|
41
|
+
str = 'log 2'
|
42
|
+
MathCalculator.calculate(str).should == log(2)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "Tokenize expression" do
|
47
|
+
it "should create an array of tokens in expressions" do
|
48
|
+
str = 'cos(90)*acos(0.23)*cos(90)'
|
49
|
+
MathCalculator.tokenize(str).should == %w[cos ( 90 ) * acos ( 0.23 ) * cos ( 90 )]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "Tokenize expression" do
|
54
|
+
it "should create an array of tokens in expressions" do
|
55
|
+
str = '(3*exp(t/2)*cos(3*t-PI/2))'
|
56
|
+
MathCalculator.tokenize(str, :t => 2).should == %w[ ( 3 * exp ( t / 2 ) * cos ( 3 * t - PI / 2 ) )]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "Evaluate expression" do
|
61
|
+
it "should evaluate matehematicale expressions" do
|
62
|
+
str = '(3*exp(t/2)*cos(3*t-PI/2))'
|
63
|
+
MathCalculator.calculate(str, :t => 2).should == -2.2785902140319134
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "Unsafe expression" do
|
68
|
+
it "should not be evaluated" do
|
69
|
+
str = 'PI*3+xyz'
|
70
|
+
MathCalculator.calculate(str).should == nil
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "Unsafe variable" do
|
75
|
+
it "should not be evaluated" do
|
76
|
+
str = '2+2'
|
77
|
+
MathCalculator.calculate(str, :t => 'abc').should == nil
|
78
|
+
end
|
79
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'math_calculator'
|
5
|
+
include Math
|
6
|
+
|
7
|
+
# Requires supporting files with custom matchers and macros, etc,
|
8
|
+
# in ./support/ and its subdirectories.
|
9
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: math_calculator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Anil Sharma
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-06-09 00:00:00 -04:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Math calculator evalutaes expressions containg terms supported bt the Ruby's Math module.
|
18
|
+
email: sharma.ruby2gem@sharmail.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- lib/math_calculator.rb
|
27
|
+
- spec/spec_helper.rb
|
28
|
+
- spec/math_calculator_spec.rb
|
29
|
+
- README.rdoc
|
30
|
+
- LICENSE
|
31
|
+
- spec//spec_helper.rb
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://rubygems.org/gems/math_calculator
|
34
|
+
licenses: []
|
35
|
+
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.5.3
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: Math calculator using the Ruby Math module
|
60
|
+
test_files:
|
61
|
+
- spec//spec_helper.rb
|
62
|
+
- spec/math_calculator_spec.rb
|