Hi everyone!
If I write in pseudocode
include math.h
/*... */
approx. sqrt = sqrt((double) x) + 1
for any unsigned int x, will the approx sqrt of 4 be 3, the approx
sqrt of 5 be 3, the approx sqrt of 48 be 8, and the approx sqrt of 49
be 8?
Thanks
|
|
0
|
|
|
|
Reply
|
albert.xtheunknown0 (176)
|
1/30/2009 10:58:44 PM |
|
On Jan 30, 2:58=A0pm, Albert <albert.xtheunkno...@gmail.com> wrote:
> Hi everyone!
>
> If I write in pseudocode
> include math.h
> /*... */
> approx. sqrt =3D sqrt((double) x) + 1
>
> for any unsigned int x, will the approx sqrt of 4 be 3, the approx
> sqrt of 5 be 3, the approx sqrt of 48 be 8, and the approx sqrt of 49
> be 8?
>
No
--
Fred K
|
|
0
|
|
|
|
Reply
|
fred.l.kleinschmidt (236)
|
1/30/2009 11:04:43 PM
|
|
On Fri, 30 Jan 2009 14:58:44 -0800, Albert wrote:
> Hi everyone!
>
> If I write in pseudocode
> include math.h
> /*... */
> approx. sqrt = sqrt((double) x) + 1
>
> for any unsigned int x, will the approx sqrt of 4 be 3, the approx sqrt
> of 5 be 3, the approx sqrt of 48 be 8, and the approx sqrt of 49 be 8?
You could write the C code and find out for yourself.
What do you mean by approx? From your expression you can have many
approximations: 8, 7.9, 7.93 etc.
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org
|
|
0
|
|
|
|
Reply
|
spamahead (251)
|
1/30/2009 11:14:00 PM
|
|
Albert <albert.xtheunknown0@gmail.com> writes:
> Hi everyone!
>
> If I write in pseudocode
> include math.h
> /*... */
> approx. sqrt = sqrt((double) x) + 1
It would be helpful if you showed us some real code. I guess
"approx. sqrt" is supposed to be an approximate square root, but what
is its type?
> for any unsigned int x, will the approx sqrt of 4 be 3, the approx
> sqrt of 5 be 3, the approx sqrt of 48 be 8, and the approx sqrt of 49
> be 8?
What are you trying to do, and why are you adding 1 to the result?
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
|
|
0
|
|
|
|
Reply
|
kst-u (21545)
|
1/30/2009 11:15:57 PM
|
|
Albert wrote:
> Hi everyone!
>
> If I write in pseudocode
> include math.h
> /*... */
> approx. sqrt = sqrt((double) x) + 1
>
> for any unsigned int x, will the approx sqrt of 4 be 3, the approx
> sqrt of 5 be 3, the approx sqrt of 48 be 8, and the approx sqrt of 49
> be 8?
Do you intend to convert the root-plus-one to integer?
If so, three-quarters of your predictions are correct[*] but
the "approx sqrt" of 48 will be 7, not 8.
[*] Probably. There are few guarantees on the accuracy
of the sqrt() function, and it is conceivable -- unlikely, but
conceivable -- that sqrt(49.0) might yield a number close to
but not exactly equal to 7.0. If it's a tiny bit small, like
6.999999999999998446, you'll get 7, not 8, as the "approx sqrt"
of 49.
What problem are you trying to solve?
--
Eric Sosman
esosman@ieee-dot-org.invalid
|
|
0
|
|
|
|
Reply
|
esosman2 (2945)
|
1/30/2009 11:20:22 PM
|
|
"Albert" <albert.xtheunknown0@gmail.com> wrote in message news:
> Hi everyone!
>
> If I write in pseudocode
> include math.h
> /*... */
> approx. sqrt = sqrt((double) x) + 1
>
> for any unsigned int x, will the approx sqrt of 4 be 3, the approx
> sqrt of 5 be 3, the approx sqrt of 48 be 8, and the approx sqrt of 49
> be 8?
>
sqrt(x), no need for a cast, will give to the square root of x as a
double, within the limits of the sqrt() fucntion accuracy.
To reduce to an integer, the best way is
(int) floor( sqrt(x) + 0.5 );
if you want to always round up for some reason, use
(int) ceil( sqrt(x) );
You can bypass the floor() and ceil() routines with careful casting, but it
is better to get into the habit of doing it properly.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
|
|
0
|
|
|
|
Reply
|
regniztar (3128)
|
1/31/2009 5:54:37 PM
|
|
Albert wrote:
> Hi everyone!
>
> If I write in pseudocode
> include math.h
> /*... */
> approx. sqrt = sqrt((double) x) + 1
>
> for any unsigned int x, will the approx sqrt of 4 be 3, the approx
> sqrt of 5 be 3, the approx sqrt of 48 be 8, and the approx sqrt of 49
> be 8?
When (a) is the largest integer that is
less than or equal to the square root of (n), then:
(n > a * a + a)
is the expression to use to determine whether to round
(a) up or down to get an approximation of the square root.
/* BEGIN new.c output */
n sqrt(n) ap_sq
0 0.000000 0
1 1.000000 1
2 1.414214 1
3 1.732051 2
4 2.000000 2
5 2.236068 2
6 2.449490 2
7 2.645751 3
8 2.828427 3
9 3.000000 3
10 3.162278 3
11 3.316625 3
12 3.464102 3
13 3.605551 4
14 3.741657 4
15 3.872983 4
16 4.000000 4
17 4.123106 4
18 4.242641 4
19 4.358899 4
20 4.472136 4
21 4.582576 5
22 4.690416 5
23 4.795832 5
24 4.898979 5
25 5.000000 5
/* END new.c output */
/* BEGIN new.c */
#include <stdio.h>
#include <math.h>
#define LIMIT 26
unsigned ap_sq(unsigned n);
int main(void)
{
unsigned n;
puts("/* BEGIN new.c output */\n");
puts(" n sqrt(n) ap_sq\n");
for (n = 0; n != LIMIT; ++n) {
printf("%2u %f %2u\n", n, sqrt(n), ap_sq(n));
}
puts("\n/* END new.c output */");
return 0;
}
unsigned ap_sq(unsigned n)
{
unsigned a, b;
a = n;
for (b = ((1 == a) + a) / 2; a > b; b = (n / a + a) / 2) {
a = b;
}
return (n > a * a + a) + a;
}
/* END new.c */
--
pete
|
|
0
|
|
|
|
Reply
|
pfiland (6614)
|
1/31/2009 8:12:40 PM
|
|
|
6 Replies
42 Views
(page loaded in 0.092 seconds)
Similiar Articles: root finding routine - comp.soft-sys.math.mathematicaI want to use the Square Root of Raised Cosine Function as the shapin ... Roots - Bisection method - comp.soft-sys.matlab Find approximate root of function of integer ... Fastest Implementation of Integer Wavelets in VC++ - comp ...> For > integer, you need a multiply, add, shift, add ... scheme is always reversible, but it won't approximate the ... Very fast fixed point implementation of square root for ... Minimum Phase Impulse Response - comp.dspThen, one of the square roots of this rational function will have approximately 2x fewer poles and 2x fewer ... the filter.It becomes worser as the number of roots i.e number ... Sampling: What Nyquist Didn't Say, and What to Do About It - comp ...Sampling at a rate of F/N when N is integer will never help >> subsample a signal ... If you have 1/n harmonic distribution, as from a square wave or from an SCR light ... Convert UTC seconds to actual time. - comp.lang.c... corresponding POSIX "seconds since the Epoch" value is only an approximation of the number ... in the middle of your code, rather > than generating a call to a square-root ... Extended-Precision Division - comp.lang.asm.x86I assume you talk about integer divide, here the 'binary 1/x approximation' wont help on speed. ... asm.x86... transcendental functions, > division, and square root ... How to get envelope from AM signal without phase shift - comp.dsp ...If your HT contains an odd number of taps, the signal at the middle tap will be ... envelope can then be obtained by squaring each, adding, and taking the square root. why first order filter have slope 6db/octave - comp.dspThe "square-root" of this gives the filter. (actually ... Provides hands-on experience with a number of key ... I clearly stated the region where the approximation was ... Fit: Asymmetric errorbars? - comp.graphics.apps.gnuplot... to attack that kind of maximum likelihood >> approximation ... Nelder-mead Algorithm, Doubt on number of Parameters ... least square fit for experimental data - comp.soft-sys ... [comp.publish.cdrom] CD-Recordable FAQ, Part 1/4 - comp.publish ...Archive-name: cdrom/cd-recordable/part1 Posting-Frequency: monthly Last-modified: 2008/10/09 Version: 2.71 Send corrections and updates to And... Methods of computing square roots - Wikipedia, the free encyclopediaGenerally, these methods yield approximate results. To get a higher precision for the root, a higher precision for the square is required and a larger number of steps ... How do you approximate square root of non perfect square numbersApproximating Square Roots To approximate the square root of any non perfect square number, you can perform a series of steps: Guess Divide Average For example, let's ... 7/27/2012 11:32:49 PM
|