Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
pnelson committed Mar 2, 2014
0 parents commit 8e2be55
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 0 deletions.
27 changes: 27 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) 2014 by Philip Nelson

Some rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

- The names of the contributors may not be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14 changes: 14 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fraction
========

Approximate fractions from floats.

Modeled after https://www.ics.uci.edu/~eppstein/numth/frap.c.

Note
----

I feel like this is probably already implemented in the standard library. I
took a look at `Rat` from `math/big` but didn't have any luck.

http://play.golang.org/p/6ht0tn5xKH
57 changes: 57 additions & 0 deletions fraction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Package fraction finds rational approximations to real numbers.
package fraction

// Fraction returns the numerator, denominator and error of v, limiting the
// denominator to a maximum of d. If the provided value of d is less than 1, it
// defaults to 10. If v is negative, the numerator will be negative.
func Fraction(v float64, d int64) (int64, int64, float64) {
sign := int64(1)
ai := int64(0)
m := [2][2]int64{{1, 0}, {0, 1}}

if v < 0 {
sign = -1
v *= -1.0
}

if d < 1 {
d = 10
}

x := v

// Find terms until denominator gets too large.
for m[1][0]*ai+m[1][1] <= d {
m[0][1], m[0][0] = m[0][0], m[0][0]*ai+m[0][1]
m[1][1], m[1][0] = m[1][0], m[1][0]*ai+m[1][1]

// Handle division by 0.
if x == float64(ai) {
break
}

x = 1 / (x - float64(ai))
ai = int64(x)

// Handle representation failure.
if x > float64(0x7FFFFFFF) {
break
}
}

return m[0][0] * sign, m[1][0], v - (float64(m[0][0]) / float64(m[1][0]))
}

// WholeFraction returns the whole, numerator, denominator and error of v,
// limiting the denominator to a maximum of d. If the provided value of d is
// less than 1, it defaults to 10. The whole number will take the sign unless
// it is 0, in which case the numerator will.
func WholeFraction(v float64, d int64) (int64, int64, int64, float64) {
whole := int64(v)
v -= float64(whole)
num, den, e := Fraction(v, d)
if num < 0 && whole != 0 {
num *= -1
}
return whole, num, den, e
}
88 changes: 88 additions & 0 deletions fraction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package fraction

import (
"fmt"
"math"
"testing"
)

var fractionTest = []struct {
v float64
d int64
num int64
den int64
}{
{0.33, 10, 1, 3},
{0.33, -1, 1, 3},
{-0.33, 10, -1, 3},
}

var wholeFractionTest = []struct {
v float64
d int64
whole int64
num int64
den int64
}{
{2.33, 10, 2, 1, 3},
{2.33, -1, 2, 1, 3},
{-2.33, 10, -2, 1, 3},
{-0.33, 10, 0, -1, 3},
}

func TestFraction(t *testing.T) {
for i, tt := range fractionTest {
num, den, e := Fraction(tt.v, tt.d)
if num != tt.num {
t.Errorf("%d. Fraction num = %v, want %v", i, num, tt.num)
}
if den != tt.den {
t.Errorf("%d. Fraction den = %v, want %v", i, den, tt.den)
}
if abs := math.Abs(e); abs > 0.01 {
t.Errorf("%d. Fraction math.Abs(%v) = %v > 0.01", i, e, abs)
}
}
}

func TestWholeFraction(t *testing.T) {
for i, tt := range wholeFractionTest {
whole, num, den, e := WholeFraction(tt.v, tt.d)
if whole != tt.whole {
t.Errorf("%d. WholeFraction whole = %v, want %v", i, whole, tt.whole)
}
if num != tt.num {
t.Errorf("%d. WholeFraction num = %v, want %v", i, num, tt.num)
}
if den != tt.den {
t.Errorf("%d. WholeFraction den = %v, want %v", i, den, tt.den)
}
if abs := math.Abs(e); abs > 0.01 {
t.Errorf("%d. WholeFraction math.Abs(%v) = %v > 0.01", i, e, abs)
}
}
}

func ExampleFraction() {
num, den, e := Fraction(0.33, -1)
fmt.Println(num)
fmt.Println(den)
fmt.Println(e)
// Output:
// 1
// 3
// -0.0033333333333332993
}

func ExampleWholeFraction() {
whole, num, den, e := WholeFraction(2.5, -1)
fmt.Println(whole)
fmt.Println(num)
fmt.Println(den)
fmt.Println(e)
// Output:
// 2
// 1
// 2
// 0
}

0 comments on commit 8e2be55

Please sign in to comment.