| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040 |
- // 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.
- /*============================================================
- **
- **
- **
- ** Purpose: Some floating-point math operations
- **
- **
- ===========================================================*/
- //This class contains only static members and doesn't require serialization.
- using System.Diagnostics;
- using System.Runtime;
- 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 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);
- }
- [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)
- {
- var 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)
- {
- var 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.
- var xbits = BitConverter.DoubleToInt64Bits(x);
- var 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
- }
- var regularMod = x % y;
- if (double.IsNaN(regularMod))
- {
- return double.NaN;
- }
- if ((regularMod == 0) && double.IsNegative(x))
- {
- return double.NegativeZero;
- }
- var alternativeResult = (regularMod - (Abs(y) * Sign(x)));
- if (Abs(alternativeResult) == Abs(regularMod))
- {
- var divisionResult = x / y;
- var 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)
- {
- // When val1 and val2 are both finite or infinite, return the larger
- // * We count +0.0 as larger than -0.0 to match MSVC
- // When val1 or val2, but not both, are NaN return the opposite
- // * We return the opposite if either is NaN to match MSVC
- if (double.IsNaN(val1))
- {
- return val2;
- }
- if (double.IsNaN(val2))
- {
- return val1;
- }
- // We do this comparison first and separately to handle the -0.0 to +0.0 comparision
- // * Doing (val1 < val2) first could get transformed into (val2 >= val1) by the JIT
- // which would then return an incorrect value
- if (val1 == val2)
- {
- return double.IsNegative(val1) ? val2 : val1;
- }
- return (val1 < val2) ? val2 : val1;
- }
- [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)
- {
- // When val1 and val2 are both finite or infinite, return the larger
- // * We count +0.0 as larger than -0.0 to match MSVC
- // When val1 or val2, but not both, are NaN return the opposite
- // * We return the opposite if either is NaN to match MSVC
- if (float.IsNaN(val1))
- {
- return val2;
- }
- if (float.IsNaN(val2))
- {
- return val1;
- }
- // We do this comparison first and separately to handle the -0.0 to +0.0 comparision
- // * Doing (val1 < val2) first could get transformed into (val2 >= val1) by the JIT
- // which would then return an incorrect value
- if (val1 == val2)
- {
- return float.IsNegative(val1) ? val2 : val1;
- }
- return (val1 < val2) ? val2 : val1;
- }
- [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)
- {
- // When x and y are both finite or infinite, return the larger magnitude
- // * We count +0.0 as larger than -0.0 to match MSVC
- // When x or y, but not both, are NaN return the opposite
- // * We return the opposite if either is NaN to match MSVC
- if (double.IsNaN(x))
- {
- return y;
- }
- if (double.IsNaN(y))
- {
- return x;
- }
- // We do this comparison first and separately to handle the -0.0 to +0.0 comparision
- // * Doing (ax < ay) first could get transformed into (ay >= ax) by the JIT which would
- // then return an incorrect value
- double ax = Abs(x);
- double ay = Abs(y);
- if (ax == ay)
- {
- return double.IsNegative(x) ? y : x;
- }
- return (ax < ay) ? y : x;
- }
- [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)
- {
- // When val1 and val2 are both finite or infinite, return the smaller
- // * We count -0.0 as smaller than -0.0 to match MSVC
- // When val1 or val2, but not both, are NaN return the opposite
- // * We return the opposite if either is NaN to match MSVC
- if (double.IsNaN(val1))
- {
- return val2;
- }
- if (double.IsNaN(val2))
- {
- return val1;
- }
- // We do this comparison first and separately to handle the -0.0 to +0.0 comparision
- // * Doing (val1 < val2) first could get transformed into (val2 >= val1) by the JIT
- // which would then return an incorrect value
- if (val1 == val2)
- {
- return double.IsNegative(val1) ? val1 : val2;
- }
- return (val1 < val2) ? val1 : 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)
- {
- // When val1 and val2 are both finite or infinite, return the smaller
- // * We count -0.0 as smaller than -0.0 to match MSVC
- // When val1 or val2, but not both, are NaN return the opposite
- // * We return the opposite if either is NaN to match MSVC
- if (float.IsNaN(val1))
- {
- return val2;
- }
- if (float.IsNaN(val2))
- {
- return val1;
- }
- // We do this comparison first and separately to handle the -0.0 to +0.0 comparision
- // * Doing (val1 < val2) first could get transformed into (val2 >= val1) by the JIT
- // which would then return an incorrect value
- if (val1 == val2)
- {
- return float.IsNegative(val1) ? val1 : val2;
- }
- return (val1 < val2) ? val1 : 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)
- {
- // When x and y are both finite or infinite, return the smaller magnitude
- // * We count -0.0 as smaller than -0.0 to match MSVC
- // When x or y, but not both, are NaN return the opposite
- // * We return the opposite if either is NaN to match MSVC
- if (double.IsNaN(x))
- {
- return y;
- }
- if (double.IsNaN(y))
- {
- return x;
- }
- // We do this comparison first and separately to handle the -0.0 to +0.0 comparision
- // * Doing (ax < ay) first could get transformed into (ay >= ax) by the JIT which would
- // then return an incorrect value
- double ax = Abs(x);
- double ay = Abs(y);
- if (ax == ay)
- {
- return double.IsNegative(x) ? x : y;
- }
- return (ax < ay) ? x : 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 Math.Round(double),
- // FloatingPointUtils::round(double), and FloatingPointUtils::round(float)
- // ************************************************************************************
- // If the number has no fractional part do nothing
- // This shortcut is necessary to workaround precision loss in borderline cases on some platforms
- if (a == (double)((long)a))
- {
- return a;
- }
- // We had a number that was equally close to 2 integers.
- // We need to return the even one.
- double flrTempVal = Floor(a + 0.5);
- if ((a == (Floor(a) + 0.5)) && (FMod(flrTempVal, 2.0) != 0))
- {
- flrTempVal -= 1.0;
- }
- return CopySign(flrTempVal, a);
- }
- [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)
- {
- var 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;
- }
- private static void ThrowMinMaxException<T>(T min, T max)
- {
- throw new ArgumentException(SR.Format(SR.Argument_MinMaxValue, min, max));
- }
- }
- }
|