https://github.com/ktsu-dev/semanticquantity
https://github.com/ktsu-dev/semanticquantity
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/ktsu-dev/semanticquantity
- Owner: ktsu-dev
- License: mit
- Created: 2024-06-03T06:01:22.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-13T06:29:16.000Z (about 1 year ago)
- Last Synced: 2025-04-23T00:14:25.220Z (about 1 year ago)
- Language: PowerShell
- Size: 282 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
- Authors: AUTHORS.md
Awesome Lists containing this project
README
# ktsu.SemanticQuantity





## Overview
The `ktsu.SemanticQuantity` library provides a base class for creating semantic quantities with a specific storage type. This allows for defining quantities with meaningful semantics, such as length, mass, time, etc., and performing arithmetic operations on them while preserving their semantics.
## Features
- Define semantic quantities with meaningful semantics.
- Perform arithmetic operations on semantic quantities.
- Support for a wide range of numeric types for storage.
- Integration with .NET numeric interfaces.
## Table of Contents
- [Installation](#installation)
- [Usage](#usage)
- [Creating a Semantic Quantity](#creating-a-semantic-quantity)
- [Arithmetic Operations](#arithmetic-operations)
- [Comparison Operations](#comparison-operations)
- [Supported Numeric Types](#supported-numeric-types)
- [API Reference](#api-reference)
- [Contributing](#contributing)
- [License](#license)
## Installation
To install the `ktsu.SemanticQuantity` library, you can use the .NET CLI:
```sh
dotnet add package ktsu.SemanticQuantity
```
Or, add the package reference directly in your project file:
```xml
```
## Usage
### Creating a Semantic Quantity
You can create a semantic quantity from various numeric types using the `Create` method:
```csharp
using ktsu.SemanticQuantity;
using System.Numerics;
// Define a specific semantic quantity type
public record Length : SemanticQuantity;
var length = Length.Create(123.45);
Console.WriteLine(length.Quantity); // Output: 123.45
```
### Arithmetic Operations
You can perform various arithmetic operations on semantic quantities:
```csharp
var length1 = Length.Create(100.0);
var length2 = Length.Create(200.0);
var sum = length1 + length2;
var difference = length1 - length2;
var product = length1 * 2.0;
var quotient = length2 / 2.0;
var ratio = length2 / length1;
```
### Supported Numeric Types
The `SemanticQuantity`class supports a wide range of numeric types for storage through the`INumber` interface. The following types are supported:
- `int`
- `long`
- `short`
- `sbyte`
- `uint`
- `ulong`
- `ushort`
- `byte`
- `BigInteger`
- `double`
- `float`
- `Half`
- `decimal`
## API Reference
### `SemanticQuantity`
- `TStorage Quantity { get; protected set; }`: Gets the stored quantity value.
#### Methods
- `static TQuantity Create(TStorage quantity)`: Creates a new instance of the specified `SemanticQuantity` type with the given quantity.
### `SemanticQuantity`
- Inherits from `SemanticQuantity` and implements various arithmetic operators.
#### Methods
- `static TResult Multiply(SemanticQuantity self, SemanticQuantity other)`: Multiplies two semantic quantities and returns the result as a new instance of the specified type.
- `static TResult Multiply(SemanticQuantity self, TStorage other)`: Multiplies a semantic quantity by a scalar value and returns the result as a new instance of the specified type.
- `static TResult Divide(SemanticQuantity self, SemanticQuantity other)`: Divides one semantic quantity by another and returns the result as a new instance of the specified type.
- `static TResult Divide(SemanticQuantity self, TStorage other)`: Divides a semantic quantity by a scalar value and returns the result as a new instance of the specified type.
- `static TStorage DivideToStorage(SemanticQuantity self, SemanticQuantity other)`: Divides one semantic quantity by another and returns the result as a scalar value.
- `static TResult Add(SemanticQuantity self, SemanticQuantity other)`: Adds two semantic quantities and returns the result as a new instance of the specified type.
- `static TResult Subtract(SemanticQuantity self, SemanticQuantity other)`: Subtracts one semantic quantity from another and returns the result as a new instance of the specified type.
- `static TResult Negate(SemanticQuantity self)`: Negates a semantic quantity and returns the result as a new instance of the specified type.
### Operators
- `static TSelf operator +(SemanticQuantity left, TSelf right)`: Adds two semantic quantities.
- `static TSelf operator -(SemanticQuantity left, TSelf right)`: Subtracts one semantic quantity from another.
- `static TSelf operator -(SemanticQuantity value)`: Negates a semantic quantity.
- `static TSelf operator *(SemanticQuantity left, TStorage right)`: Multiplies a semantic quantity by a scalar value.
- `static TSelf operator /(SemanticQuantity left, TStorage right)`: Divides a semantic quantity by a scalar value.
- `static TStorage operator /(SemanticQuantity left, SemanticQuantity right)`: Divides one semantic quantity by another.
## Contributing
Contributions are welcome! Please feel free to submit a pull request or open an issue.
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.