Struct FloatRange
Represents an inclusive minimum/maximum range of single-precision floating-point values. Useful for designer-authored data such as price variance, randomized cooldowns, FOV limits, and any other parameter where a value must fall between two bounds.
Implements
Inherited Members
Namespace: Scylla.Core.Util.Ranges
Assembly: ScyllaCore.dll
Syntax
[Serializable]
public struct FloatRange : IEquatable<FloatRange>
Remarks
FloatRange is a value type (struct) and is fully serializable
by Unity's Inspector via [Serializable] and [SerializeField]. It can
be used as a Unity Inspector field directly, and a custom UI Toolkit drawer renders
a min/max two-field row by default and a MinMaxSlider when an additional
UnityEngine.RangeAttribute declares the outer bounds.
The range is inclusive on both ends: Contains(float) returns
true when the input is greater than or equal to Min and
less than or equal to Max. The constructor does not validate that
Min <= Max; consumers that require this invariant
must check explicitly. Use IsEmpty and IsValid to
inspect the configured bounds.
Random() returns a value drawn from UnityEngine.Random.Range,
where the upper bound is inclusive for float overloads. To use a custom
random source for determinism, sample externally and use Lerp(float)
to map a normalized t value to the range.
Equality uses approximate floating-point comparison via
Approximately(float, float, float), which tolerates the small
rounding errors that accumulate during arithmetic on float values.
Constructors
FloatRange(float, float)
Initializes a new FloatRange with the supplied min
and max bounds. The constructor performs no ordering check;
callers responsible for the invariant Min <= Max must validate the inputs
explicitly. Non-finite values are rejected.
Declaration
public FloatRange(float min, float max)
Parameters
| Type | Name | Description |
|---|---|---|
| float | min | The lower bound (inclusive). |
| float | max | The upper bound (inclusive). |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown when either argument is NaN or infinite. |
Fields
NegativeOneToOne
A FloatRange spanning -1f to 1f, inclusive. Common for
signed normalized parameters such as input axis values.
Declaration
public static readonly FloatRange NegativeOneToOne
Field Value
| Type | Description |
|---|---|
| FloatRange |
Zero
A FloatRange with both Min and Max set
to 0f. Useful as a sentinel value or default empty range.
Declaration
public static readonly FloatRange Zero
Field Value
| Type | Description |
|---|---|
| FloatRange |
ZeroToOne
A FloatRange spanning 0f to 1f, inclusive. Common for
normalized parameters such as alpha values, weights, and probabilities.
Declaration
public static readonly FloatRange ZeroToOne
Field Value
| Type | Description |
|---|---|
| FloatRange |
Properties
IsEmpty
Returns true when Min equals Max exactly,
using Approximately(float, float, float) for the comparison.
Declaration
public bool IsEmpty { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsValid
Declaration
public bool IsValid { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Max
The upper bound of this range (inclusive).
Declaration
public float Max { get; }
Property Value
| Type | Description |
|---|---|
| float |
Min
The lower bound of this range (inclusive).
Declaration
public float Min { get; }
Property Value
| Type | Description |
|---|---|
| float |
Size
The size of the range, equal to Max minus Min. May be negative when the range is invalid (Min > Max).
Declaration
public float Size { get; }
Property Value
| Type | Description |
|---|---|
| float |
Methods
Clamp(float)
Clamps value to the inclusive bounds of this range using
Clamp(float, float, float).
Declaration
public float Clamp(float value)
Parameters
| Type | Name | Description |
|---|---|---|
| float | value | The value to clamp. |
Returns
| Type | Description |
|---|---|
| float | The clamped value. |
Contains(float)
Returns true when value is within the inclusive bounds
of this range.
Declaration
public bool Contains(float value)
Parameters
| Type | Name | Description |
|---|---|---|
| float | value | The value to test. |
Returns
| Type | Description |
|---|---|
| bool |
|
Equals(FloatRange)
Determines whether two FloatRange values are approximately equal by comparing both Min and Max via Approximately(float, float, float).
Declaration
public bool Equals(FloatRange other)
Parameters
| Type | Name | Description |
|---|---|---|
| FloatRange | other | The other range to compare with. |
Returns
| Type | Description |
|---|---|
| bool |
|
Equals(object)
Determines whether the specified object is a FloatRange that is approximately equal to this instance.
Declaration
public override bool Equals(object obj)
Parameters
| Type | Name | Description |
|---|---|---|
| object | obj | The object to compare with. |
Returns
| Type | Description |
|---|---|
| bool |
|
Overrides
GetHashCode()
Returns a hash code for this range based on the raw bit representations of Min and Max. Two values considered approximately equal by Equals(FloatRange) may produce different hash codes when their raw bit representations differ.
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| int | A 32-bit signed integer hash code. |
Overrides
InverseLerp(float)
Computes the normalized position of value within the range,
mapping Min to 0 and Max to 1. Returns
0 when the range is empty (Min equals Max).
Declaration
public float InverseLerp(float value)
Parameters
| Type | Name | Description |
|---|---|---|
| float | value | The value to project onto the normalized [0, 1] interval. |
Returns
| Type | Description |
|---|---|
| float | The normalized position. Values outside the range produce results outside [0, 1]; callers may clamp explicitly using Clamp01(float). |
Lerp(float)
Linearly interpolates between Min and Max using
t as the interpolation factor. Delegates to
Lerp(float, float, float), which clamps t
to the range [0, 1] before interpolation.
Declaration
public float Lerp(float t)
Parameters
| Type | Name | Description |
|---|---|---|
| float | t | The interpolation factor; clamped to [0, 1]. |
Returns
| Type | Description |
|---|---|
| float | The interpolated value. |
Random()
Returns a uniformly distributed random value drawn from this range using
Range(float, float), which is inclusive on both
ends for float overloads.
Declaration
public float Random()
Returns
| Type | Description |
|---|---|
| float |
ToString()
Returns a human-readable string representation of this range using the invariant culture for number formatting.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| string | A string in the format |
Overrides
Operators
operator ==(FloatRange, FloatRange)
Returns true when left is approximately equal to
right, delegating to Equals(FloatRange).
Declaration
public static bool operator ==(FloatRange left, FloatRange right)
Parameters
| Type | Name | Description |
|---|---|---|
| FloatRange | left | The left-hand operand. |
| FloatRange | right | The right-hand operand. |
Returns
| Type | Description |
|---|---|
| bool |
|
operator !=(FloatRange, FloatRange)
Returns true when left is not approximately equal to
right.
Declaration
public static bool operator !=(FloatRange left, FloatRange right)
Parameters
| Type | Name | Description |
|---|---|---|
| FloatRange | left | The left-hand operand. |
| FloatRange | right | The right-hand operand. |
Returns
| Type | Description |
|---|---|
| bool |
|