Good day!
The following question arose: for example, we have a double variable and some accuracy (precition variable), which will determine the number of decimal places of interest:
double x = 9.3813020199999; double precition = 0.001; Knowing the above data, we need to get the exact number 9.381 (and without extra zeros at the end). Is there a way to do this?
UPD I need to test the Sqrt method (code below) and I don’t know how to properly write a test for NUnit if the method returns a double. After all, we should have an expected-value that is compared with the return value of the method.
public static double Sqrt(double x, int n, double precition) { if (x < 0 && n % 2 == 0) throw new ArgumentException(); double result = x / n; double previousResult; do { previousResult = result; result = ((double)1 / n) * ((n - 1) * previousResult + (x / Sqr(previousResult, n - 1))); } while (Math.Abs(result - previousResult) > precition); return result; } So far, my testing method, written using NUnit, looks like this:
[TestFixture] public class NewtonSqrtTests { [TestCase(4, 2, 0.001, ExpectedResult = 2.000)] [TestCase(27, 3, 0.0001, ExpectedResult = 3.0000)] [TestCase(88, 2, 0.001, ExpectedResult = 9.380)] [TestCase(81, 2, 0.001, ExpectedResult = 9.000)] public double Sqrt_PositiveTest(double x, int n, double precition) { return NewtonSqrt.Sqrt(x, n, precition); } }
precitionis exactly 0.001? Not so: ideone.com/TgKjVj - VladDdoubletype. - VladD