GithubHelp home page GithubHelp logo

cyberflamego / decimal Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ericmj/decimal

0.0 2.0 0.0 515 KB

Arbitrary precision decimal arithmetic

Home Page: https://hexdocs.pm/decimal/

License: Apache License 2.0

Elixir 100.00%

decimal's Introduction

Decimal

Build Status

Arbitrary precision decimal arithmetic.

Usage

Add Decimal as a dependency in your mix.exs file:

def deps do
  [{:decimal, "~> 2.0"}]
end

Next, run mix deps.get in your shell to fetch and compile Decimal. Start an interactive Elixir shell with iex -S mix:

iex> alias Decimal, as: D
iex> D.add(6, 7)
#Decimal<13>
iex> D.div(1, 3)
#Decimal<0.333333333>
iex> D.new("0.33")
#Decimal<0.33>

Examples

Using the context

The context specifies the maximum precision of the result of calculations and the rounding algorithm if the result has a higher precision than the specified maximum. It also holds the list of set of trap enablers and the current set flags.

The context is stored in the process dictionary. You don't have to pass the context around explicitly and the flags will be updated automatically.

The context is accessed with Decimal.Context.get/0 and set with Decimal.Context.set/1. It can be set temporarily with Decimal.Context.with/2.

iex> D.Context.get()
%Decimal.Context{flags: [:rounded, :inexact], precision: 9, rounding: :half_up,
 traps: [:invalid_operation, :division_by_zero]}

iex> D.Context.with(%D.Context{precision: 2}, fn -> IO.inspect D.Context.get() end)
%Decimal.Context{flags: [], precision: 2, rounding: :half_up,
 traps: [:invalid_operation, :division_by_zero]}
%Decimal.Context{flags: [], precision: 2, rounding: :half_up,
 traps: [:invalid_operation, :division_by_zero]}

iex> D.Context.set(%D.Context{D.Context.get() | traps: []})
:ok

iex> D.Context.get()
%Decimal.Context{flags: [:rounded, :inexact], precision: 9, rounding: :half_up,
 traps: []}

Precision and rounding

Use :precision option to limit the amount of decimal digits in the coefficient:

iex> D.Context.set(%D.Context{D.Context.get() | precision: 9})
:ok

iex> D.div(100, 3)
#Decimal<33.3333333>

iex> D.Context.set(%D.Context{D.Context.get() | precision: 2})
:ok

iex> D.div(100, 3)
#Decimal<33>

The :rounding option specifies the algorithm and precision of the rounding operation:

iex> D.Context.set(%D.Context{D.Context.get() | rounding: :half_up})
:ok

iex> D.div(31, 2)
#Decimal<16>

iex> D.Context.set(%D.Context{D.Context.get() | rounding: :floor})
:ok

iex> D.div(31, 2)
#Decimal<15>

Comparisons

Using comparison operators (<, =, >) with two or more decimal digits may not produce accurate result. Instead, use comparison functions.

iex> D.compare(-1, 0)
:lt
iex> D.compare(0, -1)
:gt
iex> D.compare(0, 0)
:eq

iex> D.equal?(-1, 0)
false
iex> D.equal?(0, "0.0")
true

Flags and trap enablers

When an exceptional condition is signalled, its flag is set in the current context. Decimal.Error will be raised if the trap enabler is set.

iex> D.Context.set(%D.Context{D.Context.get() | rounding: :floor, precision: 2})
:ok

iex> D.Context.get().traps
[:invalid_operation, :division_by_zero]

iex> D.Context.get().flags
[]

iex> D.div(31, 2)
#Decimal<15>

iex> D.Context.get().flags
[:inexact, :rounded]

:inexact and :rounded flag were signalled above because the result of the operation was inexact given the context's precision and had to be rounded to fit the precision. Decimal.Error was not raised because the signals' trap enablers weren't set.

iex> D.Context.set(%D.Context{D.Context.get() | traps: D.Context.get().traps ++ [:inexact]})
:ok

iex> D.div(31, 2)
** (Decimal.Error)

The default trap enablers, such as :division_by_zero, can be unset:

iex> D.Context.get().traps
[:invalid_operation, :division_by_zero]

iex> D.div(42, 0)
** (Decimal.Error)

iex> D.Context.set(%D.Context{D.Context.get() | traps: [], flags: []})
:ok

iex> D.div(42, 0)
#Decimal<Infinity>

iex> D.Context.get().flags
[:division_by_zero]

Mitigating rounding errors

TODO

License

Copyright 2013 Eric Meadows-Jönsson

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

decimal's People

Contributors

adamu avatar adrianomitre avatar adzz avatar alvinlindstam avatar andrewdryga avatar apelsinka223 avatar aptinio avatar charlesokwuagwu avatar eqmvii avatar ericmj avatar evax avatar fertapric avatar fuelen avatar jbranchaud avatar jesenko avatar jfeltesse avatar jwarwick avatar kelostrada avatar kianmeng avatar kipcole9 avatar klotzandrew avatar kronicdeth avatar lexmag avatar lukaszsamson avatar prodis avatar razuf avatar starbelly avatar tubedude avatar whatyouhide avatar wojtekmach avatar

Watchers

 avatar  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.