@@ -259,6 +259,20 @@ public static explicit operator Decimal128(float value)
259
259
return new Decimal128 ( value ) ;
260
260
}
261
261
262
+ #if NET5_0_OR_GREATER
263
+ /// <summary>
264
+ /// Performs an explicit conversion from <see cref="Half"/> to <see cref="Decimal128"/>.
265
+ /// </summary>
266
+ /// <param name="value">The value.</param>
267
+ /// <returns>
268
+ /// The result of the conversion.
269
+ /// </returns>
270
+ public static explicit operator Decimal128 ( Half value )
271
+ {
272
+ return new Decimal128 ( value ) ;
273
+ }
274
+ #endif
275
+
262
276
/// <summary>
263
277
/// Performs an implicit conversion from <see cref="System.Int32"/> to <see cref="Decimal128"/>.
264
278
/// </summary>
@@ -371,6 +385,20 @@ public static explicit operator float(Decimal128 value)
371
385
return Decimal128 . ToSingle ( value ) ;
372
386
}
373
387
388
+ #if NET5_0_OR_GREATER
389
+ /// <summary>
390
+ /// Performs an explicit conversion from <see cref="Decimal128"/> to <see cref="Half"/>.
391
+ /// </summary>
392
+ /// <param name="value">The value to convert.</param>
393
+ /// <returns>
394
+ /// The result of the conversion.
395
+ /// </returns>
396
+ public static explicit operator Half ( Decimal128 value )
397
+ {
398
+ return Decimal128 . ToHalf ( value ) ;
399
+ }
400
+ #endif
401
+
374
402
/// <summary>
375
403
/// Performs an explicit conversion from <see cref="Decimal128"/> to <see cref="System.Int32"/>.
376
404
/// </summary>
@@ -962,6 +990,40 @@ public static float ToSingle(Decimal128 d)
962
990
}
963
991
}
964
992
993
+ #if NET5_0_OR_GREATER
994
+ /// <summary>
995
+ /// Converts the value of the specified <see cref="Decimal128"/> to the equivalent <see cref="Half"/>.
996
+ /// </summary>
997
+ /// <param name="d">The number to convert.</param>
998
+ /// <returns>A <see cref="Half"/> equivalent to <paramref name="d" />.</returns>
999
+ public static Half ToHalf ( Decimal128 d )
1000
+ {
1001
+ if ( Flags . IsFirstForm ( d . _highBits ) )
1002
+ {
1003
+ // TODO: implement this more efficiently
1004
+ var stringValue = d . ToString ( ) ;
1005
+ return Half . Parse ( stringValue , CultureInfo . InvariantCulture ) ;
1006
+ }
1007
+
1008
+ if ( Flags . IsSecondForm ( d . _highBits ) )
1009
+ {
1010
+ return ( Half ) 0.0 ;
1011
+ }
1012
+
1013
+ if ( Flags . IsPositiveInfinity ( d . _highBits ) )
1014
+ {
1015
+ return Half . PositiveInfinity ;
1016
+ }
1017
+
1018
+ if ( Flags . IsNegativeInfinity ( d . _highBits ) )
1019
+ {
1020
+ return Half . NegativeInfinity ;
1021
+ }
1022
+
1023
+ return Half . NaN ;
1024
+ }
1025
+ #endif
1026
+
965
1027
/// <summary>
966
1028
/// Converts the value of the specified <see cref="Decimal128"/> to the equivalent 16-bit unsigned integer.
967
1029
/// </summary>
@@ -1441,6 +1503,21 @@ public Decimal128(float value)
1441
1503
_lowBits = decimal128Value . GetIEEELowBits ( ) ;
1442
1504
}
1443
1505
1506
+ #if NET5_0_OR_GREATER
1507
+ /// <summary>
1508
+ /// Initializes a new instance of the <see cref="Decimal128"/> struct.
1509
+ /// </summary>
1510
+ /// <param name="value">The value.</param>
1511
+ public Decimal128 ( Half value )
1512
+ {
1513
+ // TODO: implement this more efficiently
1514
+ var stringValue = JsonConvert . ToString ( value ) ;
1515
+ var decimal128Value = Decimal128 . Parse ( stringValue ) ;
1516
+ _highBits = MapIEEEHighBitsToDecimal128HighBits ( decimal128Value . GetIEEEHighBits ( ) ) ;
1517
+ _lowBits = decimal128Value . GetIEEELowBits ( ) ;
1518
+ }
1519
+ #endif
1520
+
1444
1521
/// <summary>
1445
1522
/// Initializes a new instance of the <see cref="Decimal128"/> struct.
1446
1523
/// </summary>
0 commit comments