All Projects → TylerBrinkley → Genumerics

TylerBrinkley / Genumerics

Licence: MIT license
Genumerics is a high-performance .NET library for generic numeric operations

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to Genumerics

Golang Examples
Some examples for the programming language Go.
Stars: ✭ 14 (-12.5%)
Mutual labels:  generic
GenericTensor
The only library allowing to create Tensors (matrices extension) with custom types
Stars: ✭ 42 (+162.5%)
Mutual labels:  generic
Observable
A generic ObservableObject for every property!
Stars: ✭ 41 (+156.25%)
Mutual labels:  generic
remark-directive
remark plugin to support directives
Stars: ✭ 137 (+756.25%)
Mutual labels:  generic
numeric
numeric facilities for C++ 14; dual numbers, dual quaternions, constrained numbers, intervals
Stars: ✭ 21 (+31.25%)
Mutual labels:  numeric
dart sealed
Dart and Flutter sealed class generator and annotations, with match methods and other utilities. There is also super_enum compatible API.
Stars: ✭ 16 (+0%)
Mutual labels:  generic
Stdlib
✨ Standard library for JavaScript and Node.js. ✨
Stars: ✭ 2,749 (+17081.25%)
Mutual labels:  numeric
TableViewExtension
This extension simplify registering any cell, reusing and other verbosity steps.
Stars: ✭ 13 (-18.75%)
Mutual labels:  generic
EntitasGenericAddon
Addon to Entitas that allows using generic methods instead of code generator and uses type inference to insure compile time correctness
Stars: ✭ 17 (+6.25%)
Mutual labels:  generic
omnipersistence
Utilities for JPA, JDBC and DataSources
Stars: ✭ 24 (+50%)
Mutual labels:  generic
Arduino-Queue.h
Generic C++ circular queue for Arduino embedded projects.
Stars: ✭ 59 (+268.75%)
Mutual labels:  generic
use-color-change
📈📉React hook for flashing a text when a value becomes higher or lower
Stars: ✭ 32 (+100%)
Mutual labels:  numeric
Processor
Ontology-driven Linked Data processor and server for SPARQL backends. Apache License.
Stars: ✭ 54 (+237.5%)
Mutual labels:  generic
generic-for-core
🏗️ Generic Repository & UOW Pattern For ASP.NET Core
Stars: ✭ 55 (+243.75%)
Mutual labels:  generic
react-native-card-button
Fully customizable Card Button via Paraboly for React Native.
Stars: ✭ 16 (+0%)
Mutual labels:  generic
NumericUpDownLib
Implements numeric up down WPF controls to edit/display values (byte, integer, short, ushort etc.) with a textbox and optional up/down arrow (repeat) buttons. Value editing is possible by dragging the mouse vertically/horizontally, clicking up/down buttons, using up/down or left right cursor keys, spinning mousewheel on mouseover, or editing th…
Stars: ✭ 68 (+325%)
Mutual labels:  numeric
nativescript-numeric-keyboard
🔢 Replace the meh default number/phone keyboard with this stylish one
Stars: ✭ 33 (+106.25%)
Mutual labels:  numeric
generic
generic streamlink plugin
Stars: ✭ 18 (+12.5%)
Mutual labels:  generic
Generic-SQL-Audit-Trail
A generic audit trail based on triggers and dynamic SQL.
Stars: ✭ 15 (-6.25%)
Mutual labels:  generic
RangeTree
A generic interval tree implementation in C#
Stars: ✭ 144 (+800%)
Mutual labels:  generic

GitHub last commit (master) NuGet Version NuGet Downloads

Genumerics

Genumerics is a high-performance .NET library for generic numeric operations. It is compatible with .NET Framework 4.5+ and .NET Standard 2.0+.

The Problem

You may have come across while working in .NET where you would like to perform numeric operations over a generic numeric type. Unfortunately .NET doesn't provide a way to do that natively.

This library fills that gap by providing most standard numeric operations for the following built-in numeric types and their nullable equivalents with the ability to add support for other numeric types.

sbyte, byte, short, ushort, int, uint, long, ulong, float, double, decimal, nint, nuint, and BigInteger

Genumerics Demo

Below is a demo of some basic uses of Genumerics in the form of unit tests.

using Genumerics;
using NUnit.Framework;

public class GenumericsDemo
{
    [TestCase(4, 5, 9)]
    [TestCase(2.25, 6.75, 9.0)]
    public void Add<T>(T left, T right, T expected)
    {
        Assert.AreEqual(expected, Number.Add(left, right));
        Assert.AreEqual(expected, AddWithOperator<T>(left, right));
    }

    private T AddWithOperator<T>(Number<T> left, Number<T> right) => left + right;

    [TestCase(9, 6, true)]
    [TestCase(3.56, 4.07, false)]
    public void GreaterThan<T>(T left, T right, bool expected)
    {
        Assert.AreEqual(expected, Number.GreaterThan(left, right));
        Assert.AreEqual(expected, GreaterThanWithOperator<T>(left, right));
    }

    private bool GreaterThanWithOperator<T>(Number<T> left, Number<T> right) => left > right;

    [TestCase(4, 4.0)]
    [TestCase(27.0, 27)]
    public void Convert<TFrom, TTo>(TFrom value, TTo expected)
    {
        Assert.AreEqual(expected, Number.Convert<TFrom, TTo>(value));
        Assert.AreEqual(expected, Number.Create(value).To<TTo>());
    }

    [TestCase("98765", 98765)]
    [TestCase("12.3456789", 12.3456789)]
    public void Parse<T>(string value, T expected)
    {
        Assert.AreEqual(expected, Number.Parse<T>(value));
    }

    [TestCase(123, null, "123")]
    [TestCase(255, "X", "FF")]
    public void ToString<T>(T value, string? format, string expected)
    {
        Assert.AreEqual(expected, Number.ToString(value, format));
    }

    [TestCase(sbyte.MaxValue)]
    [TestCase(float.MaxValue)]
    public void MaxValue<T>(T expected)
    {
        Assert.AreEqual(expected, Number.MaxValue<T>());
    }
}

Performance Comparison

The summation algorithms below were benchmarked in .NET Core 3.0 and .NET 4.8 to determine the relative performance of the library compared with an int specific algorithm. As can be seen in the results, the performance is equivalent.

Results

Method Mean Error StdDev Ratio
Sum 531.0 ns 1.40 ns 1.31 ns 1.00
SumNumber 521.2 ns 1.41 ns 1.32 ns 0.98
SumNumber2 531.7 ns 0.81 ns 0.76 ns 1.00
SumAdd 530.1 ns 1.04 ns 0.97 ns 1.00
SumAdd2 532.2 ns 1.60 ns 1.33 ns 1.00

Code

using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using Genumerics;

public class Program
{
    static void Main() => BenchmarkRunner.Run<SumBenchmarks<int, DefaultNumericOperations>>();
}

[SimpleJob(RuntimeMoniker.Net461), SimpleJob(RuntimeMoniker.NetCoreApp30), LegacyJitX86Job]
public class SumBenchmarks<T, TNumericOperations>
    where TNumericOperations : struct, INumericOperations<T>
{
    private int[] _intItems;
    private T[] _items;

    [Benchmark(Baseline = true)]
    public int Sum()
    {
        int sum = 0;
        foreach (int item in _intItems)
        {
            sum += item;
        }
        return sum;
    }

    [Benchmark]
    public T SumNumber()
    {
        Number<T> sum = default;
        foreach (T item in _items)
        {
            sum += item;
        }
        return sum;
    }

    [Benchmark]
    public T SumNumber2()
    {
        Number<T, TNumericOperations> sum = default;
        foreach (T item in _items)
        {
            sum += item;
        }
        return sum;
    }

    [Benchmark]
    public T SumAdd()
    {
        T sum = default;
        foreach (T item in _items)
        {
            sum = Number.Add(sum, item);
        }
        return sum;
    }

    [Benchmark]
    public T SumAdd2()
    {
        T sum = default;
        TNumericOperations operations = default;
        foreach (T item in _items)
        {
            sum = operations.Add(sum, item);
        }
        return sum;
    }

    [GlobalSetup]
    public void Setup()
    {
        _intItems = new int[1000];
        _items = new T[1000];
        Random rand = new Random();
        for (int i = 0; i < 1000; ++i)
        {
            int value = rand.Next(10);
            _intItems[i] = value;
            _items[i] = Number.Create(value).To<T>();
        }
    }
}

Interface

See fuget for exploring the interface.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].