Not able to find the square of the floating number ...
-
Quote:
double sqrt (double f) { double x, z, tempf; unsigned long *tfptr = ((unsigned long *)&tempf) + 1; tempf = f; *tfptr = (0xbfcdd90a - *tfptr)>>1; x = tempf; z = f*0.5; x = (1.5*x) - (x*x)*(x*z); //The more you make replicates of this statement //the higher the accuracy, here only 2 replicates are used x = (1.5*x) - (x*x)*(x*z); return x*f; }
That is really messy! You are using uninitialized variables (that is garbage). It looks you want to implement the Babylonian method[^] but you do nothing for computing the initial guess. The following code is based on that very Wikipedia page.
void initial_guess(double r, int *pa, int *pn)
{
*pn = 0;while ( r < 1.0)
{
r *= 100.0;
--(*pn);
}
while ( r >= 100.0 )
{
r /= 100.0;
++(*pn);
}*pa = (r < 10.0) ? 2 : 6;
}double square_root(double r)
{
int a, i, n;
initial_guess(r, &a, &n);
double x=1.0;while (n < 0)
{
x/=10.0;
++n;
}
while(n > 0)
{
x *= 10.0;
--n;
}x *= a;
for (i = 0; i<10; ++i) // 10 is the arbitrary number of iterations I chose
{
x = 0.5 * (x + r/x);
}return x;
}THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
You are welcome.
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
Quote:
double sqrt (double f) { double x, z, tempf; unsigned long *tfptr = ((unsigned long *)&tempf) + 1; tempf = f; *tfptr = (0xbfcdd90a - *tfptr)>>1; x = tempf; z = f*0.5; x = (1.5*x) - (x*x)*(x*z); //The more you make replicates of this statement //the higher the accuracy, here only 2 replicates are used x = (1.5*x) - (x*x)*(x*z); return x*f; }
That is really messy! You are using uninitialized variables (that is garbage). It looks you want to implement the Babylonian method[^] but you do nothing for computing the initial guess. The following code is based on that very Wikipedia page.
void initial_guess(double r, int *pa, int *pn)
{
*pn = 0;while ( r < 1.0)
{
r *= 100.0;
--(*pn);
}
while ( r >= 100.0 )
{
r /= 100.0;
++(*pn);
}*pa = (r < 10.0) ? 2 : 6;
}double square_root(double r)
{
int a, i, n;
initial_guess(r, &a, &n);
double x=1.0;while (n < 0)
{
x/=10.0;
++n;
}
while(n > 0)
{
x *= 10.0;
--n;
}x *= a;
for (i = 0; i<10; ++i) // 10 is the arbitrary number of iterations I chose
{
x = 0.5 * (x + r/x);
}return x;
}THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
using
for(i=0;i<5;i++)
{ //float y,sum;
float f= ((float)rand()/(float)(RAND_MAX))*a;
y=square(f);
sum +=y;
}
printf("\n summation of squared number is:=%f\t",sum);gives output but not the exact answer.. any solution actually i want to find the summation of that squared float number..
-
using
for(i=0;i<5;i++)
{ //float y,sum;
float f= ((float)rand()/(float)(RAND_MAX))*a;
y=square(f);
sum +=y;
}
printf("\n summation of squared number is:=%f\t",sum);gives output but not the exact answer.. any solution actually i want to find the summation of that squared float number..
How do you check the result?
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
How do you check the result?
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
Quote:
Generated random number || Squaring of random number: ========================================================= 0.035679 ----- 0.001273 0.079935 ----- 0.006390 0.033320 ----- 0.001110 0.042424 ----- 0.001800 0.012387 ----- 0.000153 summation of squared number is:=0.010346
where the actual summation should be 0.10726
-
Quote:
Generated random number || Squaring of random number: ========================================================= 0.035679 ----- 0.001273 0.079935 ----- 0.006390 0.033320 ----- 0.001110 0.042424 ----- 0.001800 0.012387 ----- 0.000153 summation of squared number is:=0.010346
where the actual summation should be 0.10726
You are wrong, the output of the following program:
int main()
{
int i;
double sum = 0.0;
double a[] =
{
0.035679,
0.079935,
0.03332,
0.042424,
0.012387
};for (i=0; i<sizeof(a)/sizeof(a[0]); ++i)
{
double sq = square_root(a[i]);
printf("a[%d] = %g, sqrt(a[%d])=%g\n", i, a[i], i, sq);
sum += sq;
}
printf("sum of square roots = %g\n", sum);
}is
a[0] = 0.035679, sqrt(a[0])=0.188889
a[1] = 0.079935, sqrt(a[1])=0.282728
a[2] = 0.03332, sqrt(a[2])=0.182538
a[3] = 0.042424, sqrt(a[3])=0.205971
a[4] = 0.012387, sqrt(a[4])=0.111297
sum of square roots = 0.971422That is correct (at least according to Excel :-) ).
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
Quote:
Generated random number || Squaring of random number: ========================================================= 0.035679 ----- 0.001273 0.079935 ----- 0.006390 0.033320 ----- 0.001110 0.042424 ----- 0.001800 0.012387 ----- 0.000153 summation of squared number is:=0.010346
where the actual summation should be 0.10726
Are you needing the sqaure or the square root? PS and the sum of your numbers is 0.010726.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
-
You are wrong, the output of the following program:
int main()
{
int i;
double sum = 0.0;
double a[] =
{
0.035679,
0.079935,
0.03332,
0.042424,
0.012387
};for (i=0; i<sizeof(a)/sizeof(a[0]); ++i)
{
double sq = square_root(a[i]);
printf("a[%d] = %g, sqrt(a[%d])=%g\n", i, a[i], i, sq);
sum += sq;
}
printf("sum of square roots = %g\n", sum);
}is
a[0] = 0.035679, sqrt(a[0])=0.188889
a[1] = 0.079935, sqrt(a[1])=0.282728
a[2] = 0.03332, sqrt(a[2])=0.182538
a[3] = 0.042424, sqrt(a[3])=0.205971
a[4] = 0.012387, sqrt(a[4])=0.111297
sum of square roots = 0.971422That is correct (at least according to Excel :-) ).
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
Are you needing the sqaure or the square root? PS and the sum of your numbers is 0.010726.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
-
How do you compute the squares? This program:
#include <stdio.h>
int main()
{
double a [] =
{
0.035679,
0.079935,
0.033320,
0.042424,
0.012387
};
double sum, square;
int n;sum = 0.0;
for (n=0; n<sizeof(a)/sizeof(a[0]); ++n)
{
square = a[n]*a[n];
sum += square;
printf("%g ----> %g\n", a[n], square);
}printf("sum of squares: %g\n", sum);
return 0;
}gives:
0.035679 ----> 0.00127299
0.079935 ----> 0.0063896
0.03332 ----> 0.00111022
0.042424 ----> 0.0017998
0.012387 ----> 0.000153438
sum of squares: 0.0107261THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite