fraction_parser 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.1.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{fraction_parser}
8
- s.version = "0.0.3"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Steve Hoeksema"]
12
- s.date = %q{2010-04-16}
12
+ s.date = %q{2010-04-21}
13
13
  s.description = %q{Parses fractions and converts them to Rational}
14
14
  s.email = %q{steve@seven.net.nz}
15
15
  s.extra_rdoc_files = [
@@ -1,13 +1,34 @@
1
+ require "rational"
2
+
1
3
  class FractionParser
2
4
 
3
5
  def self.parse(string)
4
6
  string = string.strip
5
7
  if string =~ /^((\d+)\s+)?((\d+)\/(\d+))$/
6
- sum = Rational($4, $5)
8
+ sum = Rational($4.to_i, $5.to_i)
7
9
  sum += $2.to_i if $2
8
10
  sum
9
11
  else
10
- sum = Rational(string.to_f, 1)
12
+ sum = self.float_to_rational(string.to_f)
13
+ end
14
+ end
15
+
16
+ # Whoa this method is crazy
17
+ # I nicked it from Jannis Harder at http://markmail.org/message/nqgrsmaixwbrvsno
18
+ def self.float_to_rational(value)
19
+ if value.nan?
20
+ return Rational(0,0) # Div by zero error
21
+ elsif value.infinite?
22
+ return Rational(value<0 ? -1 : 1,0) # Div by zero error
23
+ end
24
+
25
+ s,e,f = [value].pack("G").unpack("B*").first.unpack("AA11A52")
26
+ s = (-1)**s.to_i
27
+ e = e.to_i(2)
28
+ if e.nonzero? and e<2047
29
+ Rational(s)* Rational(2)**(e-1023)*Rational("1#{f}".to_i(2),0x10000000000000)
30
+ elsif e.zero?
31
+ Rational(s)* Rational(2)**(-1024)*Rational("0#{f}".to_i(2),0x10000000000000)
11
32
  end
12
33
  end
13
34
 
@@ -2,20 +2,21 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "FractionParser" do
4
4
 
5
- def should_parse(string, float)
6
- FractionParser.parse(string).to_f.should eql(float)
5
+ def should_parse_fraction(string, float)
6
+ fp = FractionParser.parse(string)
7
+ fp.to_f.should eql(float)
7
8
  end
8
9
 
9
10
  it "should parse fractions" do
10
- should_parse("3/4", 0.75)
11
- should_parse("2 1/16", 2.0625)
12
- should_parse("8", 8.0)
13
- should_parse("8.25", 8.25)
11
+ should_parse_fraction("3/4", 0.75)
12
+ should_parse_fraction("2 1/16", 2.0625)
13
+ should_parse_fraction("8", 8.0)
14
+ should_parse_fraction("8.25", 8.25)
14
15
  end
15
16
 
16
17
  it "should otherwise just convert to a float" do
17
- should_parse("2.0 3/4", 2.0)
18
- should_parse("2 3 /4", 2.0)
18
+ should_parse_fraction("2.0 3/4", 2.0)
19
+ should_parse_fraction("2 3 /4", 2.0)
19
20
  end
20
21
 
21
22
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 1
7
8
  - 0
8
- - 3
9
- version: 0.0.3
9
+ version: 0.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Steve Hoeksema
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-16 00:00:00 +12:00
17
+ date: 2010-04-21 00:00:00 +12:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency