-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBitRepresentationOfNumBetween0And1.cs
123 lines (105 loc) · 3.46 KB
/
BitRepresentationOfNumBetween0And1.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
117
118
119
120
121
122
123
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Text;
// Display a 32 bit representation of of a number between 0 and 1 as a string.
namespace BitRepresentationOfNumBetween0And1
{
public class DoubleToBitsAsString
{
const int MAX_BITS = 32;
public string Convert(double num)
{
if (num > 1 || num < 0) { throw new ArgumentOutOfRangeException(); }
double div = 1D / 2D;
var sb = new StringBuilder(".");
int count = 0;
while (num > 0)
{
if (count >= MAX_BITS) { throw new Exception(); }
if (num >= div)
{
num -= div;
sb.Append(1);
}
else
{
sb.Append(0);
}
count++;
div /= 2;
}
return sb.ToString();
}
}
[TestClass]
public class BitRepresentationOfNumBetween0And1
{
[TestMethod]
public void WhenHalf_ExpectOnlyHighestBitSet()
{
var converter = new DoubleToBitsAsString();
Assert.AreEqual(".1", converter.Convert(0.5D));
}
[TestMethod]
public void WhenQuarter_ExpectOnlySecondHighestBitSet()
{
var converter = new DoubleToBitsAsString();
Assert.AreEqual(".01", converter.Convert(0.25D));
}
[TestMethod]
public void WhenMultipleBitsSet_ExpectCorrectConversion()
{
var converter = new DoubleToBitsAsString();
Assert.AreEqual(".011", converter.Convert(0.375D));
}
[TestMethod]
[ExpectedException(typeof(Exception))]
public void TestMethod_SmallestQuantum()
{
var converter = new DoubleToBitsAsString();
converter.Convert(double.Epsilon);
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void TestMethod_Max()
{
var converter = new DoubleToBitsAsString();
converter.Convert(double.MaxValue);
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void TestMethod_LargestNegative()
{
var converter = new DoubleToBitsAsString();
converter.Convert(double.MinValue);
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void TestMethod_PostiveInfinity()
{
var converter = new DoubleToBitsAsString();
converter.Convert(double.PositiveInfinity);
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void TestMethod_NegativeInfinity()
{
var converter = new DoubleToBitsAsString();
converter.Convert(double.NegativeInfinity);
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void WhenGreaterThanOne_ExpectRangeException()
{
var converter = new DoubleToBitsAsString();
converter.Convert(1.1D);
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void WhenNegative_ExpectRangeException()
{
var converter = new DoubleToBitsAsString();
converter.Convert(-0.1D);
}
}
}