Rebracer update.
1 // --------------------------------------------------------------------------------------------------------------------
2 // <copyright file="DoubleExtensions.cs" company="OxyPlot">
3 // The MIT License (MIT)
5 // Copyright (c) 2012 Oystein Bjorke
7 // Permission is hereby granted, free of charge, to any person obtaining a
8 // copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
15 // The above copyright notice and this permission notice shall be included
16 // in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 // Extension methods for double values.
29 // --------------------------------------------------------------------------------------------------------------------
33 using System.Globalization;
36 /// Provides extension methods for the <see cref="Double"/> type.
38 public static class DoubleExtensions
41 /// Squares the specified value.
49 public static double Squared(this double x)
55 /// Exponent function.
63 public static double GetExponent(this double x)
65 return Math.Round(Math.Log(Math.Abs(x), 10));
69 /// Mantissa function.
70 /// http://en.wikipedia.org/wiki/Mantissa
78 public static double GetMantissa(this double x)
80 return x / Math.Pow(10, x.GetExponent());
84 /// Removes the floating point noise.
86 /// <param name="value">
90 /// A double without noise.
92 public static double RemoveNoise2(this double value)
94 return (double)((decimal)value);
98 /// Removes the floating point noise.
100 /// <param name="value">
103 /// <param name="maxDigits">
104 /// The maximum number of digits.
107 /// A double without noise.
109 public static double RemoveNoise(this double value, int maxDigits = 8)
111 return double.Parse(value.ToString("e" + maxDigits));
115 /// Removes the noise from double math.
117 /// <param name="value">
121 /// A double without noise.
123 public static double RemoveNoiseFromDoubleMath(this double value)
125 if (value.IsZero() || Math.Abs(Math.Log10(Math.Abs(value))) < 27)
127 return (double)((decimal)value);
130 return double.Parse(value.ToString(CultureInfo.InvariantCulture), CultureInfo.InvariantCulture);
134 /// Determines whether the specified value is zero.
136 /// <param name="value">The value.</param>
138 /// <c>true</c> if the specified value is zero; otherwise, <c>false</c>.
140 public static bool IsZero(this double value)
142 return Math.Abs(value) < double.Epsilon;
146 /// Calculates the nearest larger multiple of the specified value.
148 /// <param name="value">
151 /// <param name="step">
155 /// The multiple value.
157 public static double ToUpperMultiple(this double value, double step)
159 var i = (int)Math.Ceiling(value / step);
160 return (step * i).RemoveNoise();
164 /// Calculates the nearest smaller multiple of the specified value.
166 /// <param name="value">
169 /// <param name="step">
173 /// The multiple value.
175 public static double ToLowerMultiple(this double value, double step)
177 var i = (int)Math.Floor(value / step);
178 return (step * i).RemoveNoise();
184 // Gets the mantissa and exponent.
187 /// From <see cref="http://stackoverflow.com/questions/389993/extracting-mantissa-and-exponent-from-double-in-c"/>
189 /// <param name="d">The d.</param>
190 /// <param name="negative">if set to <c>true</c> [negative].</param>
191 /// <param name="mantissa">The mantissa.</param>
192 /// <param name="exponent">The exponent.</param>
193 public static void GetMantissaAndExponent(this double d, out bool negative, out long mantissa, out int exponent)
195 // Translate the double into sign, exponent and mantissa.
196 long bits = BitConverter.DoubleToInt64Bits(d);
198 // Note that the shift is sign-extended, hence the test against -1 not 1
199 negative = (bits < 0);
200 exponent = (int)((bits >> 52) & 0x7ffL);
201 mantissa = bits & 0xfffffffffffffL;
203 // Subnormal numbers; exponent is effectively one higher,
204 // but there's no extra normalisation bit in the mantissa
210 // Normal numbers; leave exponent as it is but add extra
211 // bit to the front of the mantissa
214 mantissa = mantissa | (1L << 52);
217 // Bias the exponent. It's actually biased by 1023, but we're
218 // treating the mantissa as m.0 rather than 0.m, so we need
219 // to subtract another 52 from it.
228 while ((mantissa & 1) == 0)
229 { /* i.e., Mantissa is even */