refloat

package module
v0.0.0-...-f41276e Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: BSD-3-Clause Imports: 4 Imported by: 0

README

Refloat

Go Reference

Float parser that sacrifices nothing.

gopher.png

Features

  • Accurate. It finds the "best" approximation to the input just like the standard library, strconv. Fuzzing tests in addition to standard library tests and parse-number-fxx-test-data are actively done.

  • Compatible. Basically, it is an improvement on ParseFloat in the standard library, and the usage is exactly the same.

  • Fast. Faster than the standard library on benchmarks with normally distributed floats and bitwise uniform random float inputs. For more information, benchmark it yourself or see below.

Installation

go get github.com/sugawarayuuta/refloat

Benchmarks

gantt
title norm = normally distributed, bits = bitwise uniform (ns/op - lower is better)
todayMarker off
dateFormat  X
axisFormat %s
section bits 64bit 
strconv: 0,106
refloat: 0,91
section norm 64bit 
strconv: 0,92
refloat: 0,65
section bits 32bit 
strconv: 0,79
refloat: 0,80
section norm 32bit 
strconv: 0,65
refloat: 0,49

Articles

There are articles written in English and Japanese.

Thank you

The icon above is from gopher-stickers by Ueda Takuya.

Awesome talk and inspirational algorithm by Daniel Lemire.

Sollya is used for precomputing the polynomial for approximations.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRange indicates that a value is out of range for the target type.
	ErrRange = errors.New("value out of range")
	// ErrSyntax indicates that a value does not have the right syntax for the target type.
	ErrSyntax = errors.New("invalid syntax")
)

Functions

func ParseFloat

func ParseFloat(num string, size int) (float64, error)

ParseFloat converts the string num to a floating-point number with the precision specified by size: 32 for float32, or 64 for float64. When size=32, the result still has type float64, but it will be convertible to float32 without changing its value.

ParseFloat accepts decimal and hexadecimal floating-point numbers as defined by the Go syntax for floating-point literals. If num is well-formed and near a valid floating-point number, ParseFloat returns the nearest floating-point number rounded using IEEE754 unbiased rounding. (Parsing a hexadecimal floating-point value only rounds when there are more bits in the hexadecimal representation than will fit in the mantissa.)

The errors that ParseFloat returns have concrete type *NumError and include err.Num = num.

If num is not syntactically well-formed, ParseFloat returns err.Err = ErrSyntax.

If num is syntactically well-formed but is more than 1/2 ULP away from the largest floating point number of the given size, ParseFloat returns f = ±Inf, err.Err = ErrRange.

ParseFloat recognizes the string "NaN", and the (possibly signed) strings "Inf" and "Infinity" as their respective special floating point values. It ignores case when matching.

Types

type NumError

type NumError struct {
	Func string // the failing function (ParseBool, ParseInt, ParseUint, ParseFloat, ParseComplex)
	Num  string // the input
	Err  error  // the reason the conversion failed (e.g. ErrRange, ErrSyntax, etc.)
}

A NumError records a failed conversion.

func (*NumError) Error

func (err *NumError) Error() string

func (*NumError) Unwrap

func (err *NumError) Unwrap() error

Jump to

Keyboard shortcuts

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