base

package module
v0.0.0-...-3775ac2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 9, 2018 License: MIT Imports: 1 Imported by: 0

README

base

For outputting numbers of arbitrary bases.

Make use of a web demo at https://tusf.github.io/base/

Documentation

Overview

This package implements a formatter useful for displaying numbers of arbitrary bases.

Usage

A formatter has methods for outputting a number to a string. The base/dozenal package has some default formatters for outputting numbers in base-12, otherwise known as dozenal.

var num int64 = 123
fmt.Printf("Decimal: %d\nDozenal: %s", num, dozenal.ASCII.Int64(num))

Output:

Decimal: 123
Dozenal: X3

Notice that when using fmt, you need to use the "s" verb, because the formmater already gives you a string as output.

New Formatter

If you want different numerals, such as if you have your own digits for ten and eleven in Dozenal or even if you want to map 0-9 to something unique as well, you can use NewFormatter() to create a formatter.

mybase := base.NewFormatter([]string{"a","b","c","d","e","f","g","h","i","j","k","l","m"})
fmt.Println(mybase.Int64(100)) // Output: "hj"

Notice in this example that the chosen base is the len of the slice; in this case base 13.

Big

The formatter also has methods for the arbitrarily large numbers implemented in math/big. BigInt() and BigRat() accept a big.Int and big.Rat respectively, calculate it, and output it in the desired base. This allows displaying arbitrarily large integers and fractions.

big.Rat.String() outputs a number in the "a/b" format, however the formatter goes to the effort of evaluating that expression. Thus, the following example:

r := big.NewRat(1,7) // Represents a rational number of 1/7
fmt.Println(r)
fmt.Println(dozenal.ASCII.BigRat(r))

Output:

1/7
0;[186X35]

Because big.Rat represents a rational number, the fractional form will eventually terminate or loop. In this case, 1/7 is an infinite loop of 6 repeating digits, represented within square-brackets.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Formatter

type Formatter struct {
	// contains filtered or unexported fields
}

Formatter can be created to utilize sets of digits and bases that are atypical.

func NewFormatter

func NewFormatter(slice []string) Formatter

Create a Formatter with the desired digits to use. The base is decided by the len() of slice.

func (Formatter) BigInt

func (z Formatter) BigInt(i *big.Int) string

Accepts a big.Int from the `math/big` package, and returns an integer as a string.

func (Formatter) BigRat

func (z Formatter) BigRat(i *big.Rat) string

Accepts a big.Rat from the `math/big` package, and returns a number as a string. If the number can be represented as a whole number, the result is returned. Non-whole numbers are represented by the whole number and fractional place-values delimetered by a `;`. If fractional place-values are repeating, the repeating digits are surrounded in brackets.

For example, using base 12, a big.Rat representing (in Decimal) 1/12 will return as "0;1", and 1/9 as "0;14". 1/10 should appear as "0;1[2497]", as the "2497" would normally repeat infinitely in dozenal.

func (Formatter) Int64

func (z Formatter) Int64(n int64) string

Accepts an int64, and returns a number as a string.

func (Formatter) UInt64

func (z Formatter) UInt64(n uint64) string

Accepts a uint64, and returns a number as a string.

Directories

Path Synopsis
A demonstration of the TUSF/base package, meant for the web browser.
A demonstration of the TUSF/base package, meant for the web browser.
This package implements a formatter useful for displaying numbers as dozenal, otherwise called duodecimal.
This package implements a formatter useful for displaying numbers as dozenal, otherwise called duodecimal.
dec2doz
Take stdin as a number, output it as dozenal.
Take stdin as a number, output it as dozenal.
time
This package displays the time using dozenal digits.
This package displays the time using dozenal digits.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL