Coder Social home page Coder Social logo

elixir-float_pp's Introduction

FloatPP

Pretty printer for Floats written in Elixir.

Writes the shortest, correctly rounded string of decimals that converts back to a Double when parsed with String.to_float/1.

Implements the algorithm from "Printing Floating-Point Numbers Quickly and Accurately" in Proceedings of the SIGPLAN '96 Conference on Programming Language Design and Implementation.

Output format options

The output can be in either decimal or scientific format, and optionally rounded to an arbitrary number of decimal places, using one of several rounding algorithms.

Output is the shortest decimal string which can be round tripped back into the original Double value (ie without loss of precision)

Rounding algorithms are based on the definitions given in IEEE 754, but also include 2 additional options (effectively the complementary versions):

Rounding algorithms

Directed roundings:

  • :down - Round towards 0 (truncate), eg 10.9 rounds to 10.0
  • :up - Round away from 0, eg 10.1 rounds to 11.0. (Non IEEE algorithm)
  • :ceiling - Round toward +โˆž - Also known as rounding up or ceiling
  • :floor - Round toward -โˆž - Also known as rounding down or floor

Round to nearest:

  • :half_even - Round to nearest value, but in a tiebreak, round towards the nearest value with an even (zero) least significant bit, which occurs 50% of the time. This is the default for IEEE binary floating-point and the recommended value for decimal.
  • :half_up - Round to nearest value, but in a tiebreak, round away from 0. This is the default algorithm for Erlang's Kernel.round/2
  • :half_down - Round to nearest value, but in a tiebreak, round towards 0 (Non IEEE algorithm)

Examples

iex> FloatPP.to_string(12.3456, %{decimals: 3, compact: true, rounding: :half_even})
"12.346"

iex> FloatPP.to_string(12.3456, %{decimals: 6, compact: false, rounding: :half_even})
"12.345600"

iex> FloatPP.to_string(12.3456, %{scientific: 3, compact: true, rounding: :ceiling})
"1.234e+01"

iex> FloatPP.to_string(12.3456, %{decimals: nil})
"12.3456"

"""

elixir-float_pp's People

Contributors

ewildgoose avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

elixir-float_pp's Issues

why isn't elixir standard keyword style?

like this would be wonderful, forget about any unnecessary parentheses, brackets, or braces

iex> FloatPP.to_string 12.3456, decimals: 3, compact: true, rounding: :half_even
"12.346"

this is currently working:

iex(18)> Float.to_string 34.0, decimals: 4, compact: false
"34.0000"

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.