Giter Site home page Giter Site logo

zig-parsefloat's Introduction

An implementation of fmt.parseFloat for zig.

This is intended to be a robust, fast and accurate float parser to replace the existing code in std.

For a detail on the methods used, see:

A good corpus of test data can be found at:

This is a fairly direct port of dec2flt found in the rust tree:

This was chosen for a few reasons, but primarily because the concepts map fairly well to Zig and a lot of the work here has been backported to the reference C++ implementation.

High-Level

The main entrypoint is parseFloat(comptime T: type, s: []const u8) !T.

This function will:

  1. Parse the number into mantissa/exponent/negative a. If the number was special (nan/inf), return immediately b. If the number could not be parsed, returns an error
  2. Attempt to convert via a fast path, where the mantissa/exponent is directly representable by a machine-sized float.
  3. If not succesful, attempt to convert using the eisel-lemire algorithm. This will work for ~99% of cases.
  4. If not succesful, convert using a big decimal type. This will always be work.

Performance Testing

This repo vendors https://github.com/tiehuis/simple_fastfloat_benchmark.

cmake -B build simple_fastfloat_benchmark
cmake --build build
./simple_fastfloat_benchmark/run_bench.sh

If you make changes to the zig parseFloat function in this repo. Perform the following to update the benchmark program.

./update_zig_ftoa_benchmark_lib # Update the vendored library
cmake --build build
./simple_fastfloat_benchmark/run_bench.sh

TODO

Before merging, complete the following:

  • Allow underscores according to zig spec in floating point literals
  • support for hex-floats in parseFloat directly (currently uses parseHexFloat separate function).
  • Fix eisel-lemire algorithm for f16 (1 in 5 million test cases failing).
  • Consider f128 support mechanism. Likely push back to a later commit.

f128 support

Implemented in https://github.com/tiehuis/zig-parsefloat/commit/db7bfff803d000a781ca54f82b8f8b3f27ebbd19.

zig-parsefloat's People

Contributors

tiehuis avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.