SIUnitsArithmetic 1.2.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package SIUnitsArithmetic --version 1.2.5                
NuGet\Install-Package SIUnitsArithmetic -Version 1.2.5                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SIUnitsArithmetic" Version="1.2.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SIUnitsArithmetic --version 1.2.5                
#r "nuget: SIUnitsArithmetic, 1.2.5"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SIUnitsArithmetic as a Cake Addin
#addin nuget:?package=SIUnitsArithmetic&version=1.2.5

// Install SIUnitsArithmetic as a Cake Tool
#tool nuget:?package=SIUnitsArithmetic&version=1.2.5                

SI Units library with arithmetic capabilities

SIUnitsArithmetic defines metric system units and provides related arithmetic operations including unit to unit multiplication even with compound units of any combination of basic units, resulting in higher or lower order units (m², 1/m or m/s).

Derived units compund of any combination of basic units (length, time, mass) are supported, including Newton and Joule are also included.

The demand for such libraries has grown alongside the development of engineering programs that require specific numbers with specified units. My solution eliminates the challenge of managing units by allowing you to focus on the metric types, such as MetricLength or MetricTime. The library takes care of the underlying mathematics, seamlessly handling unit conversions and enabling operations like unit to unit multiplication, resulting in higher or lower order units like m² or 1/m. Consider the Square class below:

Square sq = new Square(2.5.km(), 4.3.km(), 10.005.m());
Console.WriteLine(sq.Volume());

class Square
{
    MetricLength _length;
    MetricLength _width;
    MetricLength -height;

    public Square(MetricLength l, MetricLength w, MetricLength h)
    {
        _length = l; _width = w; _height = h;
    }
    public Metric Volume()
    {
        return _length * _width * _height;
    }
}

The units of the inputs of the constructor won't be a problem anymore. The same class can handle the values with various units (mm, m, cm) and return the same result with correct unit.

Extensiblity

Now custom units can be defined by the users of this library. Example of a Customunit:


// This unit has to be registered
// call this method only once in the application
// derived degree indicates the exponents of length, time and mass unit components of the custom derived unit.
CustomUnit.RegisterSpecialUnit(new DerivedDegree(2, -2, 2), CustomUnit.Instance);

class CustomUnit : CustomSpecialUnit<CustomUnit>
{
    // use new modifier
    public new string Symbol { get; } = "custom";
    CustomUnit(MetricLength l_unit, MetricTime t_unit, MetricMass m_unit, double scaler) : base(l_unit, t_unit, m_unit, scaler)
    
    }
    // override this method
    protected override CustomUnit New(MetricLength l_unit, MetricTime t_unit, MetricMass m_unit)
    {
        return new CustomUnit(l_unit, t_unit, m_unit, 1);
    }
    // define a static Instance method
    public static CustomUnit Instance(MetricLength l_unit, MetricTime t_unit, MetricMass m_unit)
    {
        return new CustomUnit(l_unit, t_unit, m_unit, 1);
    }
}

Examples

// now supports derived unit arithmetics and special units Joule and newton.
var speed = 2.km() / 1.hour();
var acc = speed / 1.hour();
var f2 = acc * 80.kg();
var e1 = f2 * 500.mm();
Joule e2 = (Joule)(f1 * 10.m());
Assert.IsTrue(e1 is Joule); //true
Assert.IsTrue(f2 is Newton); //true
Assert.IsTrue(e2 is Joule); //true

var speed = 1.m() / 1.second()
// 1 m/s

3.kg() / (2.m() * 2.second(2));
// 0,75 kg/m.s²

5.m(2) * 2.second(3) / 3.kg(2);
//10,000000000000002 m².s³/kg²

var m1 = 2.m();
// 2 m

m1.dcm()
// 200 cm

var s1 = 3.hour();
// 3 s

s1.minute()
// 180 m

s1.second()
// 10800 s

var m12 = (2.mm() * 10.cm() + 4.m(2)).dm();
// 400,02000000000004 dm2

var m13 = 2 * m12;
// 800,0400000000001 dm2

var m14 = (m12 / 2).m();
// 2,0001 m²

Operations Operants Result
+ Metric(n) + Metric(n) => Metric(n) where n is degree
- Metric(n) - Metric(n) => Metric(n) where n is degree
* Metric(n) * Metric(m) => Metric(n) where n and m are degrees
/ Metric(n) / Metric(m) => Metric(n-m) where n and m are degrees
* n * Metric(m) => Metric(m) where n is double
/ n / Metric(m) => Metric(-m) where n is double
* Metric(m) * n => Metric(m) where n is double
/ Metric(m) / n => Metric(m) where n is double

Note: zero degree metrics [Metric(0)] are equal to scalers.

Conversions

MetricLength MetricMass MetricTime
[MetricLength].km(degree) [MetricMass].mg() [MetricTime].msec()
[MetricLength].m(degree) [MetricMass].g() [MetricTime].milisecond()
[MetricLength].dm(degree) [MetricMass].kg() [MetricTime].second()
[MetricLength].cm(degree) [MetricMass].tonne() [MetricTime].minute()
[MetricLength].mm(degree) [MetricMass].t() [MetricTime].hour()
[MetricLength].MetricLength(SiMetricUnits unit) [MetricMass].MetricMass(SiMassUnit) [MetricTime].MetricTime()

Supported Units

Units of Length Units of Time Units of Mass
yoktometre, [ym] picosecond picogram, [pg]
zeptometre, [zm] nanosecond nanogram, [ng]
attometre, [am] microsecond microgram, [µg]
femtometre, [fm] milisecond miligram, [mg]
picometre, [pm] second centigram, [cg]
nanometre, [nm] minute decigram, [dg]
micrometre, [µm] hour gram, [g]
milimetre, [mm] day decagram, [dac]
centimetre, [cm] hectogram, [hg]
decimetre, [dm] kilogram, [kg]
metre, [m] tonne, [t]
decametre, [dam] kilotonne, [kt]
hectometre, [hm] megatonne, [Mt]
kilometre, [km]
megametre, [Mm]
gigametre, [Gm]
terametre, [Tm]
petametre, [Pm]
exametre, [Em]
zettametre, [Zm]
yottametre, [Ym]

Project Repository

https://github.com/kzlsahin/SiUnitsArithmetic

Contributions are welcome.

Lisance

This package is released under the MIT licence.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.4 156 3/11/2024
1.3.3 155 8/25/2023
1.3.1 153 8/25/2023
1.3.0 140 8/23/2023
1.2.5 172 8/1/2023

- Bug of multiplication and division of derivedUnits with double type values is fixed.
- Special derived units Newton and Joule are added.
- Extensibility is added.
- DerivedUnit arithmetics are improved.
- Unit tests coverage is extended.
- Logic comparison operators are added.