Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added comments for godoc.

  • Loading branch information...
commit 06fb2706e6a256cacf149b4b35194e18f9d6d5f8 1 parent 9945c1c
@anschelsc authored
Showing with 20 additions and 6 deletions.
  1. +20 −6 frac.go
View
26 frac.go
@@ -2,7 +2,7 @@
//Under go's BSD-style license
//at $GOROOT/LICENSE
-//The frac package implements high-precision fractions.
+//The frac package implements fractions.
package frac
import (
@@ -10,6 +10,7 @@ import (
"fmt"
)
+//A Frac is a fraction.
type Frac struct {
num, den uint64
positive bool
@@ -22,7 +23,7 @@ const (
)
func abs(i int64) uint64 {
- if i>=0{
+ if i >= 0 {
return uint64(i)
}
return uint64(-i)
@@ -43,6 +44,7 @@ func gcd(x, y uint64) uint64 {
return x
}
+//New() returns a *Frac unless den==0
func New(num, den int64) (*Frac, os.Error) {
if den == 0 {
return nil, DivByZero
@@ -56,6 +58,7 @@ func New(num, den int64) (*Frac, os.Error) {
return f, nil
}
+//f.String() satisfies fmt.Stringer
func (f *Frac) String() string {
if f.positive {
return fmt.Sprintf("%d/%d", f.num, f.den)
@@ -64,8 +67,8 @@ func (f *Frac) String() string {
}
func (f *Frac) simplify() {
- if f.num == 0 {
- f.den = 1
+ if f.num == 0 { //This is not strictly necessary, but it prevents
+ f.den = 1 //f.String() from outputing "-0"
return
}
common := gcd(f.num, f.den)
@@ -73,10 +76,12 @@ func (f *Frac) simplify() {
f.den /= common
}
+//f.Positive() returns true if f>0 and false if f<0. Behavior at f==0 is undefined.
func (f *Frac) Positive() bool {
return f.positive
}
+//f.Plus(other) returns f + other, leaving both unchanged.
func (f *Frac) Plus(other *Frac) *Frac {
if f.den == other.den {
ret := new(Frac)
@@ -103,24 +108,29 @@ func (f *Frac) Plus(other *Frac) *Frac {
return (&Frac{f.num * other.den, f.den * other.den, f.positive}).Plus(&Frac{other.num * f.den, other.den * f.den, other.positive})
}
+//f.Negative() returns -f.
func (f *Frac) Negative() *Frac {
return &Frac{f.num, f.den, !f.positive}
}
+//f.Minus(other) returns f - other, leaving both unchanged.
func (f *Frac) Minus(other *Frac) *Frac {
return f.Plus(other.Negative())
}
+//f.Inverse() returns f^-1.
func (f *Frac) Inverse() *Frac {
return &Frac{f.den, f.num, f.positive}
}
+//f.Times(other) returns f * other, leaving both unchanged.
func (f *Frac) Times(other *Frac) *Frac {
ret := &Frac{f.num * other.num, f.den * other.den, f.positive == other.positive}
ret.simplify()
return ret
}
+//f.Divided(other) returns f ÷ other, leaving both unchanged.
func (f *Frac) Divided(other *Frac) (*Frac, os.Error) {
if other.num == 0 {
return nil, DivByZero
@@ -128,6 +138,7 @@ func (f *Frac) Divided(other *Frac) (*Frac, os.Error) {
return f.Times(other.Inverse()), nil
}
+//f.Numerator() returns the (signed) numerator of f.
func (f *Frac) Numerator() int64 {
ret := int64(f.num)
if !f.positive {
@@ -136,18 +147,21 @@ func (f *Frac) Numerator() int64 {
return ret
}
+//f.Denominator() returns the (always positive) denominator of f.
func (f *Frac) Denominator() int64 {
return int64(f.den)
}
-func (f *Frac) Float() float {
- ret := float(f.num) / float(f.den)
+//f.Float64() returns a Float64 approximation of f.
+func (f *Frac) Float64() float64 {
+ ret := float64(f.num) / float64(f.den)
if !f.positive {
ret *= -1
}
return ret
}
+//f.Mixed() returns a string like "2 1/3".
func (f *Frac) Mixed() string {
if f.positive {
return fmt.Sprintf("%d %d/%d", f.num/f.den, f.num%f.den, f.den)
Please sign in to comment.
Something went wrong with that request. Please try again.