Documentation ¶
Overview ¶
Package numeral provides the ability to create custom positional numeral systems in an efficient and performant way. You can create numerals based on custom numeral systems and use them at will.
Each digit represented as a circular list that contains the all the possible numeral.
Each number is represented as a doubly linked list of circular lists.
Example
// create a slice of runes. digitValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} number := numeral.NewNumeral(digitValues, "128z") // will make the number 1290. number.Increment() // will make the number 128y. number.Decrement() //will give you the string representation of the number. strnumber:=number.String()
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Numeral ¶
type Numeral struct {
// contains filtered or unexported fields
}
Numeral represents a numeral that is consisted by its digits and digit values.
func Diff ¶
Diff returns the absolute difference between two numerals
Example ¶
package main import ( "fmt" "github.com/slysterous/numeral" ) func main() { testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} num, err := numeral.NewFromDecimal(testValues, 22) if err != nil { //handle the error } num2, err := numeral.NewFromDecimal(testValues, 12) if err != nil { //handle error } num3, err := numeral.Diff(testValues, *num2, *num) if err != nil { //handle error } fmt.Printf("decimal result should be 10, number: %d", num3.Decimal()) }
Output:
func NewFromDecimal ¶
NewFromDecimal creates a numeral from a decimal integer.
Example ¶
package main import ( "fmt" "github.com/slysterous/numeral" ) func main() { testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} number, _ := numeral.NewFromDecimal(testValues, 100) fmt.Printf("numeral: %v", number) }
Output:
func NewNumeral ¶
NewNumeral initializes a numeral by providing the initial number in strings along with the possible values that each digit can have.
Example ¶
package main import ( "fmt" "github.com/slysterous/numeral" ) func main() { testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} number, _ := numeral.NewNumeral(testValues, "100") fmt.Printf("numeral: %v", number) }
Output:
func Sum ¶
Sum sums 2 numerals into a 3rd one. Values are needed to define the new system under which the number will be displayed. Every number can be from a different system.
Example ¶
package main import ( "fmt" "github.com/slysterous/numeral" ) func main() { testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} number1, err := numeral.NewNumeral(testValues, "100") if err != nil { //handle the error } number2, err := numeral.NewNumeral(testValues, "100") if err != nil { //handle the error } sum, err := numeral.Sum(testValues, *number1, *number2) if err != nil { //handle the error } fmt.Printf("sum is: %s", sum.String()) }
Output:
func (*Numeral) Add ¶
Add adds a number to the already existing number
Example ¶
package main import ( "github.com/slysterous/numeral" ) func main() { testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} number, err := numeral.NewNumeral(testValues, "123z") if err != nil { //handle the error } num2, err := numeral.NewFromDecimal(testValues, 1) if err != nil { //handle error } err = number.Add(*num2) if err != nil { //handle error } }
Output:
func (*Numeral) Decimal ¶
Decimal converts a numeral to a decimal integer.
Example ¶
package main import ( "fmt" "github.com/slysterous/numeral" ) func main() { testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} number, _ := numeral.NewNumeral(testValues, "100") dec := number.Decimal() fmt.Printf("%d", dec) }
Output: 1296
func (*Numeral) Decrement ¶
Decrement performs a -1 to the Numeral.
Example ¶
package main import ( "fmt" "github.com/slysterous/numeral" ) func main() { testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} number, _ := numeral.NewNumeral(testValues, "1230") err := number.Decrement() if err != nil { // do whatever you need with the error } fmt.Printf(number.String()) }
Output: 122z
func (*Numeral) Increment ¶
Increment performs a +1 to the Numeral.
Example ¶
package main import ( "fmt" "github.com/slysterous/numeral" ) func main() { testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} number, _ := numeral.NewNumeral(testValues, "123z") number.Increment() fmt.Printf(number.String()) }
Output: 1240
func (Numeral) String ¶
String returns a string representation of Numeral.
Example ¶
package main import ( "fmt" "github.com/slysterous/numeral" ) func main() { testValues := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} number, err := numeral.NewFromDecimal(testValues, 2021) if err != nil { //handle the err } fmt.Printf("numeral as a string representation: %s", number.String()) }
Output: