Every now and then I find something
1 2 3
|
unsigned long long n
...
unsigned long long imax = sqrt( n + 0.5 );
|
what to my understanding converts n to real, adds 1/2, computes real sqrt and converts the result to integer (discarding all decimal places). Nosy as I am I'd like to know the difference to
imax = sqrt( n ), so n is integer I guess the sqrt procedure is a different one than for an argument of type real.
To find differences in the result I run following, alas not completely convinced if it makes sense, i) in a fundamental view, and ii) in some details.
Questions about i) are there for sure differences for the two manner to get
int(sqrt(int))? If yes, where? Where in two ways, where documented and where in the number range. Probably I start my search from the wrong end, when I begin at max and decrement the chance for rounding effects could be more present.
Questions about ii) How may I make sure that
imax = sqrt( n ) uses a different routine for sqrt than
imax = sqrt( (real) n )? Besides unwinding the
(i % 1000000000 == 0) test (to get a hint about "progress"), are there options to quicken this drudgery?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream> /* cout, cin, endl */
using namespace std;
int main()
{
for(unsigned long long i = 0; i<=~0ull; i++)
{
unsigned long long p = sqrt( i );
unsigned long long q = sqrt( i + .5 );
if (p != q) cout << "\nGotcha at " << i;
if (i % 1000000000 == 0) cout << "\n" << i;
}
cout << " FIN";
}
|
Edit: formatting