Skip to content

A library for handling big numbers and decimals under Mono/.NET frameworks

License

Notifications You must be signed in to change notification settings

deveel/deveel-math

Repository files navigation

NuGet NuGet Downloads Coverage

Deveel Math

This library is an opinionated port of the Java Math package provided as part of the Apache Harmony framework (now defunct), that can be used to factorize big numbers and decimals in .NET applications.

Why Deveel Math?

At the time of the development of this library, the .NET framework did not provide a native support for big numbers and decimals, and not even the System.Numerics namespace was available yet, which was limiting the operations that could be performed on big numbers.

In fact, during the development of the DeveelDB database engine, we needed a library that could handle big numbers and decimals in a more flexible way, and that could be used in a cross-platform environment.

Stil at today, the .NET framework does not provide a native support for big decimals, and the System.Numerics namespace is still limited to handling operations on big integers.

What is Deveel Math?

This is a little effort to address this gap, providing the community with a library that can be used to handle big numbers and decimals in a more flexible way.

It doesn't have any ambition to replace the System.Numerics namespace, but it can be used as a complement to it, especially when dealing with big decimals.

Given the limited knowledge of the author in the field of numerical analysis, the library is subject to reviews and any contribution to improve the quality of the code is welcome.

How to Install It

The library is available as a NuGet package, and it can be installed in any .NET application that supports the .NET 6.0 or later (prior support to .NET 4.8 and _.NET Standard 1.3 has been dropped).

The binaries are available in two deployment streams:

Type Source Package
Stable NuGet NuGet
Pre-Release GitHub Static Badge

|

To install the dmath library you can use the following command from the NuGet Package Manager Console on the root of your project:

PM> Install-Package dmath

or rather using the dotnet CLI:

$ dotnet add package dmath

Note: Since version 2.0.x the library has been migrated to .NET 6.0 and the support for .NET Standard 1.3 has been dropped.

BigDecimal

The BigDecimal class represents a big decimal number that can be used to perform arithmetic operations with arbitrary precision.

The class provides a set of methods to perform arithmetic operations, such as addition, subtraction, multiplication, division, and rounding.

Creating a BigDecimal

To create a new BigDecimal instance, you can use one of the following constructors:

// Creating an instance from an integer
var number = new BigDecimal(1234567890);

// Creating an instance from a long integer
var number = new BigDecimal(1234567890L);

// Creating an instance from a double
var number = new BigDecimal(1234567890.123456);

or rather from a string:

// Parsing a string to a big decimal
var number = BigDecimal.Parse("1234567890");

Contributing

If you want to contribute to the development of this library, you can fork the repository and submit a pull request with your changes.

Please make sure to follow the coding style and conventions used in the project, and to provide a clear description of the changes you are proposing.

Future Development

BigInteger Porting

When the library was first ported from Java, the BigInteger class was not included in the porting process, as the System.Numerics.BigInteger class was not available in the .NET framework yet, and thus we had to port also the BigInteger class from the Harmony framework.

Now that the System.Numerics.BigInteger class is available in the .NET framework, we can consider to remove the BigInteger class from the library, and to use the native class instead.

Performance Benchmarks

We should consider to add performance benchmarks to the library, to measure the performance of the arithmetic operations on big numbers and decimals, and to compare the performance of the library with the native .NET classes.

License

The library is released under the terms of the Apache License 2.0, and it is provided as-is without any warranty or support.