Package floatconv

import "gitlab.com/zoralab/cerigo/encoding/float/floatconv"

Package floatconv is a base numbering system converter for float numbers.

It is based on Holloway and Cory’s research efforts available at:

  1. https://doi.org/10.5281/zenodo.3579594
  2. https://gitlab.com/holloway/floatconv

User can use floatconv package directly or through encoding/float package.

Constants

Normalization constants are enumerations ID for adjusting the output to the desired presentation.

const (
	// NormalizeNormal is to normalize toward single digit round
	// number.
	//

	// Example: 1. XXXX.YYYYeZZZ --> X.XXXYYYYe(ZZZ+3)
	NormalizeNormal = 0

	// NormalizeRound is to normalize toward full round numbers.
	//

	// Example: 1. XXXX.YYYYeZZZ --> XXXXYYYY.0e(ZZZ-4)
	NormalizeRound = 1

	// NormalizeNone is to not apply any normalization.
	//

	// Example: 1. XXXX.YYYYeZZZ --> XXXX.YYYYeZZZ
	NormalizeNone = 2
)

Error messages are the list of strings used for generating Error object.

const (
	// ErrorBaseOutOfRange is the given base value is outside of
	// the supported range. It requires user to provide base
	// number within 2 to 36.
	ErrorBaseOutOfRange = "base must be in the range of 2 โ‰ฅ b โ‰ฅ 36"

	// ErrorConvertBaseNotUsable is the converter is not usable
	// for base conversion. It requires to perform proper parsing
	// again.
	ErrorConvertBaseNotUsable = "unable to convert invalid data"
)

Converter

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

Converter is the object structure meant for performing base conversion.

It is safe to create using &struct{} mechanism.

Func (c *Converter) ConvertBase(base int, normalization int, precisionLimit int) (t *Text, err error)

Func (c *Converter) ParseISO6093(number string) error

ParseISO6093 is to accept ISO6093 compliant string format as input.

If the given string is invalid, this function returns an error and the converter is not usable, waiting for the next parsing operation.

Func (c *Converter) ParseText(t *Text) (err error)

ParseText is to accept a pre-filled *Text data structure as input.

If the given Text data structure is invalid, ParseText(…) will return an error and the converter is not usable, waiting for the next parsing operation.

Text

type Text struct {
	Sign         string
	Round        string
	Fraction     string
	Base         string
	ExponentSign string
	Exponent     string
}

Text is a data structure holding float’s subcomponents' value as string.

It has 6 segments that can be re-constructed using String() function.

  1. Text.Sign - the float positive/negative sign
  2. Text.Round - the round value (XXXX.0)
  3. Text.Fraction - the fractional value (0.XXXX)
  4. Text.Base - the exponent base number in decimal value
  5. Text.ExponentSign - the positive/negative sign for exponent value.
  6. Text.Exponent - the exponent value (B^(ยฑXXXX) in base10.

Func (t *Text) String() string

String reconstructs the float’s subcomponents into a single string output.

Due to the multiple base runes limitations, String() only present the [Scientific] format. See “FORMAT” for explanation.

FORMAT

  1. Symbol: -dddd.dddd*B^(ยฑdd)
  2. Fields: [Sign][Round].[Fraction]*[Base]^([ExponentSign][Exponent])

SYMBOL LEGENDS

    • = negative sign only. Missing if positive.
  1. d = digits (0|1|2|3|4|5|6|7|8|9|a|…|z).
    • = multiplication sign ( ‘x’ in mathematics).
  2. B = base number representing exponent symbol.
  3. ยฑ = positive or negative sign.
  4. … = variable length of repeating characters, before and after.
  5. . = decimal spot that separates round value and fractional value.