If you want to use FP numbers in a loop don't do it in the end condition or increment expression. Find a way to use ints & convert them to double inside the loop. For example you can work out what (y-x) / 0.1 is as an int.
You still have doubles on line 5.
There are lots of ways of doing this, look at these:
Oh sorry Ideasman, I think i know what you mean now. just revised. I no longer have an ending condition as a loop nor the increment condition so I hope its okay now. And Ill also continue to read through the wikipedia page, anyways here it is.
Jumping Juniper Berries - Batman!! 83 LOC to compare 2 doubles.You are doing way too much hard work. I really think you need to get out of this habit of looking at things char by char when there are either built in functions or easier methods. See below.
For one, consider that +1.023e-10 is a valid double, as is -1.023E+10 or 1.023E10 or -1.023.
More importantly consider that all we want to do is this: @Irishfitz why couldn't you figure this out yourself? It pretty simple really.
1 2 3 4 5 6 7 8
double begin = 1.0;
double end = 9.0;
double step = 0.1;
int NumberOfTimesToLoop = static_cast<int>( (end - begin)/step ); //should be 80 for this example
for (int a = 0;a < NumberOfTimesToLoop ;++a) {
// your code
}
To compare using a particular precision, do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
constdouble PRECISION = 0.01;
double a = 2.02;
double b = 2.0;
if( (a-b) > PRECISION ) {
std::cout << "a is greater than b within precision\n";
}
elseif ( (a-b) < -PRECISION ) {
std::cout << "a is less than b within precision\n";
}
else {
std::cout << "a is equal to b within precision\n";
}
You probably should make these functions that return bool. For the equality one, compare the absolute value of the difference to the PRECISION.
@Irishfitz
Also, try to learn about the += -= *= /= operators.
I think the problem lies not in the code, but in the means of which you are computing the square root in the first place. For one, it will take forever to do so- plug in 612, and you'll be waiting all day. My recommendation is Newton's Method, which can be applied to square roots with this simple formula:
nth root of A:
xk+1=(1/n)((n-1)xk+(A/xkn-1))
Here, n is whatever root you are taking it to (in this case, 2). A is what you are finding the root of, and xk is the value calculated before the one you're calculating now, where x0 is your initial guess (it could just be the number divided by 2).
So, how is this more efficient? Well, say if we wanted the square root of 12. Now, we know that this reduces down to 2 times the square root of 3, which rounds to be about... 3.4641 or so. Now, using your algorithm, this would take... a lot of iterations to reach, clearly. However, using the one I just mentioned, with a guess of... 6
It has gotten within 3 decimal points by the third iteration. This, compared to your method, which would take (assuming an increment of .001) 3464 increments. That, and yours comes with the issue of floating-point rounding, while this method does not. Essentially, you are going through a lot more difficult of a method than you have to just to calculate a square root. There's no need to increment some billion number of times. Just use the method I provided, and you'll have a sufficient answer within 10 iterations, versus 10000.
As for the guess, just take the input and divide it by 2. It'll be close for smaller values, and if you're doing enough iterations, the difference will be negligible.