Giter Site home page Giter Site logo

vgrudenic / grisu.net Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kring/grisu.net

0.0 2.0 0.0 1.98 MB

Grisu is a fast, new (2010) algorithm for converting a floating-point number to a decimal string. It was introduced in a paper by Florian Loitsch (http://dl.acm.org/citation.cfm?doid=1809028.1806623) and the author claims it to be as much as 4 times faster than previous techniques. The code here is a port of the C++ code in the "double-conversion" project on Google Code (http://code.google.com/p/double-conversion/) to C#.

License: BSD 3-Clause "New" or "Revised" License

C# 15.88% HTML 74.37% Batchfile 0.01% JavaScript 0.18% XSLT 0.15% CSS 0.75% C++ 3.21% Visual Basic 5.44%

grisu.net's Introduction

Grisu is a fast, new algorithm for converting a floating-point number to a decimal string. It was introduced in a paper by Florian Loitsch in 2010 (http://dl.acm.org/citation.cfm?doid=1809028.1806623) and it as much as four times faster than previous techniques.

The code here is a port of the C++ code in the "double-conversion" project on Google Code (http://code.google.com/p/double-conversion/) to C#. My focus is on very fast JSON serialization of floating point numbers, however, so I eliminated all the options and configurability.

Instead of offering options, my port aims to:

  • Guarantee that the generated strings can be parsed back to an identical double. It uses the fewest digits possible while still achieving this.
  • Produce the most compact JSON-compatible strings possible by choosing regular decimal or scientific notation, whichever is shorter.
  • Be really, really fast.

Using this library is simple, because it really has only one method.

StringBuilder builder = new StringBuilder();
GrisuDotNet.Grisu.DoubleToString(1.23, builder);
Assert.AreEqual("1.23", builder.ToString());

In my tests on a 64-bit version of .NET 4, this library writes doubles to a StringBuilder over two times faster than StringBuilder's default serialization of doubles, while guaranteeing that the strings round-trip back to the same doubles. When the "R" format specifier is used with Double.ToString to guarantee that a double round-trips, this library produces output over four times faster and also generates more compact output.

If you make any improvements to this code - especially performance improvements - please send me a pull request.

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.