-
Notifications
You must be signed in to change notification settings - Fork 11
/
DivideUsingBSWithPrecision.cpp
62 lines (57 loc) · 1.25 KB
/
DivideUsingBSWithPrecision.cpp
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
#include <bits/stdc++.h>
using namespace std;
// binary search to find quotient
int getQuotient(int divisor, int divident)
{
int s = 0, e = divident;
int ans = 0;
while (s <= e)
{
int mid = s + (e - s) / 2;
if (mid * divisor == divident)
{
return mid;
}
else if (mid * divisor < divident)
{
// right and update ans as we need to find floor value
ans = mid;
s = mid + 1;
}
else
{
// left
e = mid - 1;
}
}
return ans;
}
double getQuotientWithPrecision(int divisor, int divident)
{
double quotient = getQuotient(divisor, divident);
double step = 0.1;
int precision = 10;
for (int i = 0; i < precision; i++)
{
double temp = quotient;
while (temp * divisor < divident)
{
quotient = temp;
temp += step;
}
step /= 10;
}
return quotient;
}
int main()
{
int divisor = -3;
int divident = 571;
double ans = getQuotientWithPrecision(abs(divisor), abs(divident));
if ((divisor < 0 && divident > 0) || (divisor > 0 && divident < 0))
{
ans = -ans;
}
printf("%.10f\n", ans);
return 0;
}