You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
4.9 KiB

  1. #region License
  2. // Copyright (c) 2007 James Newton-King
  3. //
  4. // Permission is hereby granted, free of charge, to any person
  5. // obtaining a copy of this software and associated documentation
  6. // files (the "Software"), to deal in the Software without
  7. // restriction, including without limitation the rights to use,
  8. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following
  11. // conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. // OTHER DEALINGS IN THE SOFTWARE.
  24. #endregion
  25. using System;
  26. using System.Collections.Generic;
  27. using System.Text;
  28. namespace Newtonsoft.Json.Utilities
  29. {
  30. internal static class MathUtils
  31. {
  32. public static int IntLength(ulong i)
  33. {
  34. if (i < 10000000000)
  35. {
  36. if (i < 10)
  37. {
  38. return 1;
  39. }
  40. if (i < 100)
  41. {
  42. return 2;
  43. }
  44. if (i < 1000)
  45. {
  46. return 3;
  47. }
  48. if (i < 10000)
  49. {
  50. return 4;
  51. }
  52. if (i < 100000)
  53. {
  54. return 5;
  55. }
  56. if (i < 1000000)
  57. {
  58. return 6;
  59. }
  60. if (i < 10000000)
  61. {
  62. return 7;
  63. }
  64. if (i < 100000000)
  65. {
  66. return 8;
  67. }
  68. if (i < 1000000000)
  69. {
  70. return 9;
  71. }
  72. return 10;
  73. }
  74. else
  75. {
  76. if (i < 100000000000)
  77. {
  78. return 11;
  79. }
  80. if (i < 1000000000000)
  81. {
  82. return 12;
  83. }
  84. if (i < 10000000000000)
  85. {
  86. return 13;
  87. }
  88. if (i < 100000000000000)
  89. {
  90. return 14;
  91. }
  92. if (i < 1000000000000000)
  93. {
  94. return 15;
  95. }
  96. if (i < 10000000000000000)
  97. {
  98. return 16;
  99. }
  100. if (i < 100000000000000000)
  101. {
  102. return 17;
  103. }
  104. if (i < 1000000000000000000)
  105. {
  106. return 18;
  107. }
  108. if (i < 10000000000000000000)
  109. {
  110. return 19;
  111. }
  112. return 20;
  113. }
  114. }
  115. public static char IntToHex(int n)
  116. {
  117. if (n <= 9)
  118. {
  119. return (char)(n + 48);
  120. }
  121. return (char)((n - 10) + 97);
  122. }
  123. public static int? Min(int? val1, int? val2)
  124. {
  125. if (val1 == null)
  126. {
  127. return val2;
  128. }
  129. if (val2 == null)
  130. {
  131. return val1;
  132. }
  133. return Math.Min(val1.GetValueOrDefault(), val2.GetValueOrDefault());
  134. }
  135. public static int? Max(int? val1, int? val2)
  136. {
  137. if (val1 == null)
  138. {
  139. return val2;
  140. }
  141. if (val2 == null)
  142. {
  143. return val1;
  144. }
  145. return Math.Max(val1.GetValueOrDefault(), val2.GetValueOrDefault());
  146. }
  147. public static double? Max(double? val1, double? val2)
  148. {
  149. if (val1 == null)
  150. {
  151. return val2;
  152. }
  153. if (val2 == null)
  154. {
  155. return val1;
  156. }
  157. return Math.Max(val1.GetValueOrDefault(), val2.GetValueOrDefault());
  158. }
  159. public static bool ApproxEquals(double d1, double d2)
  160. {
  161. const double epsilon = 2.2204460492503131E-16;
  162. if (d1 == d2)
  163. {
  164. return true;
  165. }
  166. double tolerance = ((Math.Abs(d1) + Math.Abs(d2)) + 10.0) * epsilon;
  167. double difference = d1 - d2;
  168. return (-tolerance < difference && tolerance > difference);
  169. }
  170. }
  171. }