Beryllium.Math 0.2.1

Prefix Reserved
dotnet add package Beryllium.Math --version 0.2.1
                    
NuGet\Install-Package Beryllium.Math -Version 0.2.1
                    
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="Beryllium.Math" Version="0.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Beryllium.Math" Version="0.2.1" />
                    
Directory.Packages.props
<PackageReference Include="Beryllium.Math" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Beryllium.Math --version 0.2.1
                    
#r "nuget: Beryllium.Math, 0.2.1"
                    
#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.
#:package Beryllium.Math@0.2.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Beryllium.Math&version=0.2.1
                    
Install as a Cake Addin
#tool nuget:?package=Beryllium.Math&version=0.2.1
                    
Install as a Cake Tool

BerylliumMath

A small, dependency-free set of math helpers for .NET 10: tolerance-aware comparisons, clamping, angle conversion, interpolation and remapping, frame-rate independent damping, and a guarded look-rotation builder on System.Numerics types.

Everything lives in one static class, Beryllium.Mathematics.MathUtils.

Install

dotnet add package BerylliumMath

Targets net10.0. No package dependencies.

Comparison

Single-precision helpers use an absolute tolerance of 1e-6, double-precision helpers 1e-10. Every comparison method has a float and a double overload.

Method Notes
IsZero(a) Absolute value below the tolerance.
IsOne(a), IsMinusOne(a) Approximately 1 or -1.
AreEqual(a, b), AreNotEqual(a, b) Hybrid tolerance: absolute near zero, relative to the larger magnitude elsewhere, so large values a few ULPs apart still compare equal.
IsLessOrEqual(a, b), IsGreaterOrEqual(a, b) Ordered comparison with the same tolerance on the equality side.

NaN never compares equal to anything, itself included. Two same-signed infinities are equal.

Clamping

ClampToNonNegative(value) for int, float and double. NaN is propagated rather than clamped to zero.

Conversions

ToDegrees(radians) and ToRadians(degrees) for float and double. The single-precision radians-to-degrees factor is the correctly rounded float nearest 180/π; 180.0f / MathF.PI evaluates one ULP low.

Interpolation and remapping

These helpers are single-precision only.

Method Notes
InverseLerp(min, max, current) Fraction of current between min and max. Unclamped; 0 for a degenerate range.
Remap(current, oldMin, oldMax, newMin, newMax) Maps a value between ranges. Unclamped; newMin for a degenerate source range.
SecondsToDampingCoefficient(convergenceFraction, durationSec) Turns "cover this fraction of the remaining distance in this many seconds" into a damping coefficient.
InverseExpLerpAmount(dampingCoefficient, elapsedSeconds) The lerp amount of an exponential approach after elapsedSeconds, i.e. 1 - exp(-k * dt).

The last two together give frame-rate independent smoothing:

// Reach 95% of the way to the target in 0.18 seconds, whatever the frame rate.
var damping = MathUtils.SecondsToDampingCoefficient(0.95f, 0.18f);

// Each frame:
var amount = MathUtils.InverseExpLerpAmount(damping, elapsedSeconds);
current = float.Lerp(current, target, amount);

convergenceFraction is clamped to [1e-4, 0.9999], since a fraction of exactly 1 would need an infinite coefficient, and durationSec to at least 1e-4, so a zero or negative duration behaves as a near-instant snap rather than producing an infinite or negative coefficient. NaN in either argument propagates.

Orientation

LookRotation(forward, up) builds the unit System.Numerics.Quaternion whose local -Z axis points along forward, with its local +Y axis along the part of up perpendicular to forward. That is the right-handed, "-Z is forward" convention shared by System.Numerics and MonoGame. Neither input needs to be unit length, and even components too large to square in single precision are handled. LookRotation(forward) uses world +Y as the up reference.

It never returns NaN, whatever the input:

  • A forward shorter than 1e-6, or with a NaN or infinite component, returns Quaternion.Identity.
  • An up that is zero, NaN or infinite, or within about 0.8° of the forward axis, cannot define a frame, so world +Y is substituted. When forward itself is (nearly) vertical, world +Z is used looking up and world -Z looking down instead, which gives the default view pitched straight up or down.

The fallback cannot follow yaw, so a camera driven by LookRotation(forward) snaps when pitched within about 0.8° of straight up or down, by the angle between its heading and world -Z; clamp the pitch short of that.

using System.Numerics;
using Beryllium.Mathematics;

var rotation = MathUtils.LookRotation(target - position, Vector3.UnitY);
var facing = Vector3.Transform(-Vector3.UnitZ, rotation); // ≈ Vector3.Normalize(target - position)

MonoGame users can pass the components through new System.Numerics.Vector3(v.X, v.Y, v.Z) and read the result back with new Quaternion(q.X, q.Y, q.Z, q.W); the component semantics are identical.

Building and testing

dotnet test BerylliumMath.sln

Changelog

  • 0.2.1 LookRotation no longer returns NaN for an infinite forward or one too large to square in single precision (beyond about 1.8e19), and honours a very large up instead of replacing it with world +Y. Looking (nearly) straight up now gives the default view pitched up, as looking down already did, rather than that view rolled by 180°.
  • 0.2.0 Added LookRotation.
  • 0.1.0 Renamed the Mathematics class to MathUtils.

License

MIT

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Beryllium.Math:

Package Downloads
Beryllium.ShapingFunctions

A collection of 44 shaping (easing) functions for animation, interpolation and signal shaping.

Beryllium.Audio

Beryllium engine audio manager

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.1 114 9/25/2026