Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 119 additions & 2 deletions src/MissingValues/Int256.Conversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ private static bool TryConvertFromChecked<TOther>(TOther value, out Int256 resul
float actual => (Int256)actual,
double actual => (Int256)actual,
NFloat actual => (Int256)actual,
#if NET11_0_OR_GREATER
Decimal32 actual => (Int256)actual,
Decimal64 actual => (Int256)actual,
Decimal128 actual => (Int256)actual,
#endif
decimal actual => (Int256)actual,
byte actual => (Int256)actual,
ushort actual => (Int256)actual,
Expand Down Expand Up @@ -112,10 +117,16 @@ private static bool TryConvertFromSaturating<TOther>(TOther value, out Int256 re
result = value switch
{
char actual => actual,
Half actual => (Int256)actual,
float actual => (Int256)actual,
Half actual => (Half.IsPositiveInfinity(actual)) ? MaxValue : (Half.IsNegativeInfinity(actual)) ? MinValue : (Int256)actual,
float actual => (float.IsPositiveInfinity(actual)) ? MaxValue : (float.IsNegativeInfinity(actual)) ? MinValue : (Int256)actual,
double actual => (actual <= -TwoPow255) ? MinValue : (actual > +TwoPow255) ? MaxValue : (Int256)actual,
NFloat actual => (actual <= -TwoPow255) ? MinValue : (actual > +TwoPow255) ? MaxValue : (Int256)actual,
#if NET11_0_OR_GREATER
// 2^255 in Binary Encoding
Decimal32 actual => (actual <= Decimal32.DecodeBinary(0xD5D8_57A4)) ? MinValue : (actual > Decimal32.DecodeBinary(0x55D8_57A4)) ? MaxValue : (Int256)actual,
Decimal64 actual => (actual <= Decimal64.DecodeBinary(0xB974_919D_5556_EB52)) ? MinValue : (actual > Decimal64.DecodeBinary(0x3974_919D_5556_EB52)) ? MaxValue : (Int256)actual,
Decimal128 actual => (actual <= Decimal128.DecodeBinary(new UInt128(0xB097_1D73_14F5_34B6, 0x09C6_EFE6_C11D_255B))) ? MinValue : (actual > Decimal128.DecodeBinary(new UInt128(0x3097_1D73_14F5_34B6, 0x09C6_EFE6_C11D_255B))) ? MaxValue : (Int256)actual,
#endif
decimal actual => (Int256)actual,
byte actual => (Int256)actual,
ushort actual => (Int256)actual,
Expand Down Expand Up @@ -152,6 +163,12 @@ private static bool TryConvertFromTruncating<TOther>(TOther value, out Int256 re
float actual => (float.IsPositiveInfinity(actual)) ? MaxValue : (float.IsNegativeInfinity(actual)) ? MinValue : (Int256)actual,
double actual => (actual <= -TwoPow255) ? MinValue : (actual > +TwoPow255) ? MaxValue : (Int256)actual,
NFloat actual => (actual <= -TwoPow255) ? MinValue : (actual > +TwoPow255) ? MaxValue : (Int256)actual,
#if NET11_0_OR_GREATER
// 2^255 in Binary Encoding
Decimal32 actual => (actual <= Decimal32.DecodeBinary(0xD5D8_57A4)) ? MinValue : (actual > Decimal32.DecodeBinary(0x55D8_57A4)) ? MaxValue : (Int256)actual,
Decimal64 actual => (actual <= Decimal64.DecodeBinary(0xB974_919D_5556_EB52)) ? MinValue : (actual > Decimal64.DecodeBinary(0x3974_919D_5556_EB52)) ? MaxValue : (Int256)actual,
Decimal128 actual => (actual <= Decimal128.DecodeBinary(new UInt128(0xB097_1D73_14F5_34B6, 0x09C6_EFE6_C11D_255B))) ? MinValue : (actual > Decimal128.DecodeBinary(new UInt128(0x3097_1D73_14F5_34B6, 0x09C6_EFE6_C11D_255B))) ? MaxValue : (Int256)actual,
#endif
decimal actual => (Int128)actual,
byte actual => (Int256)actual,
ushort actual => (Int256)actual,
Expand Down Expand Up @@ -186,6 +203,11 @@ static bool INumberBase<Int256>.TryConvertToChecked<TOther>(Int256 value, out TO
float => (TOther)(object)(float)value,
double => (TOther)(object)(double)value,
NFloat => (TOther)(object)(NFloat)value,
#if NET11_0_OR_GREATER
Decimal32 => (TOther)(object)(Decimal32)value,
Decimal64 => (TOther)(object)(Decimal64)value,
Decimal128 => (TOther)(object)(Decimal128)value,
#endif
decimal => (TOther)(object)(decimal)value,
byte => (TOther)(object)(byte)value,
ushort => (TOther)(object)(ushort)value,
Expand Down Expand Up @@ -223,6 +245,11 @@ static bool INumberBase<Int256>.TryConvertToSaturating<TOther>(Int256 value, out
float => (TOther)(object)(float)value,
double => (TOther)(object)(double)value,
NFloat => (TOther)(object)(NFloat)value,
#if NET11_0_OR_GREATER
Decimal32 => (TOther)(object)(Decimal32)value,
Decimal64 => (TOther)(object)(Decimal64)value,
Decimal128 => (TOther)(object)(Decimal128)value,
#endif
decimal => (TOther)(object)(decimal)value,
byte => (TOther)(object)((value >= (Int256)byte.MaxValue) ? byte.MaxValue : (value <= (Int256)byte.MinValue) ? byte.MinValue : (byte)value),
ushort => (TOther)(object)((value >= (Int256)ushort.MaxValue) ? ushort.MaxValue : (value <= (Int256)ushort.MinValue) ? ushort.MinValue : (ushort)value),
Expand Down Expand Up @@ -258,6 +285,11 @@ static bool INumberBase<Int256>.TryConvertToTruncating<TOther>(Int256 value, out
float => (TOther)(object)(float)value,
double => (TOther)(object)(double)value,
NFloat => (TOther)(object)(NFloat)value,
#if NET11_0_OR_GREATER
Decimal32 => (TOther)(object)(Decimal32)value,
Decimal64 => (TOther)(object)(Decimal64)value,
Decimal128 => (TOther)(object)(Decimal128)value,
#endif
decimal => (TOther)(object)(decimal)value,
byte => (TOther)(object)(byte)value,
ushort => (TOther)(object)(ushort)value,
Expand Down Expand Up @@ -645,6 +677,34 @@ public static explicit operator decimal(in Int256 value)
}
return (decimal)(double)(UInt256)(value);
}

#if NET11_0_OR_GREATER
/// <summary>
/// Explicitly converts a <see cref="Int256" /> value to a <see cref="Decimal32"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static explicit operator Decimal32(in Int256 value)
{
return BitHelper.ConvertToDecimalN<Decimal32, Int256>(in value);
}
/// <summary>
/// Explicitly converts a <see cref="Int256" /> value to a <see cref="Decimal64"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static explicit operator Decimal64(in Int256 value)
{
return BitHelper.ConvertToDecimalN<Decimal64, Int256>(in value);
}
/// <summary>
/// Explicitly converts a <see cref="Int256" /> value to a <see cref="Decimal128"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static explicit operator Decimal128(in Int256 value)
{
return BitHelper.ConvertToDecimalN<Decimal128, Int256>(in value);
}
#endif

/// <summary>
/// Explicitly converts a <see cref="Int256" /> value to a <see cref="Octo"/>.
/// </summary>
Expand Down Expand Up @@ -872,6 +932,63 @@ public static explicit operator checked Int256(BigInteger value)
/// <param name="value">The value to convert.</param>
/// <exception cref="OverflowException"><paramref name="value"/> is outside the range of <see cref="Int256"/>.</exception>
public static explicit operator checked Int256(decimal value) => checked((Int256)(double)value);

#if NET11_0_OR_GREATER
/// <summary>
/// Explicitly converts a <see cref="Decimal32" /> value to a <see cref="Int256"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static explicit operator Int256(Decimal32 value)
{
return BitHelper.ConvertFromDecimalN<Int256, Decimal32>(value);
}
/// <summary>
/// Explicitly converts a <see cref="Decimal32" /> value to a <see cref="Int256"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <exception cref="OverflowException"><paramref name="value"/> is outside the range of <see cref="Int256"/>.</exception>
public static explicit operator checked Int256(Decimal32 value)
{
return BitHelper.ConvertFromDecimalN<Int256, Decimal32>(value, true);
}

/// <summary>
/// Explicitly converts a <see cref="Decimal64" /> value to a <see cref="Int256"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static explicit operator Int256(Decimal64 value)
{
return BitHelper.ConvertFromDecimalN<Int256, Decimal64>(value);
}
/// <summary>
/// Explicitly converts a <see cref="Decimal64" /> value to a <see cref="Int256"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <exception cref="OverflowException"><paramref name="value"/> is outside the range of <see cref="Int256"/>.</exception>
public static explicit operator checked Int256(Decimal64 value)
{
return BitHelper.ConvertFromDecimalN<Int256, Decimal64>(value, true);
}

/// <summary>
/// Explicitly converts a <see cref="Decimal128" /> value to a <see cref="Int256"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static explicit operator Int256(Decimal128 value)
{
return BitHelper.ConvertFromDecimalN<Int256, Decimal128>(value);
}
/// <summary>
/// Explicitly converts a <see cref="Decimal128" /> value to a <see cref="Int256"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <exception cref="OverflowException"><paramref name="value"/> is outside the range of <see cref="Int256"/>.</exception>
public static explicit operator checked Int256(Decimal128 value)
{
return BitHelper.ConvertFromDecimalN<Int256, Decimal128>(value, true);
}
#endif

/// <summary>
/// Explicitly converts a <see cref="double" /> value to a <see cref="Int256"/>.
/// </summary>
Expand Down
119 changes: 117 additions & 2 deletions src/MissingValues/Int512.Conversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ private static bool TryConvertFromChecked<TOther>(TOther value, out Int512 resul
double actual => (Int512)actual,
NFloat actual => (Int512)actual,
Quad actual => (Int512)actual,
#if NET11_0_OR_GREATER
Decimal32 actual => (Int512)actual,
Decimal64 actual => (Int512)actual,
Decimal128 actual => (Int512)actual,
#endif
decimal actual => (Int512)actual,
byte actual => (Int512)actual,
ushort actual => (Int512)actual,
Expand Down Expand Up @@ -117,7 +122,12 @@ private static bool TryConvertFromSaturating<TOther>(TOther value, out Int512 re
float actual => (float.IsPositiveInfinity(actual)) ? MaxValue : (float.IsNegativeInfinity(actual)) ? MinValue : (Int512)actual,
double actual => (actual <= -TwoPow511) ? MinValue : (actual > +TwoPow511) ? MaxValue : (Int512)actual,
NFloat actual => (actual <= -TwoPow511) ? MinValue : (actual > +TwoPow511) ? MaxValue : (Int512)actual,
Quad actual => (actual <= (new Quad(0xC1FE_0000_0000_0000, 0x0000_0000_0000_0000))) ? MinValue : (actual > (new Quad(0x41FE_0000_0000_0000, 0x0000_0000_0000_0000))) ? MaxValue : (Int512)actual,
#if NET11_0_OR_GREATER
// 2^511 in Binary Encoding
Decimal32 actual => (Decimal32.IsPositiveInfinity(actual)) ? MaxValue : (Decimal32.IsNegativeInfinity(actual)) ? MinValue : (Int256)actual,
Decimal64 actual => (actual <= Decimal64.DecodeBinary(14057934741360918819)) ? MinValue : (actual > Decimal64.DecodeBinary(4834562704506143011)) ? MaxValue : (Int256)actual,
Decimal128 actual => (actual <= Decimal128.DecodeBinary(new UInt128(0xB131_4A87_29FC_3DDB, 0x71FD_852E_9D69_DCCB))) ? MinValue : (actual > Decimal128.DecodeBinary(new UInt128(0x3131_4A87_29FC_3DDB, 0x71FD_852E_9D69_DCCB))) ? MaxValue : (Int256)actual,
#endif
decimal actual => (Int512)actual,
byte actual => (Int512)actual,
ushort actual => (Int512)actual,
Expand Down Expand Up @@ -156,7 +166,12 @@ private static bool TryConvertFromTruncating<TOther>(TOther value, out Int512 re
float actual => (float.IsPositiveInfinity(actual)) ? MaxValue : (float.IsNegativeInfinity(actual)) ? MinValue : (Int512)actual,
double actual => (actual <= -TwoPow511) ? MinValue : (actual > +TwoPow511) ? MaxValue : (Int512)actual,
NFloat actual => (actual <= -TwoPow511) ? MinValue : (actual > +TwoPow511) ? MaxValue : (Int512)actual,
Quad actual => (actual <= (new Quad(0xC1FE_0000_0000_0000, 0x0000_0000_0000_0000))) ? MinValue : (actual > (new Quad(0x41FE_0000_0000_0000, 0x0000_0000_0000_0000))) ? MaxValue : (Int512)actual,
#if NET11_0_OR_GREATER
// 2^511 in Binary Encoding
Decimal32 actual => (Decimal32.IsPositiveInfinity(actual)) ? MaxValue : (Decimal32.IsNegativeInfinity(actual)) ? MinValue : (Int256)actual,
Decimal64 actual => (actual <= Decimal64.DecodeBinary(14057934741360918819)) ? MinValue : (actual > Decimal64.DecodeBinary(4834562704506143011)) ? MaxValue : (Int256)actual,
Decimal128 actual => (actual <= Decimal128.DecodeBinary(new UInt128(0xB131_4A87_29FC_3DDB, 0x71FD_852E_9D69_DCCB))) ? MinValue : (actual > Decimal128.DecodeBinary(new UInt128(0x3131_4A87_29FC_3DDB, 0x71FD_852E_9D69_DCCB))) ? MaxValue : (Int256)actual,
#endif
decimal actual => (Int512)actual,
byte actual => (Int512)actual,
ushort actual => (Int512)actual,
Expand Down Expand Up @@ -195,6 +210,11 @@ static bool INumberBase<Int512>.TryConvertToChecked<TOther>(Int512 value, out TO
double => (TOther)(object)(double)value,
NFloat => (TOther)(object)(NFloat)value,
Quad => (TOther)(object)(Quad)value,
#if NET11_0_OR_GREATER
Decimal32 => (TOther)(object)(Decimal32)value,
Decimal64 => (TOther)(object)(Decimal64)value,
Decimal128 => (TOther)(object)(Decimal128)value,
#endif
decimal => (TOther)(object)(decimal)value,
byte => (TOther)(object)(byte)value,
ushort => (TOther)(object)(ushort)value,
Expand Down Expand Up @@ -233,6 +253,11 @@ static bool INumberBase<Int512>.TryConvertToSaturating<TOther>(Int512 value, out
double => (TOther)(object)(double)value,
NFloat => (TOther)(object)(NFloat)value,
Quad => (TOther)(object)(Quad)value,
#if NET11_0_OR_GREATER
Decimal32 => (TOther)(object)(Decimal32)value,
Decimal64 => (TOther)(object)(Decimal64)value,
Decimal128 => (TOther)(object)(Decimal128)value,
#endif
decimal => (TOther)(object)(decimal)value,
byte => (TOther)(object)((value >= (Int512)byte.MaxValue) ? byte.MaxValue : (value <= (Int512)byte.MinValue) ? byte.MinValue : (byte)value),
ushort => (TOther)(object)((value >= (Int512)ushort.MaxValue) ? ushort.MaxValue : (value <= (Int512)ushort.MinValue) ? ushort.MinValue : (ushort)value),
Expand Down Expand Up @@ -269,6 +294,11 @@ static bool INumberBase<Int512>.TryConvertToTruncating<TOther>(Int512 value, out
double => (TOther)(object)(double)value,
NFloat => (TOther)(object)(NFloat)value,
Quad => (TOther)(object)(Quad)value,
#if NET11_0_OR_GREATER
Decimal32 => (TOther)(object)(Decimal32)value,
Decimal64 => (TOther)(object)(Decimal64)value,
Decimal128 => (TOther)(object)(Decimal128)value,
#endif
decimal => (TOther)(object)(decimal)value,
byte => (TOther)(object)(byte)value,
ushort => (TOther)(object)(ushort)value,
Expand Down Expand Up @@ -658,6 +688,34 @@ public static explicit operator decimal(in Int512 value)
}
return (decimal)(UInt512)(value);
}

#if NET11_0_OR_GREATER
/// <summary>
/// Explicitly converts a <see cref="Int512" /> value to a <see cref="Decimal32"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static explicit operator Decimal32(in Int512 value)
{
return BitHelper.ConvertToDecimalN<Decimal32, Int512>(in value);
}
/// <summary>
/// Explicitly converts a <see cref="Int512" /> value to a <see cref="Decimal64"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static explicit operator Decimal64(in Int512 value)
{
return BitHelper.ConvertToDecimalN<Decimal64, Int512>(in value);
}
/// <summary>
/// Explicitly converts a <see cref="Int512" /> value to a <see cref="Decimal128"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static explicit operator Decimal128(in Int512 value)
{
return BitHelper.ConvertToDecimalN<Decimal128, Int512>(in value);
}
#endif

/// <summary>
/// Explicitly converts a <see cref="Int512" /> value to a <see cref="Octo"/>.
/// </summary>
Expand Down Expand Up @@ -894,6 +952,63 @@ public static explicit operator checked Int512(BigInteger value)
/// <param name="value">The value to convert.</param>
/// <exception cref="OverflowException"><paramref name="value"/> is outside the range of <see cref="Int512"/>.</exception>
public static explicit operator checked Int512(decimal value) => checked((Int512)(double)value);

#if NET11_0_OR_GREATER
/// <summary>
/// Explicitly converts a <see cref="Decimal32" /> value to a <see cref="Int512"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static explicit operator Int512(Decimal32 value)
{
return BitHelper.ConvertFromDecimalN<Int512, Decimal32>(value);
}
/// <summary>
/// Explicitly converts a <see cref="Decimal32" /> value to a <see cref="Int512"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <exception cref="OverflowException"><paramref name="value"/> is outside the range of <see cref="Int512"/>.</exception>
public static explicit operator checked Int512(Decimal32 value)
{
return BitHelper.ConvertFromDecimalN<Int512, Decimal32>(value, true);
}

/// <summary>
/// Explicitly converts a <see cref="Decimal64" /> value to a <see cref="Int512"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static explicit operator Int512(Decimal64 value)
{
return BitHelper.ConvertFromDecimalN<Int512, Decimal64>(value);
}
/// <summary>
/// Explicitly converts a <see cref="Decimal64" /> value to a <see cref="Int512"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <exception cref="OverflowException"><paramref name="value"/> is outside the range of <see cref="Int512"/>.</exception>
public static explicit operator checked Int512(Decimal64 value)
{
return BitHelper.ConvertFromDecimalN<Int512, Decimal64>(value, true);
}

/// <summary>
/// Explicitly converts a <see cref="Decimal128" /> value to a <see cref="Int512"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static explicit operator Int512(Decimal128 value)
{
return BitHelper.ConvertFromDecimalN<Int512, Decimal128>(value);
}
/// <summary>
/// Explicitly converts a <see cref="Decimal128" /> value to a <see cref="Int512"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <exception cref="OverflowException"><paramref name="value"/> is outside the range of <see cref="Int512"/>.</exception>
public static explicit operator checked Int512(Decimal128 value)
{
return BitHelper.ConvertFromDecimalN<Int512, Decimal128>(value, true);
}
#endif

/// <summary>
/// Explicitly converts a <see cref="double" /> value to a <see cref="Int512"/>.
/// </summary>
Expand Down
Loading