eymiha_math 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/gem_package.rb +1 -1
- data/lib/approximately_equals.rb +10 -2
- data/lib/eymiha_math.rb +1 -0
- data/lib/fraction.rb +31 -0
- data/lib/rectify.rb +23 -10
- data/test/tc_fraction.rb +19 -0
- data/test/tc_rectify.rb +10 -0
- metadata +5 -3
data/gem_package.rb
CHANGED
data/lib/approximately_equals.rb
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
# Approminately equals returns true when values are close enough, within some
|
2
|
-
# distance epsilon above or below the given value.
|
2
|
+
# distance epsilon above or below the given value. Three methods and a class
|
3
|
+
# variable are added to the Numeric class.
|
4
|
+
#
|
5
|
+
# @@epsilon, and two class methods, Numeric.epsilon and Numeric.epsilon= get
|
6
|
+
# and set the maximum difference between two values for them to be
|
7
|
+
# approximately equal.
|
8
|
+
#
|
9
|
+
# approximately_equals? and the aliased operator =~ return true if two values
|
10
|
+
# are approximately equal.
|
3
11
|
|
4
|
-
#
|
12
|
+
# Behavor added to Numeric by eymiha_math.
|
5
13
|
class Numeric
|
6
14
|
|
7
15
|
# The initial default distance from a Numeric below which it is considered
|
data/lib/eymiha_math.rb
CHANGED
data/lib/fraction.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# Behavior for rendering and rounding fractions with respect to a denominator.
|
2
|
+
# Two methods are added to the Numeric class.
|
3
|
+
#
|
4
|
+
# with_fraction is an alternative to to_s that renders a value as a string
|
5
|
+
# with a whole number and any decimal part as a fraction.
|
6
|
+
#
|
7
|
+
# round_to_nearest rounds a value such that a fractional part with the given
|
8
|
+
# denominator will have integer as its numerator.
|
9
|
+
|
10
|
+
# Behavior added to Numeric by eymiha_math
|
11
|
+
class Numeric
|
12
|
+
|
13
|
+
# Renders a numeric value as a whole number and a fraction with a given
|
14
|
+
# denominator, separated with a given separator, and optionally showing
|
15
|
+
# fractions with a numerator of zero.
|
16
|
+
def with_fraction(denominator=1,separator='-',show_zero=false)
|
17
|
+
sign = self <=> 0
|
18
|
+
unsigned = self*sign
|
19
|
+
whole = (((unsigned*denominator).round)/denominator).floor
|
20
|
+
numerator = ((unsigned-whole)*denominator).round
|
21
|
+
"#{sign*whole}#{(numerator != 0)||show_zero ?
|
22
|
+
"#{separator}#{numerator}/#{denominator}" : ""}"
|
23
|
+
end
|
24
|
+
|
25
|
+
# Returns a numeric value such that any remainder is evenly divided by
|
26
|
+
# the given denominator.
|
27
|
+
def round_to_nearest(denominator=1)
|
28
|
+
((self*denominator).round)/(1.0*denominator)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
data/lib/rectify.rb
CHANGED
@@ -1,7 +1,16 @@
|
|
1
|
-
# Rectification maps values to an
|
2
|
-
#
|
1
|
+
# Rectification maps values to an limits treated either as cycles, upper
|
2
|
+
# bounds, lower bounds or both. Four methods are added to the Numeric class:
|
3
|
+
#
|
4
|
+
# wrap_rectify() wraps values to an interval treated as a cycle. Think of it
|
5
|
+
# as degrees around a compass.
|
6
|
+
#
|
7
|
+
# cut_rectify() cuts off values to keep them between an upper and lower bound.
|
8
|
+
#
|
9
|
+
# at_most() cuts off values at an upper bound.
|
10
|
+
#
|
11
|
+
# at_least() cuts off value at a lower bound.
|
3
12
|
|
4
|
-
#
|
13
|
+
# Behavior added to Numeric by eymiha_math.
|
5
14
|
class Numeric
|
6
15
|
|
7
16
|
# Treats the interval between low and high as a cycle, returning the result
|
@@ -21,13 +30,17 @@ class Numeric
|
|
21
30
|
# the two.
|
22
31
|
def cut_rectify(high=1,low=0)
|
23
32
|
low,high = high,low if low > high
|
24
|
-
|
25
|
-
low
|
26
|
-
elsif (self > high)
|
27
|
-
high
|
28
|
-
else
|
29
|
-
self
|
30
|
-
end
|
33
|
+
(self < low)? low : (self > high)? high : self
|
31
34
|
end
|
32
35
|
|
36
|
+
# Constrains a value to an upper limit.
|
37
|
+
def at_most(high=0)
|
38
|
+
self > high ? high : self
|
39
|
+
end
|
40
|
+
|
41
|
+
# Constrains a value to a lower limit.
|
42
|
+
def at_least(low=0)
|
43
|
+
self < low ? low : self
|
44
|
+
end
|
45
|
+
|
33
46
|
end
|
data/test/tc_fraction.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
require 'fraction'
|
4
|
+
|
5
|
+
class TC_fraction < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def test_with_fraction
|
8
|
+
assert(2.31.with_fraction(4) == "2-1/4")
|
9
|
+
assert(2.31.with_fraction(5) == "2-2/5")
|
10
|
+
assert(2.31.with_fraction(6,' ') == "2 2/6")
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_round_to_nearest
|
14
|
+
assert(0.22.round_to_nearest(5) == 0.2)
|
15
|
+
assert(0.22.round_to_nearest(10) == 0.2)
|
16
|
+
assert(0.22.round_to_nearest(50) == 0.22)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
data/test/tc_rectify.rb
CHANGED
@@ -25,5 +25,15 @@ class TC_rectify < Test::Unit::TestCase
|
|
25
25
|
assert(0.5.cut_rectify(0.5,2) == 0.5)
|
26
26
|
assert(0.5.cut_rectify(0,0) == 0)
|
27
27
|
end
|
28
|
+
|
29
|
+
def test_at_most
|
30
|
+
assert((-5..5).collect{|v| v.at_most(2)} ==
|
31
|
+
[-5,-4,-3,-2,-1,0,1,2,2,2,2])
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_at_least
|
35
|
+
assert((-5..5).collect{|v| v.at_least(-1.5)} ==
|
36
|
+
[-1.5,-1.5,-1.5,-1.5,-1,0,1,2,3,4,5])
|
37
|
+
end
|
28
38
|
|
29
39
|
end
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.
|
2
|
+
rubygems_version: 0.9.2
|
3
3
|
specification_version: 1
|
4
4
|
name: eymiha_math
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.1.1
|
7
|
+
date: 2007-08-22 00:00:00 -04:00
|
8
8
|
summary: Eymiha basic math extensions
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -33,8 +33,10 @@ files:
|
|
33
33
|
- rakefile.rb
|
34
34
|
- lib/approximately_equals.rb
|
35
35
|
- lib/eymiha_math.rb
|
36
|
+
- lib/fraction.rb
|
36
37
|
- lib/rectify.rb
|
37
38
|
- test/tc_approximately_equals.rb
|
39
|
+
- test/tc_fraction.rb
|
38
40
|
- test/tc_rectify.rb
|
39
41
|
test_files: []
|
40
42
|
|