Documentation ¶
Overview ¶
Package customnumber provides the ability to create custom positional numeral systems in an efficient and performant way. You can create custom numbers based on custom numeral systems and use them at will.
Each digit represented as a circular list that contains the all the possible numerals.
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 := customnumber.NewNumber(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 Number ¶
type Number struct {
// contains filtered or unexported fields
}
Number represents a custom number that is consisted by its digits and digit values.
func AbsDifference ¶
AbsDifference returns the absolute difference between two custom numbers
Example ¶
package main import ( "fmt" customnumber "github.com/slysterous/numerals" ) 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 := customnumber.NewFromDecimal(testValues, 22) if err != nil { //handle the error } num2, err := customnumber.NewFromDecimal(testValues, 12) if err != nil { //handle error } num3, err := customnumber.AbsDifference(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 custom number from a decimal integer.
Example ¶
package main import ( "fmt" customnumber "github.com/slysterous/numerals" ) 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, _ := customnumber.NewFromDecimal(testValues, 100) fmt.Printf("custom number: %v", number) }
Output:
func NewNumber ¶
NewNumber initializes a CustomNumber by providing the initial number in strings along with the possible values that each digit can have.
Example ¶
package main import ( "fmt" customnumber "github.com/slysterous/numerals" ) 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, _ := customnumber.NewNumber(testValues, "100") fmt.Printf("custom number: %v", number) }
Output:
func Sum ¶
Sum sums 2 custom numbers 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" customnumber "github.com/slysterous/numerals" ) 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 := customnumber.NewNumber(testValues, "100") if err != nil { //handle the error } number2, err := customnumber.NewNumber(testValues, "100") if err != nil { //handle the error } sum, err := customnumber.Sum(testValues, *number1, *number2) if err != nil { //handle the error } fmt.Printf("sum is: %s", sum.String()) }
Output:
func (*Number) Add ¶
Add adds a number to the already existing number
Example ¶
package main import ( customnumber "github.com/slysterous/numerals" ) 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 := customnumber.NewNumber(testValues, "123z") if err != nil { //handle the error } num2, err := customnumber.NewFromDecimal(testValues, 1) if err != nil { //handle error } err = number.Add(*num2) if err != nil { //handle error } }
Output:
func (*Number) Decimal ¶
Decimal converts a custom number to a decimal integer.
Example ¶
package main import ( "fmt" customnumber "github.com/slysterous/numerals" ) 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, _ := customnumber.NewNumber(testValues, "100") dec := number.Decimal() fmt.Printf("%d", dec) }
Output: 1296
func (*Number) Decrement ¶
Decrement performs a -1 to the Number.
Example ¶
package main import ( "fmt" customnumber "github.com/slysterous/numerals" ) 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, _ := customnumber.NewNumber(testValues, "1230") err := number.Decrement() if err != nil { // do whatever you need with the error } fmt.Printf(number.String()) }
Output: 122z
func (*Number) Increment ¶
Increment performs a +1 to the Number.
Example ¶
package main import ( "fmt" customnumber "github.com/slysterous/numerals" ) 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, _ := customnumber.NewNumber(testValues, "123z") number.Increment() fmt.Printf(number.String()) }
Output: 1240
func (Number) String ¶
String prints a string representation of Number.
Example ¶
package main import ( "fmt" customnumber "github.com/slysterous/numerals" ) 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 := customnumber.NewFromDecimal(testValues, 2021) if err != nil { //handle the err } fmt.Printf("custom number as a string representation: %s", number.String()) }
Output: