~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/Newtonsoft.Json/Src/Newtonsoft.Json/Utilities/MathUtils.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
26
 
using System;
27
 
using System.Collections.Generic;
28
 
using System.Text;
29
 
 
30
 
namespace Newtonsoft.Json.Utilities
31
 
{
32
 
  internal class MathUtils
33
 
  {
34
 
    public static int IntLength(int i)
35
 
    {
36
 
      if (i < 0)
37
 
        throw new ArgumentOutOfRangeException();
38
 
 
39
 
      if (i == 0)
40
 
        return 1;
41
 
 
42
 
      return (int)Math.Floor(Math.Log10(i)) + 1;
43
 
    }
44
 
 
45
 
    public static int HexToInt(char h)
46
 
    {
47
 
      if ((h >= '0') && (h <= '9'))
48
 
      {
49
 
        return (h - '0');
50
 
      }
51
 
      if ((h >= 'a') && (h <= 'f'))
52
 
      {
53
 
        return ((h - 'a') + 10);
54
 
      }
55
 
      if ((h >= 'A') && (h <= 'F'))
56
 
      {
57
 
        return ((h - 'A') + 10);
58
 
      }
59
 
      return -1;
60
 
    }
61
 
 
62
 
    public static char IntToHex(int n)
63
 
    {
64
 
      if (n <= 9)
65
 
      {
66
 
        return (char)(n + 48);
67
 
      }
68
 
      return (char)((n - 10) + 97);
69
 
    }
70
 
 
71
 
    public static int GetDecimalPlaces(double value)
72
 
    {
73
 
      // increasing max decimal places above 10 produces weirdness
74
 
      int maxDecimalPlaces = 10;
75
 
      double threshold = Math.Pow(0.1d, maxDecimalPlaces);
76
 
 
77
 
      if (value == 0.0)
78
 
        return 0;
79
 
      int decimalPlaces = 0;
80
 
      while (value - Math.Floor(value) > threshold && decimalPlaces < maxDecimalPlaces)
81
 
      {
82
 
        value *= 10.0;
83
 
        decimalPlaces++;
84
 
      }
85
 
      return decimalPlaces;
86
 
    }
87
 
 
88
 
    public static int? Min(int? val1, int? val2)
89
 
    {
90
 
      if (val1 == null)
91
 
        return val2;
92
 
      if (val2 == null)
93
 
        return val1;
94
 
 
95
 
      return Math.Min(val1.Value, val2.Value);
96
 
    }
97
 
 
98
 
    public static int? Max(int? val1, int? val2)
99
 
    {
100
 
      if (val1 == null)
101
 
        return val2;
102
 
      if (val2 == null)
103
 
        return val1;
104
 
 
105
 
      return Math.Max(val1.Value, val2.Value);
106
 
    }
107
 
 
108
 
    public static double? Min(double? val1, double? val2)
109
 
    {
110
 
      if (val1 == null)
111
 
        return val2;
112
 
      if (val2 == null)
113
 
        return val1;
114
 
 
115
 
      return Math.Min(val1.Value, val2.Value);
116
 
    }
117
 
 
118
 
    public static double? Max(double? val1, double? val2)
119
 
    {
120
 
      if (val1 == null)
121
 
        return val2;
122
 
      if (val2 == null)
123
 
        return val1;
124
 
 
125
 
      return Math.Max(val1.Value, val2.Value);
126
 
    }
127
 
  }
 
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
 
 
26
using System;
 
27
using System.Collections.Generic;
 
28
using System.Text;
 
29
 
 
30
namespace Newtonsoft.Json.Utilities
 
31
{
 
32
  internal class MathUtils
 
33
  {
 
34
    public static int IntLength(int i)
 
35
    {
 
36
      if (i < 0)
 
37
        throw new ArgumentOutOfRangeException();
 
38
 
 
39
      if (i == 0)
 
40
        return 1;
 
41
 
 
42
      return (int)Math.Floor(Math.Log10(i)) + 1;
 
43
    }
 
44
 
 
45
    public static char IntToHex(int n)
 
46
    {
 
47
      if (n <= 9)
 
48
        return (char)(n + 48);
 
49
 
 
50
      return (char)((n - 10) + 97);
 
51
    }
 
52
 
 
53
    public static int? Min(int? val1, int? val2)
 
54
    {
 
55
      if (val1 == null)
 
56
        return val2;
 
57
      if (val2 == null)
 
58
        return val1;
 
59
 
 
60
      return Math.Min(val1.Value, val2.Value);
 
61
    }
 
62
 
 
63
    public static int? Max(int? val1, int? val2)
 
64
    {
 
65
      if (val1 == null)
 
66
        return val2;
 
67
      if (val2 == null)
 
68
        return val1;
 
69
 
 
70
      return Math.Max(val1.Value, val2.Value);
 
71
    }
 
72
 
 
73
    public static double? Max(double? val1, double? val2)
 
74
    {
 
75
      if (val1 == null)
 
76
        return val2;
 
77
      if (val2 == null)
 
78
        return val1;
 
79
 
 
80
      return Math.Max(val1.Value, val2.Value);
 
81
    }
 
82
 
 
83
    public static bool ApproxEquals(double d1, double d2)
 
84
    {
 
85
      const double epsilon = 2.2204460492503131E-16;
 
86
 
 
87
      if (d1 == d2)
 
88
        return true;
 
89
 
 
90
      double tolerance = ((Math.Abs(d1) + Math.Abs(d2)) + 10.0) * epsilon;
 
91
      double difference = d1 - d2;
 
92
 
 
93
      return (-tolerance < difference && tolerance > difference);
 
94
    }
 
95
  }
128
96
}
 
 
b'\\ No newline at end of file'