BigSharp 2.0.1

.NET 6.0
Install-Package BigSharp -Version 2.0.1
dotnet add package BigSharp --version 2.0.1
<PackageReference Include="BigSharp" Version="2.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BigSharp --version 2.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: BigSharp, 2.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install BigSharp as a Cake Addin
#addin nuget:?package=BigSharp&version=2.0.1

// Install BigSharp as a Cake Tool
#tool nuget:?package=BigSharp&version=2.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

BigSharp

Big in C#. Port of big.js. Public domain.

A small, fast C# library for arbitrary-precision decimal arithmetic.

Documentation

Overview

The primary goal of this project is to produce a translation of big.js to C# which is as close as possible to the original implementation.

Features

  • Simple API
  • No dependencies
  • Stores values in an accessible decimal floating point format
  • Comprehensive documentation and test set

Installation

You can install BigSharp via NuGet:

package manager:

$ PM> Install-Package BigSharp

NET CLI:

$ dotnet add package BigSharp

or download source code.

Use

In the code examples below, semicolons and ToString calls are not shown.

The library exports a constructor function, Big.

A Big number is created from a primitive number, string, or other Big number.

x = new Big(123.4567)
y = new Big("123456.7e-3")
z = new Big(x)
x.Eq(y) && x.Eq(z) && y.Eq(z)          // true

In Big strict mode, creating a Big number from a primitive number is disallowed.

var bigFactory = new BigFactory(new BigConfig()
{
	STRICT = true
});
x = bigFactory.Big(1)                  // TypeError: [BigSharp] Invalid number
y = bigFactory.Big("1.0000000000000001")
y.ToNumber()                           // Error: [BigSharp] Imprecise conversion

A Big number is immutable in the sense that it is not changed by its methods.

0.3 - 0.1                              // 0.19999999999999998
x = new Big(0.3)
x.Minus(0.1)                           // "0.2"
x                                      // "0.3"

The methods that return a Big number can be chained.

x.Div(y).Plus(z).Times(9).Minus("1.234567801234567e+8").Plus(976.54321).Div("2598.11772")
x.Sqrt().Div(y).Pow(3).Gt(y.Mod(z))    // true

There are ToExponential, ToFixed and ToPrecision methods.

x = new Big(255.5)
x.ToExponential(5)                     // "2.55500e+2"
x.ToFixed(5)                           // "255.50000"
x.ToPrecision(5)                       // "255.50"

The arithmetic methods always return the exact result except Div, Sqrt and Pow (with negative exponent), as these methods involve division.

The maximum number of decimal places and the rounding mode used to round the results of these methods is determined by the value of the DP and RM properties of the Big number factory.

var bigFactory = new BigFactory(new BigConfig()
{
	DP = 10,
	RM = RoundingMode.ROUND_HALF_UP
});

x = bigFactory.Big(2);
y = bigFactory.Big(3);
z = x.Div(y)                           // "0.6666666667"
z.Sqrt()                               // "0.8164965809"
z.Pow(-3)                              // "3.3749999995"
z.Times(z)                             // "0.44444444448888888889"
z.Times(z).Round(10)                   // "0.4444444445"

The value of a Big number is stored in a decimal floating point format in terms of a coefficient, exponent and sign.

x = new Big(-123.456);
x.c                                    // [1,2,3,4,5,6]    coefficient (i.e. significand)
x.e                                    // 2                exponent
x.s                                    // -1               sign

For advanced usage, multiple Big number factories can be created, each with an independent configuration.

For further information see the API reference documentation.

System requirements

BNSharp supports:

  • Net 6

Development and testing

Make sure to rebuild projects every time you change code for testing.

Testing

To run tests:

$ dotnet test

Contributors

XeroXP.

Product Versions
.NET net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on BigSharp:

Package Downloads
BigSharp.ToFormat

Adds a ToFormat instance method to Big.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.1 69 7/12/2022
2.0.0 51 7/12/2022
1.0.0 67 6/23/2022