| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- // See the LICENSE file in the project root for more information.
- // ===================================================================================================
- // Portions of the code implemented below are based on the 'Berkeley SoftFloat Release 3e' algorithms.
- // ===================================================================================================
- /*============================================================
- **
- **
- **
- ** Purpose: Some floating-point math operations
- **
- **
- ===========================================================*/
- using System.Diagnostics;
- using System.Diagnostics.CodeAnalysis;
- using System.Runtime.CompilerServices;
- using System.Runtime.Versioning;
- namespace System
- {
- public static partial class Math
- {
- public const double E = 2.7182818284590452354;
- public const double PI = 3.14159265358979323846;
- private const int maxRoundingDigits = 15;
- private const double doubleRoundLimit = 1e16d;
- // This table is required for the Round function which can specify the number of digits to round to
- private static readonly double[] roundPower10Double = new double[] {
- 1E0, 1E1, 1E2, 1E3, 1E4, 1E5, 1E6, 1E7, 1E8,
- 1E9, 1E10, 1E11, 1E12, 1E13, 1E14, 1E15
- };
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static short Abs(short value)
- {
- if (value < 0)
- {
- value = (short)-value;
- if (value < 0)
- {
- ThrowAbsOverflow();
- }
- }
- return value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int Abs(int value)
- {
- if (value < 0)
- {
- value = -value;
- if (value < 0)
- {
- ThrowAbsOverflow();
- }
- }
- return value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static long Abs(long value)
- {
- if (value < 0)
- {
- value = -value;
- if (value < 0)
- {
- ThrowAbsOverflow();
- }
- }
- return value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [CLSCompliant(false)]
- public static sbyte Abs(sbyte value)
- {
- if (value < 0)
- {
- value = (sbyte)-value;
- if (value < 0)
- {
- ThrowAbsOverflow();
- }
- }
- return value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static decimal Abs(decimal value)
- {
- return decimal.Abs(value);
- }
- [DoesNotReturn]
- [StackTraceHidden]
- private static void ThrowAbsOverflow()
- {
- throw new OverflowException(SR.Overflow_NegateTwosCompNum);
- }
- public static long BigMul(int a, int b)
- {
- return ((long)a) * b;
- }
- public static double BitDecrement(double x)
- {
- long bits = BitConverter.DoubleToInt64Bits(x);
- if (((bits >> 32) & 0x7FF00000) >= 0x7FF00000)
- {
- // NaN returns NaN
- // -Infinity returns -Infinity
- // +Infinity returns double.MaxValue
- return (bits == 0x7FF00000_00000000) ? double.MaxValue : x;
- }
- if (bits == 0x00000000_00000000)
- {
- // +0.0 returns -double.Epsilon
- return -double.Epsilon;
- }
- // Negative values need to be incremented
- // Positive values need to be decremented
- bits += ((bits < 0) ? +1 : -1);
- return BitConverter.Int64BitsToDouble(bits);
- }
- public static double BitIncrement(double x)
- {
- long bits = BitConverter.DoubleToInt64Bits(x);
- if (((bits >> 32) & 0x7FF00000) >= 0x7FF00000)
- {
- // NaN returns NaN
- // -Infinity returns double.MinValue
- // +Infinity returns +Infinity
- return (bits == unchecked((long)(0xFFF00000_00000000))) ? double.MinValue : x;
- }
- if (bits == unchecked((long)(0x80000000_00000000)))
- {
- // -0.0 returns double.Epsilon
- return double.Epsilon;
- }
- // Negative values need to be decremented
- // Positive values need to be incremented
- bits += ((bits < 0) ? -1 : +1);
- return BitConverter.Int64BitsToDouble(bits);
- }
- public static unsafe double CopySign(double x, double y)
- {
- // This method is required to work for all inputs,
- // including NaN, so we operate on the raw bits.
- long xbits = BitConverter.DoubleToInt64Bits(x);
- long ybits = BitConverter.DoubleToInt64Bits(y);
- // If the sign bits of x and y are not the same,
- // flip the sign bit of x and return the new value;
- // otherwise, just return x
- if ((xbits ^ ybits) < 0)
- {
- return BitConverter.Int64BitsToDouble(xbits ^ long.MinValue);
- }
- return x;
- }
- public static int DivRem(int a, int b, out int result)
- {
- // TODO https://github.com/dotnet/coreclr/issues/3439:
- // Restore to using % and / when the JIT is able to eliminate one of the idivs.
- // In the meantime, a * and - is measurably faster than an extra /.
- int div = a / b;
- result = a - (div * b);
- return div;
- }
- public static long DivRem(long a, long b, out long result)
- {
- long div = a / b;
- result = a - (div * b);
- return div;
- }
- internal static uint DivRem(uint a, uint b, out uint result)
- {
- uint div = a / b;
- result = a - (div * b);
- return div;
- }
- internal static ulong DivRem(ulong a, ulong b, out ulong result)
- {
- ulong div = a / b;
- result = a - (div * b);
- return div;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static decimal Ceiling(decimal d)
- {
- return decimal.Ceiling(d);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static byte Clamp(byte value, byte min, byte max)
- {
- if (min > max)
- {
- ThrowMinMaxException(min, max);
- }
- if (value < min)
- {
- return min;
- }
- else if (value > max)
- {
- return max;
- }
- return value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static decimal Clamp(decimal value, decimal min, decimal max)
- {
- if (min > max)
- {
- ThrowMinMaxException(min, max);
- }
- if (value < min)
- {
- return min;
- }
- else if (value > max)
- {
- return max;
- }
- return value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static double Clamp(double value, double min, double max)
- {
- if (min > max)
- {
- ThrowMinMaxException(min, max);
- }
- if (value < min)
- {
- return min;
- }
- else if (value > max)
- {
- return max;
- }
- return value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static short Clamp(short value, short min, short max)
- {
- if (min > max)
- {
- ThrowMinMaxException(min, max);
- }
- if (value < min)
- {
- return min;
- }
- else if (value > max)
- {
- return max;
- }
- return value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int Clamp(int value, int min, int max)
- {
- if (min > max)
- {
- ThrowMinMaxException(min, max);
- }
- if (value < min)
- {
- return min;
- }
- else if (value > max)
- {
- return max;
- }
- return value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static long Clamp(long value, long min, long max)
- {
- if (min > max)
- {
- ThrowMinMaxException(min, max);
- }
- if (value < min)
- {
- return min;
- }
- else if (value > max)
- {
- return max;
- }
- return value;
- }
- [CLSCompliant(false)]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static sbyte Clamp(sbyte value, sbyte min, sbyte max)
- {
- if (min > max)
- {
- ThrowMinMaxException(min, max);
- }
- if (value < min)
- {
- return min;
- }
- else if (value > max)
- {
- return max;
- }
- return value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float Clamp(float value, float min, float max)
- {
- if (min > max)
- {
- ThrowMinMaxException(min, max);
- }
- if (value < min)
- {
- return min;
- }
- else if (value > max)
- {
- return max;
- }
- return value;
- }
- [CLSCompliant(false)]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static ushort Clamp(ushort value, ushort min, ushort max)
- {
- if (min > max)
- {
- ThrowMinMaxException(min, max);
- }
- if (value < min)
- {
- return min;
- }
- else if (value > max)
- {
- return max;
- }
- return value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [CLSCompliant(false)]
- public static uint Clamp(uint value, uint min, uint max)
- {
- if (min > max)
- {
- ThrowMinMaxException(min, max);
- }
- if (value < min)
- {
- return min;
- }
- else if (value > max)
- {
- return max;
- }
- return value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- [CLSCompliant(false)]
- public static ulong Clamp(ulong value, ulong min, ulong max)
- {
- if (min > max)
- {
- ThrowMinMaxException(min, max);
- }
- if (value < min)
- {
- return min;
- }
- else if (value > max)
- {
- return max;
- }
- return value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static decimal Floor(decimal d)
- {
- return decimal.Floor(d);
- }
- public static double IEEERemainder(double x, double y)
- {
- if (double.IsNaN(x))
- {
- return x; // IEEE 754-2008: NaN payload must be preserved
- }
- if (double.IsNaN(y))
- {
- return y; // IEEE 754-2008: NaN payload must be preserved
- }
- double regularMod = x % y;
- if (double.IsNaN(regularMod))
- {
- return double.NaN;
- }
- if ((regularMod == 0) && double.IsNegative(x))
- {
- return double.NegativeZero;
- }
- double alternativeResult = (regularMod - (Abs(y) * Sign(x)));
- if (Abs(alternativeResult) == Abs(regularMod))
- {
- double divisionResult = x / y;
- double roundedResult = Round(divisionResult);
- if (Abs(roundedResult) > Abs(divisionResult))
- {
- return alternativeResult;
- }
- else
- {
- return regularMod;
- }
- }
- if (Abs(alternativeResult) < Abs(regularMod))
- {
- return alternativeResult;
- }
- else
- {
- return regularMod;
- }
- }
- public static double Log(double a, double newBase)
- {
- if (double.IsNaN(a))
- {
- return a; // IEEE 754-2008: NaN payload must be preserved
- }
- if (double.IsNaN(newBase))
- {
- return newBase; // IEEE 754-2008: NaN payload must be preserved
- }
- if (newBase == 1)
- {
- return double.NaN;
- }
- if ((a != 1) && ((newBase == 0) || double.IsPositiveInfinity(newBase)))
- {
- return double.NaN;
- }
- return Log(a) / Log(newBase);
- }
- [NonVersionable]
- public static byte Max(byte val1, byte val2)
- {
- return (val1 >= val2) ? val1 : val2;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static decimal Max(decimal val1, decimal val2)
- {
- return decimal.Max(val1, val2);
- }
- public static double Max(double val1, double val2)
- {
- // This matches the IEEE 754:2019 `maximum` function
- //
- // It propagates NaN inputs back to the caller and
- // otherwise returns the larger of the inputs. It
- // treats +0 as larger than -0 as per the specification.
- if ((val1 > val2) || double.IsNaN(val1))
- {
- return val1;
- }
- if (val1 == val2)
- {
- return double.IsNegative(val1) ? val2 : val1;
- }
- return val2;
- }
- [NonVersionable]
- public static short Max(short val1, short val2)
- {
- return (val1 >= val2) ? val1 : val2;
- }
- [NonVersionable]
- public static int Max(int val1, int val2)
- {
- return (val1 >= val2) ? val1 : val2;
- }
- [NonVersionable]
- public static long Max(long val1, long val2)
- {
- return (val1 >= val2) ? val1 : val2;
- }
- [CLSCompliant(false)]
- [NonVersionable]
- public static sbyte Max(sbyte val1, sbyte val2)
- {
- return (val1 >= val2) ? val1 : val2;
- }
- public static float Max(float val1, float val2)
- {
- // This matches the IEEE 754:2019 `maximum` function
- //
- // It propagates NaN inputs back to the caller and
- // otherwise returns the larger of the inputs. It
- // treats +0 as larger than -0 as per the specification.
- if ((val1 > val2) || float.IsNaN(val1))
- {
- return val1;
- }
- if (val1 == val2)
- {
- return float.IsNegative(val1) ? val2 : val1;
- }
- return val2;
- }
- [CLSCompliant(false)]
- [NonVersionable]
- public static ushort Max(ushort val1, ushort val2)
- {
- return (val1 >= val2) ? val1 : val2;
- }
- [CLSCompliant(false)]
- [NonVersionable]
- public static uint Max(uint val1, uint val2)
- {
- return (val1 >= val2) ? val1 : val2;
- }
- [CLSCompliant(false)]
- [NonVersionable]
- public static ulong Max(ulong val1, ulong val2)
- {
- return (val1 >= val2) ? val1 : val2;
- }
- public static double MaxMagnitude(double x, double y)
- {
- // This matches the IEEE 754:2019 `maximumMagnitude` function
- //
- // It propagates NaN inputs back to the caller and
- // otherwise returns the input with a larger magnitude.
- // It treats +0 as larger than -0 as per the specification.
- double ax = Abs(x);
- double ay = Abs(y);
- if ((ax > ay) || double.IsNaN(ax))
- {
- return x;
- }
- if (ax == ay)
- {
- return double.IsNegative(x) ? y : x;
- }
- return y;
- }
- [NonVersionable]
- public static byte Min(byte val1, byte val2)
- {
- return (val1 <= val2) ? val1 : val2;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static decimal Min(decimal val1, decimal val2)
- {
- return decimal.Min(val1, val2);
- }
- public static double Min(double val1, double val2)
- {
- // This matches the IEEE 754:2019 `minimum` function
- //
- // It propagates NaN inputs back to the caller and
- // otherwise returns the larger of the inputs. It
- // treats +0 as larger than -0 as per the specification.
- if ((val1 < val2) || double.IsNaN(val1))
- {
- return val1;
- }
- if (val1 == val2)
- {
- return double.IsNegative(val1) ? val1 : val2;
- }
- return val2;
- }
- [NonVersionable]
- public static short Min(short val1, short val2)
- {
- return (val1 <= val2) ? val1 : val2;
- }
- [NonVersionable]
- public static int Min(int val1, int val2)
- {
- return (val1 <= val2) ? val1 : val2;
- }
- [NonVersionable]
- public static long Min(long val1, long val2)
- {
- return (val1 <= val2) ? val1 : val2;
- }
- [CLSCompliant(false)]
- [NonVersionable]
- public static sbyte Min(sbyte val1, sbyte val2)
- {
- return (val1 <= val2) ? val1 : val2;
- }
- public static float Min(float val1, float val2)
- {
- // This matches the IEEE 754:2019 `minimum` function
- //
- // It propagates NaN inputs back to the caller and
- // otherwise returns the larger of the inputs. It
- // treats +0 as larger than -0 as per the specification.
- if ((val1 < val2) || float.IsNaN(val1))
- {
- return val1;
- }
- if (val1 == val2)
- {
- return float.IsNegative(val1) ? val1 : val2;
- }
- return val2;
- }
- [CLSCompliant(false)]
- [NonVersionable]
- public static ushort Min(ushort val1, ushort val2)
- {
- return (val1 <= val2) ? val1 : val2;
- }
- [CLSCompliant(false)]
- [NonVersionable]
- public static uint Min(uint val1, uint val2)
- {
- return (val1 <= val2) ? val1 : val2;
- }
- [CLSCompliant(false)]
- [NonVersionable]
- public static ulong Min(ulong val1, ulong val2)
- {
- return (val1 <= val2) ? val1 : val2;
- }
- public static double MinMagnitude(double x, double y)
- {
- // This matches the IEEE 754:2019 `minimumMagnitude` function
- //
- // It propagates NaN inputs back to the caller and
- // otherwise returns the input with a larger magnitude.
- // It treats +0 as larger than -0 as per the specification.
- double ax = Abs(x);
- double ay = Abs(y);
- if ((ax < ay) || double.IsNaN(ax))
- {
- return x;
- }
- if (ax == ay)
- {
- return double.IsNegative(x) ? x : y;
- }
- return y;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static decimal Round(decimal d)
- {
- return decimal.Round(d, 0);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static decimal Round(decimal d, int decimals)
- {
- return decimal.Round(d, decimals);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static decimal Round(decimal d, MidpointRounding mode)
- {
- return decimal.Round(d, 0, mode);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static decimal Round(decimal d, int decimals, MidpointRounding mode)
- {
- return decimal.Round(d, decimals, mode);
- }
- [Intrinsic]
- public static double Round(double a)
- {
- // ************************************************************************************
- // IMPORTANT: Do not change this implementation without also updating MathF.Round(float),
- // FloatingPointUtils::round(double), and FloatingPointUtils::round(float)
- // ************************************************************************************
- // This is based on the 'Berkeley SoftFloat Release 3e' algorithm
- ulong bits = (ulong)BitConverter.DoubleToInt64Bits(a);
- int exponent = double.ExtractExponentFromBits(bits);
- if (exponent <= 0x03FE)
- {
- if ((bits << 1) == 0)
- {
- // Exactly +/- zero should return the original value
- return a;
- }
- // Any value less than or equal to 0.5 will always round to exactly zero
- // and any value greater than 0.5 will always round to exactly one. However,
- // we need to preserve the original sign for IEEE compliance.
- double result = ((exponent == 0x03FE) && (double.ExtractSignificandFromBits(bits) != 0)) ? 1.0 : 0.0;
- return CopySign(result, a);
- }
- if (exponent >= 0x0433)
- {
- // Any value greater than or equal to 2^52 cannot have a fractional part,
- // So it will always round to exactly itself.
- return a;
- }
- // The absolute value should be greater than or equal to 1.0 and less than 2^52
- Debug.Assert((0x03FF <= exponent) && (exponent <= 0x0432));
- // Determine the last bit that represents the integral portion of the value
- // and the bits representing the fractional portion
- ulong lastBitMask = 1UL << (0x0433 - exponent);
- ulong roundBitsMask = lastBitMask - 1;
- // Increment the first fractional bit, which represents the midpoint between
- // two integral values in the current window.
- bits += lastBitMask >> 1;
- if ((bits & roundBitsMask) == 0)
- {
- // If that overflowed and the rest of the fractional bits are zero
- // then we were exactly x.5 and we want to round to the even result
- bits &= ~lastBitMask;
- }
- else
- {
- // Otherwise, we just want to strip the fractional bits off, truncating
- // to the current integer value.
- bits &= ~roundBitsMask;
- }
- return BitConverter.Int64BitsToDouble((long)bits);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static double Round(double value, int digits)
- {
- return Round(value, digits, MidpointRounding.ToEven);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static double Round(double value, MidpointRounding mode)
- {
- return Round(value, 0, mode);
- }
- public static unsafe double Round(double value, int digits, MidpointRounding mode)
- {
- if ((digits < 0) || (digits > maxRoundingDigits))
- {
- throw new ArgumentOutOfRangeException(nameof(digits), SR.ArgumentOutOfRange_RoundingDigits);
- }
- if (mode < MidpointRounding.ToEven || mode > MidpointRounding.ToPositiveInfinity)
- {
- throw new ArgumentException(SR.Format(SR.Argument_InvalidEnumValue, mode, nameof(MidpointRounding)), nameof(mode));
- }
- if (Abs(value) < doubleRoundLimit)
- {
- double power10 = roundPower10Double[digits];
- value *= power10;
- switch (mode)
- {
- // Rounds to the nearest value; if the number falls midway,
- // it is rounded to the nearest value with an even least significant digit
- case MidpointRounding.ToEven:
- {
- value = Round(value);
- break;
- }
- // Rounds to the nearest value; if the number falls midway,
- // it is rounded to the nearest value above (for positive numbers) or below (for negative numbers)
- case MidpointRounding.AwayFromZero:
- {
- double fraction = ModF(value, &value);
- if (Abs(fraction) >= 0.5)
- {
- value += Sign(fraction);
- }
- break;
- }
- // Directed rounding: Round to the nearest value, toward to zero
- case MidpointRounding.ToZero:
- {
- value = Truncate(value);
- break;
- }
- // Directed Rounding: Round down to the next value, toward negative infinity
- case MidpointRounding.ToNegativeInfinity:
- {
- value = Floor(value);
- break;
- }
- // Directed rounding: Round up to the next value, toward positive infinity
- case MidpointRounding.ToPositiveInfinity:
- {
- value = Ceiling(value);
- break;
- }
- default:
- {
- throw new ArgumentException(SR.Format(SR.Argument_InvalidEnumValue, mode, nameof(MidpointRounding)), nameof(mode));
- }
- }
- value /= power10;
- }
- return value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int Sign(decimal value)
- {
- return decimal.Sign(value);
- }
- public static int Sign(double value)
- {
- if (value < 0)
- {
- return -1;
- }
- else if (value > 0)
- {
- return 1;
- }
- else if (value == 0)
- {
- return 0;
- }
- throw new ArithmeticException(SR.Arithmetic_NaN);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int Sign(short value)
- {
- return Sign((int)value);
- }
- public static int Sign(int value)
- {
- return unchecked(value >> 31 | (int)((uint)-value >> 31));
- }
- public static int Sign(long value)
- {
- return unchecked((int)(value >> 63 | (long)((ulong)-value >> 63)));
- }
- [CLSCompliant(false)]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int Sign(sbyte value)
- {
- return Sign((int)value);
- }
- public static int Sign(float value)
- {
- if (value < 0)
- {
- return -1;
- }
- else if (value > 0)
- {
- return 1;
- }
- else if (value == 0)
- {
- return 0;
- }
- throw new ArithmeticException(SR.Arithmetic_NaN);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static decimal Truncate(decimal d)
- {
- return decimal.Truncate(d);
- }
- public static unsafe double Truncate(double d)
- {
- ModF(d, &d);
- return d;
- }
- [DoesNotReturn]
- private static void ThrowMinMaxException<T>(T min, T max)
- {
- throw new ArgumentException(SR.Format(SR.Argument_MinMaxValue, min, max));
- }
- }
- }
|