Created
June 27, 2017 04:46
-
-
Save jianminchen/bed8a6d9b5469b3deccad552636ef1a6 to your computer and use it in GitHub Desktop.
Root of a number - error range - June 26, 2017 - debugging code, bug fix on line 74 (int)(value * 1000), forget () to wrap value * 1000.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
class Solution | |
{ | |
// 0 , 0.001, 3=> 0.1, x = 7, n = 3 | |
static double root(double x, uint n) | |
{ | |
// your code goes here | |
if( x < 0) | |
{ | |
return -1; | |
} | |
if( Math.Abs(x) < 0.001) | |
{ | |
return 0; // 0 | |
} | |
// assume that x is nonnagative | |
var search = binarySearch(x, n); // 0.001, 3 | |
return search; | |
} | |
/// 0.001, 3 => 0.1 from 0 to 1, 1000 numbers, | |
private static double binarySearch(double x, uint n) | |
{ | |
double start = 0; | |
double end = Math.Max(1, x); // Math.Max(1, x), x < 1, x > 1, x , [0 7] | |
int index = 0; | |
while(start <= end) // 0, 0.001 | |
{ | |
index++; | |
if(index < 20) | |
{ | |
Console.WriteLine("start=" + start); | |
Console.WriteLine("end=" + end); | |
Console.WriteLine(index); | |
} | |
else | |
{ | |
break; | |
} | |
var middle = keep3Decimal(start + (end - start)/ 2.0); // keep to 3 decimal, 0, middle = 7/2.0 = 3.500, 1.7 | |
var middleValue = Math.Pow(middle, n); // 3.5^3 > 7 | |
bool isEqual = Math.Abs(middleValue - x) < 0.001; | |
bool isLess = x - middleValue > 0.001; | |
if(isEqual) | |
{ | |
return middle; // | |
} | |
else if(isLess) | |
{ | |
start = middle + 0.001; // 1.701 - 1.7 | |
} | |
else | |
{ | |
end = middle - 0.001; // 3.5 - 0.001 = 3.499 | |
} | |
} | |
return -1; // not found | |
} | |
/// | |
private static double keep3Decimal(double value) | |
{ | |
int integerValue = (int)(value * 1000); | |
return integerValue/ 1000.0; // double | |
} | |
static void Main(string[] args) | |
{ | |
Console.WriteLine(root(7, 3)); | |
} | |
} | |
// x = 7, n = 3, 1.913 | |
// x > 1, Math.Abs(1.913 ^ 3 - 7) <= 0.001 | |
// x > 1, > 1, lower bound 1, upper bound <= 7 | |
// binary search - error range 0.001, 7000, log(n) = 10 * log7 , 30 times, find the number | |
// x = 9 , n = 2, 3.0 = 9, 2.999 -3 = 0.001, 3.000, 2.999 | |
// x < 1, upper bound 1, lower bound - nonnegative number | |
// min(0, 1), max ( 1, x) | |
// < 0.001, they are equal | |
// double -> convert to 3 decimal | |
// double d , d * 1000 -> integer - type conversion - double -> integer -> 1000.0 | |
// 0.1001 -> 100 -> 100/ 1000.0 = 0.1 00 -> fourth decimal 0.0005 -> 0.001 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Julia did white boarding test on her own code, and then she found a bug on line 29 on test case 0.001, n = 3, search value 0.1. Learn to test your own code very carefully, and then find a bug on line 29. But Julia missed the bug on line 74. The peer asked the question to point out the bug on line 75, return integerValue/ 1000.0; not integerValue/ 1.0.