Browse Source

Rounder: use Example do display examples

speter 11 years ago
parent
commit
5a2ef2d475
3 changed files with 74 additions and 148 deletions
  1. 0 1
      dec.go
  2. 2 147
      rounder.go
  3. 72 0
      rounder_example_test.go

+ 0 - 1
dec.go

@@ -39,7 +39,6 @@ package inf
 
 
 // TODO:
 // TODO:
 //  - avoid excessive deep copying (quo and rounders)
 //  - avoid excessive deep copying (quo and rounders)
-//  - use Round (not Quo) in rounding examples
 
 
 import (
 import (
 	"fmt"
 	"fmt"

+ 2 - 147
rounder.go

@@ -8,6 +8,8 @@ import (
 // result of a division to a finite Dec. It is used by Dec.Round() and
 // result of a division to a finite Dec. It is used by Dec.Round() and
 // Dec.Quo().
 // Dec.Quo().
 //
 //
+// See the Example for results of using each Rounder with some sample values.
+//
 type Rounder rounder
 type Rounder rounder
 
 
 type rounder interface {
 type rounder interface {
@@ -54,184 +56,37 @@ var RoundExact Rounder = roundExact
 // RoundDown rounds towards 0; that is, returns the Dec with the greatest
 // RoundDown rounds towards 0; that is, returns the Dec with the greatest
 // absolute value not exceeding that of the result represented by quo and rem.
 // absolute value not exceeding that of the result represented by quo and rem.
 //
 //
-// The following table shows examples of the results for
-// QuoRound(x, y, scale, RoundDown).
-//
-//      x      y    scale   result
-//  ------------------------------
-//    -1.8    10        1     -0.1
-//    -1.5    10        1     -0.1
-//    -1.2    10        1     -0.1
-//    -1.0    10        1     -0.1
-//    -0.8    10        1     -0.0
-//    -0.5    10        1     -0.0
-//    -0.2    10        1     -0.0
-//     0.0    10        1      0.0
-//     0.2    10        1      0.0
-//     0.5    10        1      0.0
-//     0.8    10        1      0.0
-//     1.0    10        1      0.1
-//     1.2    10        1      0.1
-//     1.5    10        1      0.1
-//     1.8    10        1      0.1
-//
 var RoundDown Rounder = roundDown
 var RoundDown Rounder = roundDown
 
 
 // RoundUp rounds away from 0; that is, returns the Dec with the smallest
 // RoundUp rounds away from 0; that is, returns the Dec with the smallest
 // absolute value not smaller than that of the result represented by quo and
 // absolute value not smaller than that of the result represented by quo and
 // rem.
 // rem.
 //
 //
-// The following table shows examples of the results for
-// QuoRound(x, y, scale, RoundUp).
-//
-//      x      y    scale   result
-//  ------------------------------
-//    -1.8    10        1     -0.2
-//    -1.5    10        1     -0.2
-//    -1.2    10        1     -0.2
-//    -1.0    10        1     -0.1
-//    -0.8    10        1     -0.1
-//    -0.5    10        1     -0.1
-//    -0.2    10        1     -0.1
-//     0.0    10        1      0.0
-//     0.2    10        1      0.1
-//     0.5    10        1      0.1
-//     0.8    10        1      0.1
-//     1.0    10        1      0.1
-//     1.2    10        1      0.2
-//     1.5    10        1      0.2
-//     1.8    10        1      0.2
-//
 var RoundUp Rounder = roundUp
 var RoundUp Rounder = roundUp
 
 
 // RoundHalfDown rounds to the nearest Dec, and when the remainder is 1/2, it
 // RoundHalfDown rounds to the nearest Dec, and when the remainder is 1/2, it
 // rounds to the Dec with the lower absolute value.
 // rounds to the Dec with the lower absolute value.
 //
 //
-// The following table shows examples of the results for
-// QuoRound(x, y, scale, RoundHalfDown).
-//
-//      x      y    scale   result
-//  ------------------------------
-//    -1.8    10        1     -0.2
-//    -1.5    10        1     -0.1
-//    -1.2    10        1     -0.1
-//    -1.0    10        1     -0.1
-//    -0.8    10        1     -0.1
-//    -0.5    10        1     -0.0
-//    -0.2    10        1     -0.0
-//     0.0    10        1      0.0
-//     0.2    10        1      0.0
-//     0.5    10        1      0.0
-//     0.8    10        1      0.1
-//     1.0    10        1      0.1
-//     1.2    10        1      0.1
-//     1.5    10        1      0.1
-//     1.8    10        1      0.2
-//
 var RoundHalfDown Rounder = roundHalfDown
 var RoundHalfDown Rounder = roundHalfDown
 
 
 // RoundHalfUp rounds to the nearest Dec, and when the remainder is 1/2, it
 // RoundHalfUp rounds to the nearest Dec, and when the remainder is 1/2, it
 // rounds to the Dec with the greater absolute value.
 // rounds to the Dec with the greater absolute value.
 //
 //
-// The following table shows examples of the results for
-// QuoRound(x, y, scale, RoundHalfUp).
-//
-//      x      y    scale   result
-//  ------------------------------
-//    -1.8    10        1     -0.2
-//    -1.5    10        1     -0.2
-//    -1.2    10        1     -0.1
-//    -1.0    10        1     -0.1
-//    -0.8    10        1     -0.1
-//    -0.5    10        1     -0.1
-//    -0.2    10        1     -0.0
-//     0.0    10        1      0.0
-//     0.2    10        1      0.0
-//     0.5    10        1      0.1
-//     0.8    10        1      0.1
-//     1.0    10        1      0.1
-//     1.2    10        1      0.1
-//     1.5    10        1      0.2
-//     1.8    10        1      0.2
-//
 var RoundHalfUp Rounder = roundHalfUp
 var RoundHalfUp Rounder = roundHalfUp
 
 
 // RoundHalfEven rounds to the nearest Dec, and when the remainder is 1/2, it
 // RoundHalfEven rounds to the nearest Dec, and when the remainder is 1/2, it
 // rounds to the Dec with even last digit.
 // rounds to the Dec with even last digit.
 //
 //
-// The following table shows examples of the results for
-// QuoRound(x, y, scale, RoundHalfEven).
-//
-//      x      y    scale   result
-//  ------------------------------
-//    -1.8    10        1     -0.2
-//    -1.5    10        1     -0.2
-//    -1.2    10        1     -0.1
-//    -1.0    10        1     -0.1
-//    -0.8    10        1     -0.1
-//    -0.5    10        1     -0.0
-//    -0.2    10        1     -0.0
-//     0.0    10        1      0.0
-//     0.2    10        1      0.0
-//     0.5    10        1      0.0
-//     0.8    10        1      0.1
-//     1.0    10        1      0.1
-//     1.2    10        1      0.1
-//     1.5    10        1      0.2
-//     1.8    10        1      0.2
-//
 var RoundHalfEven Rounder = roundHalfEven
 var RoundHalfEven Rounder = roundHalfEven
 
 
 // RoundFloor rounds towards negative infinity; that is, returns the greatest
 // RoundFloor rounds towards negative infinity; that is, returns the greatest
 // Dec not exceeding the result represented by quo and rem.
 // Dec not exceeding the result represented by quo and rem.
 //
 //
-// The following table shows examples of the results for
-// QuoRound(x, y, scale, RoundFloor).
-//
-//      x      y    scale   result
-//  ------------------------------
-//    -1.8    10        1     -0.2
-//    -1.5    10        1     -0.2
-//    -1.2    10        1     -0.2
-//    -1.0    10        1     -0.1
-//    -0.8    10        1     -0.1
-//    -0.5    10        1     -0.1
-//    -0.2    10        1     -0.1
-//     0.0    10        1      0.0
-//     0.2    10        1      0.0
-//     0.5    10        1      0.0
-//     0.8    10        1      0.0
-//     1.0    10        1      0.1
-//     1.2    10        1      0.1
-//     1.5    10        1      0.1
-//     1.8    10        1      0.1
-//
 var RoundFloor Rounder = roundFloor
 var RoundFloor Rounder = roundFloor
 
 
 // RoundCeil rounds towards positive infinity; that is, returns the
 // RoundCeil rounds towards positive infinity; that is, returns the
 // smallest Dec not smaller than the result represented by quo and rem.
 // smallest Dec not smaller than the result represented by quo and rem.
 //
 //
-// The following table shows examples of the results for
-// QuoRound(x, y, scale, RoundCeil).
-//
-//      x      y    scale   result
-//  ------------------------------
-//    -1.8    10        1     -0.1
-//    -1.5    10        1     -0.1
-//    -1.2    10        1     -0.1
-//    -1.0    10        1     -0.1
-//    -0.8    10        1     -0.0
-//    -0.5    10        1     -0.0
-//    -0.2    10        1     -0.0
-//     0.0    10        1      0.0
-//     0.2    10        1      0.1
-//     0.5    10        1      0.1
-//     0.8    10        1      0.1
-//     1.0    10        1      0.1
-//     1.2    10        1      0.2
-//     1.5    10        1      0.2
-//     1.8    10        1      0.2
-//
 var RoundCeil Rounder = roundCeil
 var RoundCeil Rounder = roundCeil
 
 
 var intSign = []*big.Int{big.NewInt(-1), big.NewInt(0), big.NewInt(1)}
 var intSign = []*big.Int{big.NewInt(-1), big.NewInt(0), big.NewInt(1)}

+ 72 - 0
rounder_example_test.go

@@ -0,0 +1,72 @@
+package inf_test
+
+import (
+	"fmt"
+	"os"
+	"text/tabwriter"
+
+	"speter.net/go/exp/math/dec/inf"
+)
+
+// This example displays the results of Dec.Round with each of the Rounders.
+//
+func ExampleRounder() {
+	var vals = []struct {
+		x string
+		s inf.Scale
+	}{
+		{"-0.18", 1}, {"-0.15", 1}, {"-0.12", 1}, {"-0.10", 1},
+		{"-0.08", 1}, {"-0.05", 1}, {"-0.02", 1}, {"0.00", 1},
+		{"0.02", 1}, {"0.05", 1}, {"0.08", 1}, {"0.10", 1},
+		{"0.12", 1}, {"0.15", 1}, {"0.18", 1},
+	}
+
+	var rounders = []struct {
+		name    string
+		rounder inf.Rounder
+	}{
+		{"RoundDown", inf.RoundDown}, {"RoundUp", inf.RoundUp},
+		{"RoundCeil", inf.RoundCeil}, {"RoundFloor", inf.RoundFloor},
+		{"RoundHalfDown", inf.RoundHalfDown}, {"RoundHalfUp", inf.RoundHalfUp},
+		{"RoundHalfEven", inf.RoundHalfEven}, {"RoundExact", inf.RoundExact},
+	}
+
+	fmt.Println("The results of new(inf.Dec).Round(x, s, inf.RoundXXX):\n")
+	w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.AlignRight)
+	fmt.Fprint(w, "x\ts\t|\t")
+	for _, r := range rounders {
+		fmt.Fprintf(w, "%s\t", r.name[5:])
+	}
+	fmt.Fprintln(w)
+	for _, v := range vals {
+		fmt.Fprintf(w, "%s\t%d\t|\t", v.x, v.s)
+		for _, r := range rounders {
+			x, _ := new(inf.Dec).SetString(v.x)
+			z := new(inf.Dec).Round(x, v.s, r.rounder)
+			fmt.Fprintf(w, "%d\t", z)
+		}
+		fmt.Fprintln(w)
+	}
+	w.Flush()
+
+	// Output:
+	// The results of new(inf.Dec).Round(x, s, inf.RoundXXX):
+	//
+	//      x s | Down   Up Ceil Floor HalfDown HalfUp HalfEven Exact
+	//  -0.18 1 | -0.1 -0.2 -0.1  -0.2     -0.2   -0.2     -0.2 <nil>
+	//  -0.15 1 | -0.1 -0.2 -0.1  -0.2     -0.1   -0.2     -0.2 <nil>
+	//  -0.12 1 | -0.1 -0.2 -0.1  -0.2     -0.1   -0.1     -0.1 <nil>
+	//  -0.10 1 | -0.1 -0.1 -0.1  -0.1     -0.1   -0.1     -0.1  -0.1
+	//  -0.08 1 |  0.0 -0.1  0.0  -0.1     -0.1   -0.1     -0.1 <nil>
+	//  -0.05 1 |  0.0 -0.1  0.0  -0.1      0.0   -0.1      0.0 <nil>
+	//  -0.02 1 |  0.0 -0.1  0.0  -0.1      0.0    0.0      0.0 <nil>
+	//   0.00 1 |  0.0  0.0  0.0   0.0      0.0    0.0      0.0   0.0
+	//   0.02 1 |  0.0  0.1  0.1   0.0      0.0    0.0      0.0 <nil>
+	//   0.05 1 |  0.0  0.1  0.1   0.0      0.0    0.1      0.0 <nil>
+	//   0.08 1 |  0.0  0.1  0.1   0.0      0.1    0.1      0.1 <nil>
+	//   0.10 1 |  0.1  0.1  0.1   0.1      0.1    0.1      0.1   0.1
+	//   0.12 1 |  0.1  0.2  0.2   0.1      0.1    0.1      0.1 <nil>
+	//   0.15 1 |  0.1  0.2  0.2   0.1      0.1    0.2      0.2 <nil>
+	//   0.18 1 |  0.1  0.2  0.2   0.1      0.2    0.2      0.2 <nil>
+
+}