mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-11 04:52:21 +02:00
Add ability to get min/max values for generic floating point
This commit is contained in:
parent
484b9716db
commit
af7ca28707
3 changed files with 23 additions and 2 deletions
|
@ -10,11 +10,14 @@ namespace System
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
|
||||||
#unwarn
|
#unwarn
|
||||||
public struct Double : double, IFloating, ISigned, IFormattable, IHashable, ICanBeNaN, IParseable<double>
|
public struct Double : double, IFloating, ISigned, IFormattable, IHashable, ICanBeNaN, IParseable<double>, IMinMaxValue<double>
|
||||||
{
|
{
|
||||||
public const double MinValue = -1.7976931348623157E+308;
|
public const double MinValue = -1.7976931348623157E+308;
|
||||||
public const double MaxValue = 1.7976931348623157E+308;
|
public const double MaxValue = 1.7976931348623157E+308;
|
||||||
|
|
||||||
|
public static double IMinMaxValue<double>.MinValue => MinValue;
|
||||||
|
public static double IMinMaxValue<double>.MaxValue => MaxValue;
|
||||||
|
|
||||||
// Note Epsilon should be a double whose hex representation is 0x1
|
// Note Epsilon should be a double whose hex representation is 0x1
|
||||||
// on little endian machines.
|
// on little endian machines.
|
||||||
public const double Epsilon = 4.9406564584124654E-324;
|
public const double Epsilon = 4.9406564584124654E-324;
|
||||||
|
|
|
@ -3,7 +3,7 @@ using System.Globalization;
|
||||||
namespace System
|
namespace System
|
||||||
{
|
{
|
||||||
#unwarn
|
#unwarn
|
||||||
struct Float : float, IFloating, ISigned, IFormattable, IHashable, IEquatable<float>, ICanBeNaN, IParseable<float>
|
struct Float : float, IFloating, ISigned, IFormattable, IHashable, IEquatable<float>, ICanBeNaN, IParseable<float>, IMinMaxValue<float>
|
||||||
{
|
{
|
||||||
public const float MinValue = (float)-3.40282346638528859e+38;
|
public const float MinValue = (float)-3.40282346638528859e+38;
|
||||||
public const float Epsilon = (float)1.4e-45;
|
public const float Epsilon = (float)1.4e-45;
|
||||||
|
@ -12,6 +12,9 @@ namespace System
|
||||||
public const float NegativeInfinity = -1.0f / 0.0f;
|
public const float NegativeInfinity = -1.0f / 0.0f;
|
||||||
public const float NaN = 0.0f / 0.0f;
|
public const float NaN = 0.0f / 0.0f;
|
||||||
|
|
||||||
|
public static float IMinMaxValue<float>.MinValue => MinValue;
|
||||||
|
public static float IMinMaxValue<float>.MaxValue => MaxValue;
|
||||||
|
|
||||||
// We use this explicit definition to avoid the confusion between 0.0 and -0.0.
|
// We use this explicit definition to avoid the confusion between 0.0 and -0.0.
|
||||||
public const float NegativeZero = (float)-0.0;
|
public const float NegativeZero = (float)-0.0;
|
||||||
|
|
||||||
|
|
|
@ -50,5 +50,20 @@ namespace Tests
|
||||||
FloatParseErrTest("6E+");
|
FloatParseErrTest("6E+");
|
||||||
FloatParseErrTest("6e+");
|
FloatParseErrTest("6e+");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void MinMaxTest<T>(T expectedMinValue, T expectedMaxValue)
|
||||||
|
where T : IMinMaxValue<T>
|
||||||
|
where int : operator T <=> T
|
||||||
|
{
|
||||||
|
Test.Assert(T.MinValue == expectedMinValue);
|
||||||
|
Test.Assert(T.MaxValue == expectedMaxValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public static void TestMinMax()
|
||||||
|
{
|
||||||
|
MinMaxTest<float>(Float.MinValue, Float.MaxValue);
|
||||||
|
MinMaxTest<double>(Double.MinValue, Double.MaxValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue