frac

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2023 License: Unlicense Imports: 2 Imported by: 0

README

Overview

Missing feature of the Go standard library: parsing and formatting int64 as a fractional numeric string, without any rounding or bignums, by using a fixed fraction size. Supports arbitrary radixes from 2 to 36.

For example:

"123"     <- frac 2, radix 10 -> 123_00
"123"     <- frac 3, radix 10 -> 123_000

"123.45"  <- frac 2, radix 10 -> 123_45
"123.45"  <- frac 3, radix 10 -> 123_450

"123.456" <- frac 2, radix 10 -> <error>
"123.456" <- frac 3, radix 10 -> 123_456

Performance on 64-bit machines is somewhat comparable to strconv and shouldn't be your bottleneck.

See API docs at https://pkg.go.dev/github.com/mitranim/frac.

Why

  • You use integers for money.
  • You deal with external APIs that use decimal strings for money.
  • You want to avoid rounding errors.
  • You don't want to deal with "big decimal" libraries.

Then frac is for you!

Usage

Basic usage:

import "github.com/mitranim/frac"

func main() {
  num, err := frac.ParseDec(`-123`, 2)
  assert(err == nil && num == -123_00)

  num, err = frac.ParseDec(`-123.00`, 2)
  assert(err == nil && num == -123_00)

  num, err = frac.ParseDec(`-123.45`, 2)
  assert(err == nil && num == -123_45)

  // Exponent exceeds allotted precision. Conversion is impossible.
  num, err = frac.ParseDec(`-123.456`, 2)
  assert(err != nil && num == 0)
}

func assert(ok bool) {if !ok {panic("unreachable")}}

Implementing a monetary type:

import "github.com/mitranim/frac"

type Cents int64

func (self *Cents) UnmarshalText(input []byte) error {
  num, err := frac.UnmarshalDec(input, 2)
  if err != nil {
    return err
  }
  *self = Cents(num)
  return nil
}

func (self Cents) MarshalText() ([]byte, error) {
  return frac.AppendDec(nil, int64(self), 2)
}

The resulting type Cents is an integer, but when decoding and encoding text, it's represented as a fractional with 2 decimal points.

Known Limitations

  • The code is too assembly-like. Kinda like the standard library.

  • No special support for unsigned integers.

  • When formatting, fractional precision is limited to 64. (Imagine allocating gigabytes of memory for 0.0...01.)

License

https://unlicense.org

Misc

I'm receptive to suggestions. If this library almost satisfies you but needs changes, open an issue or chat me up. Contacts: https://mitranim.com/#contacts

Documentation

Overview

Missing feature of the Go standard library: parsing and formatting integers as fractional numeric strings, without any rounding or bignums, by using a fixed fraction size. Supports arbitrary radixes from 2 to 36.

See `readme.md` for examples.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append(buf []byte, num int64, frac uint, radix uint) ([]byte, error)

Same as `Format` but appends the resulting text to the provided buffer, returning the resulting union. When there's an error, the buffer is returned as-is with no hidden modifications.

func AppendBin

func AppendBin(buf []byte, num int64, frac uint) ([]byte, error)

Shortcut for `Append(buf, num, frac, 2)`.

func AppendDec

func AppendDec(buf []byte, num int64, frac uint) ([]byte, error)

Shortcut for `Append(buf, num, frac, 10)`.

func AppendHex

func AppendHex(buf []byte, num int64, frac uint) ([]byte, error)

Shortcut for `Append(buf, num, frac, 16)`.

func AppendOct

func AppendOct(buf []byte, num int64, frac uint) ([]byte, error)

Shortcut for `Append(buf, num, frac, 8)`.

func Format

func Format(num int64, frac uint, radix uint) (string, error)

Formats an integer as a fractional number, virtually "divided" by the given fractional precision.

For example, for `frac = 2, radix = 10`, the number 12345 is encoded as "123.45", while the number 12300 is encoded as simply "123".

func FormatBin

func FormatBin(num int64, frac uint) (string, error)

Shortcut for `Format(num, frac, 2)`.

func FormatDec

func FormatDec(num int64, frac uint) (string, error)

Shortcut for `Format(num, frac, 10)`.

func FormatHex

func FormatHex(num int64, frac uint) (string, error)

Shortcut for `Format(num, frac, 16)`.

func FormatOct

func FormatOct(num int64, frac uint) (string, error)

Shortcut for `Format(num, frac, 8)`.

func Parse

func Parse(src string, frac uint, radix uint) (num int64, err error)

Parses a string that represents a fractional number, with an optional leading "+" or "-", into an integer whose value is virtually "multiplied" by the provided fractional precision.

For example, for `frac = 2, radix = 10`, "123.45" is parsed into the number 12345, while "123.456" is rejected with an error because it exceeds the allotted precision.

See `readme.md` for examples.

func ParseBin

func ParseBin(src string, frac uint) (int64, error)

Shortcut for `Parse(src, frac, 2)`.

func ParseDec

func ParseDec(src string, frac uint) (int64, error)

Shortcut for `Parse(src, frac, 10)`.

Example
package main

import (
	"github.com/mitranim/frac"
)

func main() {
	num, err := frac.ParseDec(`-123`, 2)
	assert(err == nil && num == -123_00)

	num, err = frac.ParseDec(`-123.00`, 2)
	assert(err == nil && num == -123_00)

	num, err = frac.ParseDec(`-123.45`, 2)
	assert(err == nil && num == -123_45)

	// Exponent exceeds allotted precision. Conversion is impossible.
	num, err = frac.ParseDec(`-123.456`, 2)
	assert(err != nil && num == 0)
}

func assert(ok bool) {
	if !ok {
		panic("unreachable")
	}
}

func ParseHex

func ParseHex(src string, frac uint) (int64, error)

Shortcut for `Parse(src, frac, 16)`.

func ParseOct

func ParseOct(src string, frac uint) (int64, error)

Shortcut for `Parse(src, frac, 8)`.

func Unmarshal

func Unmarshal(src []byte, frac uint, radix uint) (int64, error)

Same as `Parse` but takes a byte slice.

func UnmarshalBin

func UnmarshalBin(src []byte, frac uint) (int64, error)

Shortcut for `UnmarshalBin(src, frac, 2)`.

func UnmarshalDec

func UnmarshalDec(src []byte, frac uint) (int64, error)

Shortcut for `UnmarshalDec(src, frac, 10)`.

func UnmarshalHex

func UnmarshalHex(src []byte, frac uint) (int64, error)

Shortcut for `UnmarshalHex(src, frac, 16)`.

func UnmarshalOct

func UnmarshalOct(src []byte, frac uint) (int64, error)

Shortcut for `UnmarshalOct(src, frac, 8)`.

Types

This section is empty.

Jump to

Keyboard shortcuts

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