So I'm doing this project where, using binary search, I have to plug in values into a formula and check if the output is approximately equal to my key. Because this formula I'm plugging into has to do with area of a circle, I need it to be approximately equal to the key by 7 decimal places.
double binarySearch(min, max, key){
int mid;
if(max > min)
{
mid = (1 + (max-min)/2)/1;
if(funtion(mid) == key)
{
return mid;
}
if(function(mid) > key)
{
return binarySearch(min, mid-1, key);
}
if(function(mid) < key)
{
return binarySearch(mid+1, max, key);
}
return -1;
}
}
so I know its pointless to check if(function(mid)==key) because its never going to be exactly equal. I'm just not sure how to return an answer that's accurate to 7 decimal places (or approximately equal to our key).