1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 20:42:21 +02:00

Merge pull request #1929 from rzuckerm/min-max

Add ability to get min/max value generically for numeric types
This commit is contained in:
Brian Fiete 2024-02-13 12:04:27 +01:00 committed by GitHub
commit ba463cc5d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 95 additions and 12 deletions

View file

@ -10,11 +10,14 @@ namespace System
using System.Diagnostics;
#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 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
// on little endian machines.
public const double Epsilon = 4.9406564584124654E-324;

View file

@ -3,7 +3,7 @@ using System.Globalization;
namespace System
{
#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 Epsilon = (float)1.4e-45;
@ -12,6 +12,9 @@ namespace System
public const float NegativeInfinity = -1.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.
public const float NegativeZero = (float)-0.0;

View file

@ -0,0 +1,8 @@
namespace System
{
interface IMinMaxValue<T>
{
public static T MinValue { get; }
public static T MaxValue { get; }
}
}

View file

@ -3,7 +3,7 @@ using System;
namespace System
{
#unwarn
struct Int : int, IInteger, IHashable, IFormattable, IIsNaN, IParseable<int, ParseError>, IParseable<int>
struct Int : int, IInteger, IHashable, IFormattable, IIsNaN, IParseable<int, ParseError>, IParseable<int>, IMinMaxValue<int>
{
public enum ParseError
{
@ -16,6 +16,9 @@ namespace System
public const int MaxValue = (sizeof(int) == 8) ? 0x7FFFFFFFFFFFFFFFL : 0x7FFFFFFF;
public const int MinValue = (sizeof(int) == 8) ? -0x8000000000000000L : -0x80000000;
public static int IMinMaxValue<int>.MinValue => MinValue;
public static int IMinMaxValue<int>.MaxValue => MaxValue;
public static int operator<=>(Self a, Self b)
{
return (SelfBase)a <=> (SelfBase)b;

View file

@ -3,7 +3,7 @@ using System.Globalization;
namespace System
{
#unwarn
struct Int16 : int16, IInteger, ISigned, IHashable, IFormattable, IIsNaN, IParseable<int16, ParseError>, IParseable<int16>
struct Int16 : int16, IInteger, ISigned, IHashable, IFormattable, IIsNaN, IParseable<int16, ParseError>, IParseable<int16>, IMinMaxValue<int16>
{
public enum ParseError
{
@ -16,6 +16,9 @@ namespace System
public const int16 MaxValue = 0x7FFF;
public const int16 MinValue = -0x8000;
public static int16 IMinMaxValue<int16>.MinValue => MinValue;
public static int16 IMinMaxValue<int16>.MaxValue => MaxValue;
public static int operator<=>(Self a, Self b)
{
return (SelfBase)a <=> (SelfBase)b;

View file

@ -3,7 +3,7 @@ using System.Globalization;
namespace System
{
#unwarn
struct Int32 : int32, IInteger, ISigned, IHashable, IFormattable, IIsNaN, IParseable<int32, ParseError>, IParseable<int32>
struct Int32 : int32, IInteger, ISigned, IHashable, IFormattable, IIsNaN, IParseable<int32, ParseError>, IParseable<int32>, IMinMaxValue<int32>
{
public enum ParseError
{
@ -16,6 +16,9 @@ namespace System
public const int32 MaxValue = 0x7FFFFFFF;
public const int32 MinValue = -0x80000000;
public static int32 IMinMaxValue<int32>.MinValue => MinValue;
public static int32 IMinMaxValue<int32>.MaxValue => MaxValue;
public static int operator<=>(Self a, Self b)
{
return (SelfBase)a <=> (SelfBase)b;

View file

@ -3,7 +3,7 @@ using System.Globalization;
namespace System
{
#unwarn
struct Int64 : int64, IInteger, ISigned, IFormattable, IHashable, IIsNaN, IParseable<int64, ParseError>, IParseable<int64>
struct Int64 : int64, IInteger, ISigned, IFormattable, IHashable, IIsNaN, IParseable<int64, ParseError>, IParseable<int64>, IMinMaxValue<int64>
{
public enum ParseError
{
@ -16,6 +16,9 @@ namespace System
public const int64 MaxValue = 0x7FFFFFFFFFFFFFFFL;
public const int64 MinValue = -0x8000000000000000L;
public static int64 IMinMaxValue<int64>.MinValue => MinValue;
public static int64 IMinMaxValue<int64>.MaxValue => MaxValue;
public static int operator<=>(Int64 a, Int64 b)
{
return (SelfBase)a <=> (SelfBase)b;

View file

@ -3,7 +3,7 @@ using System.Globalization;
namespace System
{
#unwarn
struct Int8 : int8, IInteger, ISigned, IHashable, IFormattable, IIsNaN, IParseable<int8, ParseError>, IParseable<int8>
struct Int8 : int8, IInteger, ISigned, IHashable, IFormattable, IIsNaN, IParseable<int8, ParseError>, IParseable<int8>, IMinMaxValue<int8>
{
public enum ParseError
{
@ -16,6 +16,9 @@ namespace System
public const int8 MaxValue = 0x7F;
public const int8 MinValue = -0x80;
public static int8 IMinMaxValue<int8>.MinValue => MinValue;
public static int8 IMinMaxValue<int8>.MaxValue => MaxValue;
public static int operator<=>(Self a, Self b)
{
return (SelfBase)a <=> (SelfBase)b;

View file

@ -1,7 +1,7 @@
namespace System
{
#unwarn
struct UInt : uint, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable<uint, ParseError>, IParseable<uint>
struct UInt : uint, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable<uint, ParseError>, IParseable<uint>, IMinMaxValue<uint>
{
public enum ParseError
{
@ -14,6 +14,9 @@ namespace System
public const uint MaxValue = (sizeof(uint) == 8) ? 0xFFFFFFFFFFFFFFFFUL : 0xFFFFFFFFL;
public const uint MinValue = 0;
public static uint IMinMaxValue<uint>.MinValue => MinValue;
public static uint IMinMaxValue<uint>.MaxValue => MaxValue;
public bool IsNull()
{
return this == 0;

View file

@ -3,7 +3,7 @@ using System.Globalization;
namespace System
{
#unwarn
struct UInt16 : uint16, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable<uint16, ParseError>, IParseable<uint16>
struct UInt16 : uint16, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable<uint16, ParseError>, IParseable<uint16>, IMinMaxValue<uint16>
{
public enum ParseError
{
@ -16,6 +16,9 @@ namespace System
public const uint16 MaxValue = 0xFFFF;
public const uint16 MinValue = 0;
public static uint16 IMinMaxValue<uint16>.MinValue => MinValue;
public static uint16 IMinMaxValue<uint16>.MaxValue => MaxValue;
public static int operator<=>(Self a, Self b)
{
return (SelfBase)a <=> (SelfBase)b;

View file

@ -3,7 +3,7 @@ using System.Globalization;
namespace System
{
#unwarn
struct UInt32 : uint32, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable<uint32, ParseError>, IParseable<uint32>
struct UInt32 : uint32, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable<uint32, ParseError>, IParseable<uint32>, IMinMaxValue<uint32>
{
public enum ParseError
{
@ -16,6 +16,9 @@ namespace System
public const uint32 MaxValue = 0xFFFFFFFFL;
public const uint32 MinValue = 0;
public static uint32 IMinMaxValue<uint32>.MinValue => MinValue;
public static uint32 IMinMaxValue<uint32>.MaxValue => MaxValue;
public static int operator<=>(Self a, Self b)
{
return (SelfBase)a <=> (SelfBase)b;

View file

@ -3,7 +3,7 @@ using System.Globalization;
namespace System
{
#unwarn
struct UInt64 : uint64, IInteger, IUnsigned, IHashable, IIsNaN, IFormattable, IParseable<uint64, ParseError>, IParseable<uint64>
struct UInt64 : uint64, IInteger, IUnsigned, IHashable, IIsNaN, IFormattable, IParseable<uint64, ParseError>, IParseable<uint64>, IMinMaxValue<uint64>
{
public enum ParseError
{
@ -16,6 +16,9 @@ namespace System
public const uint64 MaxValue = 0xFFFFFFFFFFFFFFFFUL;
public const uint64 MinValue = 0;
public static uint64 IMinMaxValue<uint64>.MinValue => MinValue;
public static uint64 IMinMaxValue<uint64>.MaxValue => MaxValue;
public static int operator<=>(UInt64 a, UInt64 b)
{
return (SelfBase)a <=> (SelfBase)b;

View file

@ -3,7 +3,7 @@ using System.Globalization;
namespace System
{
#unwarn
struct UInt8 : uint8, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable<uint8, ParseError>, IParseable<uint8>
struct UInt8 : uint8, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable<uint8, ParseError>, IParseable<uint8>, IMinMaxValue<uint8>
{
public enum ParseError
{
@ -16,6 +16,9 @@ namespace System
public const uint8 MaxValue = 0xFF;
public const uint8 MinValue = 0;
public static uint8 IMinMaxValue<uint8>.MinValue => MinValue;
public static uint8 IMinMaxValue<uint8>.MaxValue => MaxValue;
public static int operator<=>(Self a, Self b)
{
return (SelfBase)a <=> (SelfBase)b;

View file

@ -50,5 +50,20 @@ namespace Tests
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);
}
}
}

View file

@ -113,5 +113,29 @@ namespace Tests
Uint64ParseErrorTest("+0x", .HexNumber);
Uint64ParseErrorTest("+0X", .HexNumber);
}
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<int>(Int.MinValue, Int.MaxValue);
MinMaxTest<int8>(Int8.MinValue, Int8.MaxValue);
MinMaxTest<int16>(Int16.MinValue, Int16.MaxValue);
MinMaxTest<int32>(Int32.MinValue, Int32.MaxValue);
MinMaxTest<int64>(Int64.MinValue, Int64.MaxValue);
MinMaxTest<uint>(UInt.MinValue, UInt.MaxValue);
MinMaxTest<uint8>(UInt8.MinValue, UInt8.MaxValue);
MinMaxTest<uint16>(UInt16.MinValue, UInt16.MaxValue);
MinMaxTest<uint32>(UInt32.MinValue, UInt32.MaxValue);
MinMaxTest<uint64>(UInt64.MinValue, UInt64.MaxValue);
}
}
}