-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumericTypeSuggester.cs
116 lines (106 loc) · 3.76 KB
/
NumericTypeSuggester.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Numerics;
namespace SuggestNumbersApp
{
internal class NumericTypeSuggester
{
public const string BigInteger = "BigInteger";
public const string ULong = "ulong";
public const string UInt = "uint";
public const string UShort = "ushort";
public const string Byte = "byte";
public const string Long = "long";
public const string Int = "int";
public const string Short = "short";
public const string SByte = "sbyte";
public const string Decimal = "decimal";
public const string Double = "double";
public const string Float = "float";
public const string ErrorNumberType =
"Impossible representation";
public static string GetName(BigInteger minValue,
BigInteger maxValue,
bool IntegersOnly,
bool Accuracy)
{
return IntegersOnly ? GetIntegralNumberName(minValue, maxValue) :
GetFloatingPointNumberName(minValue, maxValue, Accuracy);
}
private static string GetFloatingPointNumberName(BigInteger minValue, BigInteger maxValue, bool accuracy)
{
return accuracy ? GetPreciseFloatNumber(minValue, maxValue) :
GetImpreciseFloatNumber(minValue, maxValue);
}
private static string GetImpreciseFloatNumber(BigInteger minValue, BigInteger maxValue)
{
if (minValue >= new BigInteger(float.MinValue) &&
maxValue <= new BigInteger(float.MaxValue))
{
return Float;
};
if (minValue >= new BigInteger(double.MinValue) &&
maxValue <= new BigInteger(double.MaxValue))
{
return Double;
};
return ErrorNumberType;
}
private static string GetPreciseFloatNumber(BigInteger minValue, BigInteger maxValue)
{
if (minValue >= new BigInteger(decimal.MinValue) && maxValue <= new BigInteger(decimal.MaxValue))
{
return Decimal;
}
return ErrorNumberType;
}
private static string GetIntegralNumberName(BigInteger minValue, BigInteger maxValue)
{
return minValue >= 0 ? GetUnsignedIntegralNumberName(maxValue) :
GetSignedIntegralNumberName(minValue, maxValue);
}
private static string GetSignedIntegralNumberName(BigInteger minValue, BigInteger maxValue)
{
if (minValue >= sbyte.MinValue &&
maxValue <= sbyte.MaxValue)
{
return SByte;
}
if (minValue >= short.MinValue &&
maxValue <= short.MaxValue)
{
return Short;
}
if (minValue >= int.MinValue &&
maxValue <= int.MaxValue)
{
return Int;
}
if (minValue >= long.MinValue &&
maxValue <= long.MaxValue)
{
return Long;
}
return BigInteger;
}
private static string GetUnsignedIntegralNumberName(BigInteger maxValue)
{
if (maxValue <= byte.MaxValue)
{
return Byte;
}
if (maxValue <= ushort.MaxValue)
{
return UShort;
}
if (maxValue <= uint.MaxValue)
{
return UInt;
}
if (maxValue <= ulong.MaxValue)
{
return ULong;
}
return BigInteger;
}
}
}