Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json\Utilities\MathUtils.cs

Symbol Coverage: 59.62% (31 of 52)

Branch Coverage: 54.29% (19 of 35)

Cyclomatic Complexity Avg: 3.22 Max:7

Code Lines: 52


L V Source
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
 52180
      if (i < 0)
37
 0
        throw new ArgumentOutOfRangeException();
38

  
39
 52180
      if (i == 0)
40
 20840
        return 1;
41

  
42
 31340
      return (int)Math.Floor(Math.Log10(i)) + 1;
43
 52180
    }
44

  
45
    public static int HexToInt(char h)
46
    {
47
 0
      if ((h >= '0') && (h <= '9'))
48
      {
49
 0
        return (h - '0');
50
      }
51
 0
      if ((h >= 'a') && (h <= 'f'))
52
      {
53
 0
        return ((h - 'a') + 10);
54
      }
55
 0
      if ((h >= 'A') && (h <= 'F'))
56
      {
57
 0
        return ((h - 'A') + 10);
58
      }
59
 0
      return -1;
60
 0
    }
61

  
62
    public static char IntToHex(int n)
63
    {
64
 21032
      if (n <= 9)
65
      {
66
 15812
        return (char)(n + 48);
67
      }
68
 5220
      return (char)((n - 10) + 97);
69
 21032
    }
70

  
71
    public static int GetDecimalPlaces(double value)
72
    {
73
      // increasing max decimal places above 10 produces weirdness
74
 3
      int maxDecimalPlaces = 10;
75
 3
      double threshold = Math.Pow(0.1d, maxDecimalPlaces);
76

  
77
 3
      if (value == 0.0)
78
 0
        return 0;
79
 3
      int decimalPlaces = 0;
80
 8
      while (value - Math.Floor(value) > threshold && decimalPlaces < maxDecimalPlaces)
81
      {
82
 5
        value *= 10.0;
83
 5
        decimalPlaces++;
84
      }
85
 3
      return decimalPlaces;
86
 3
    }
87

  
88
    public static int? Min(int? val1, int? val2)
89
    {
90
 561
      if (val1 == null)
91
 558
        return val2;
92
 3
      if (val2 == null)
93
 0
        return val1;
94

  
95
 3
      return Math.Min(val1.Value, val2.Value);
96
 561
    }
97

  
98
    public static int? Max(int? val1, int? val2)
99
    {
100
 374
      if (val1 == null)
101
 372
        return val2;
102
 2
      if (val2 == null)
103
 0
        return val1;
104

  
105
 2
      return Math.Max(val1.Value, val2.Value);
106
 374
    }
107

  
108
    public static double? Min(double? val1, double? val2)
109
    {
110
 0
      if (val1 == null)
111
 0
        return val2;
112
 0
      if (val2 == null)
113
 0
        return val1;
114

  
115
 0
      return Math.Min(val1.Value, val2.Value);
116
 0
    }
117

  
118
    public static double? Max(double? val1, double? val2)
119
    {
120
 374
      if (val1 == null)
121
 374
        return val2;
122
 0
      if (val2 == null)
123
 0
        return val1;
124

  
125
 0
      return Math.Max(val1.Value, val2.Value);
126
 374
    }
127
  }
128
}