473,320 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Fast sincos routine

Hello,

Does anyone have any suggestions for where to find a good sincos
routine (i.e. a routine that calculates both the sin and cos of a
given argument) written in c?

Thanks,

Oc**********@yahoo.com
Nov 14 '05 #1
45 9344
OcelotIguana wrote:

Hello,

Does anyone have any suggestions for where to find a good sincos
routine (i.e. a routine that calculates both the sin and cos of a
given argument) written in c?

Thanks,

Oc**********@yahoo.com


Do you have a math co-processor available or does it have to be software?
If the former, they mostly have the function built in, so you had best
do it in assembler.

If you want to do it entirely in high-level C, use the following scheme:

1. Reduce the angle x to the range (0, pi/2)

2. Let z = tan(x/2),
then sin(x) = 2*z/(1+z*z), cos(x) = (1-z*z)/(1+z*z) .

3. Compute z using the continued fraction representation, with w = u*u :

tan(u) = u/(1-w/(3-w/(5-w/(7-...) ) ) )

The only problem with continued fractions is that each step requires
a division, which on Pentia is much slower than a multiply.

If your system already has tan(u), you are home free: compute tan(x/2)
and then sine and cosine with 2 divisions, 2 multiplications and 2 additions.
The arithmetic will take negligible time relative to evaluating the tangent.
--
Julian V. Noble
Professor Emeritus of Physics
jv*@lessspamformother.virginia.edu
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"God is not willing to do everything, and thereby take away our free wiil
and that share of glory that rightfully belongs to ourselves."
-- N. Machiavelli, "The Prince".
Nov 14 '05 #2
OcelotIguana wrote:

Does anyone have any suggestions for where to find a good sincos
routine (i.e. a routine that calculates both the sin and cos of a
given argument) written in c?


void sincos(const double x, double *sinval, double *cosval)
{
*sinval = sin(x);
*cosval = sqrt(1.0 - *sinval * *sinval);
}

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #3
In article <40***************@yahoo.com>, CBFalconer wrote:
OcelotIguana wrote:

Does anyone have any suggestions for where to find a good sincos
routine (i.e. a routine that calculates both the sin and cos of a
given argument) written in c?


void sincos(const double x, double *sinval, double *cosval)
{
*sinval = sin(x);
*cosval = sqrt(1.0 - *sinval * *sinval);
}


If you need perfect accuracy, and/or your computer has good floating
point hardware, that routine will be hard to beat. For merely good
accuracy, and with integer-only hardware, you need a CORDIC algorithm,
which is not normally part of the standard library. I have used
Ken Turkowski's copy, which he has posted at
http://www.worldserver.com/turk/opensource/Cordic.c.txt
It doesn't take many changes[*] to get it to compile with
gcc -std=c99 -pedantic -W -Wall -Wredundant-decls -Wpointer-arith \
-Wcast-qual -Wshadow -O2
For embedded work with a StrongARM, this saved me a bucket of CPU cycles.

- Larry
[*] If you want a copy of my patches, e-mail me.
Nov 14 '05 #4
CBFalconer wrote:

OcelotIguana wrote:

Does anyone have any suggestions for where to find a good sincos
routine (i.e. a routine that calculates both the sin and cos of a
given argument) written in c?


void sincos(const double x, double *sinval, double *cosval)
{
*sinval = sin(x);
*cosval = sqrt(1.0 - *sinval * *sinval);
}


Alas, this tells me that the cosine of pi (180 degrees)
is positive, when any schoolchild knows it's negative.

--
Er*********@sun.com
Nov 14 '05 #5
Eric Sosman wrote:
CBFalconer wrote:
OcelotIguana wrote:
Does anyone have any suggestions for where to find
a good sincos routine (i.e. a routine that calculates
both the sin and cos of a given argument) written in c?

void sincos(const double x, double *sinval, double *cosval){
*sinval = sin(x);
*cosval = sqrt(1.0 - *sinval * *sinval);
}

Alas, this tells me that the cosine of pi (180 degrees) is positive,
when any schoolchild knows it's negative.


As any honors student
in Mathematics and Physics at McGill University should know. :-)

Nov 14 '05 #6

"Eric Sosman" <Er*********@sun.com> wrote in message
news:40***************@sun.com...
CBFalconer wrote:

OcelotIguana wrote:

Does anyone have any suggestions for where to find a good sincos
routine (i.e. a routine that calculates both the sin and cos of a
given argument) written in c?


void sincos(const double x, double *sinval, double *cosval)
{
*sinval = sin(x);
*cosval = sqrt(1.0 - *sinval * *sinval);
}


Alas, this tells me that the cosine of pi (180 degrees)
is positive, when any schoolchild knows it's negative.


but sqrt of a positive number can be positive or negative

e.g. sqrt(4) = +/- 2
Nov 14 '05 #7
On Mon, 1 Mar 2004 23:03:53 -0000, in comp.lang.c , "Allan Bruce"
<al*****@TAKEAWAYf2s.com> wrote:

"Eric Sosman" <Er*********@sun.com> wrote in message
news:40***************@sun.com...

Alas, this tells me that the cosine of pi (180 degrees)
is positive, when any schoolchild knows it's negative.


but sqrt of a positive number can be positive or negative

e.g. sqrt(4) = +/- 2


True, but useless. sqrt() doesn't return a vector, or even a -ve value..
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #8
Allan Bruce wrote:
Eric Sosman wrote:
CBFalconer wrote:
OcelotIguana wrote:

Does anyone have any suggestions for where to find a good sincos
routine (i.e. a routine that calculates both the sin and cos of a
given argument) written in c?

void sincos(const double x, double *sinval, double *cosval) {
*sinval = sin(x);
*cosval = sqrt(1.0 - *sinval * *sinval);
}


Alas, this tells me that the cosine of pi (180 degrees)
is positive, when any schoolchild knows it's negative.


but sqrt of a positive number can be positive or negative

e.g. sqrt(4) = +/- 2


But the result of the standard C library function sqrt(double) cannot.

Nov 14 '05 #9

but sqrt of a positive number can be positive or negative

e.g. sqrt(4) = +/- 2


But the result of the standard C library function sqrt(double) cannot.


Its not difficult to implement though to get the correct answer.
Nov 14 '05 #10
Allan Bruce wrote:
but sqrt of a positive number can be positive or negative

e.g. sqrt(4) = +/- 2


But the result of the standard C library function sqrt(double) cannot.

It's not difficult to implement though to get the correct answer.


This is the comp.lang.c newsgroup.

Please show us the implementation in C.

Nov 14 '05 #11
"E. Robert Tisdale" wrote:
Allan Bruce wrote:
but sqrt of a positive number can be positive or negative

e.g. sqrt(4) = +/- 2

But the result of the standard C library function sqrt(double)
cannot.


It's not difficult to implement though to get the correct answer.


This is the comp.lang.c newsgroup.
Please show us the implementation in C.


You are not overly intelligent, are you?

negroot = -(posroot = sqrt(x));

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #12

"CBFalconer" <cb********@yahoo.com> wrote in message
news:40***************@yahoo.com...
"E. Robert Tisdale" wrote:
Allan Bruce wrote:
> but sqrt of a positive number can be positive or negative
>
> e.g. sqrt(4) = +/- 2

But the result of the standard C library function sqrt(double)
cannot.

It's not difficult to implement though to get the correct answer.


This is the comp.lang.c newsgroup.
Please show us the implementation in C.


You are not overly intelligent, are you?

negroot = -(posroot = sqrt(x));


Thank you. I thought he was taking the mickey asking for an implementation!
Allan
Nov 14 '05 #13
In article <c2**********@news.freedom2surf.net> "Allan Bruce" <al*****@TAKEAWAYf2s.com> writes:
"Eric Sosman" <Er*********@sun.com> wrote in message
news:40***************@sun.com...
CBFalconer wrote:
void sincos(const double x, double *sinval, double *cosval)
{
*sinval = sin(x);
*cosval = sqrt(1.0 - *sinval * *sinval);
}


Alas, this tells me that the cosine of pi (180 degrees)
is positive, when any schoolchild knows it's negative.


but sqrt of a positive number can be positive or negative

e.g. sqrt(4) = +/- 2


Not in mathematics or in C. The above is valid for x in [-pi/2,pi/2],
but a more precise implementation is:
*cosval = sqrt((1.0 - *sinval) * (1.0 + *sinval));
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 14 '05 #14
CBFalconer wrote:

"E. Robert Tisdale" wrote:
Allan Bruce wrote:
> but sqrt of a positive number can be positive or negative
>
> e.g. sqrt(4) = +/- 2

But the result of the standard C library function sqrt(double)
cannot.

It's not difficult to implement though to get the correct answer.


This is the comp.lang.c newsgroup.
Please show us the implementation in C.


You are not overly intelligent, are you?

negroot = -(posroot = sqrt(x));


Good, but not yet good enough. One additional bit
of information needs to be computed: Which of `posroot'
and `negroot' is *the* cosine of `x'?

One way to choose the sign would be to use fmod()
to determine how many multiples of pi/2 are present, but
it's rather difficult to form an exact representation of
pi/2 in C's native arithmetic types ... As Plauger says
somewhere in "The Standard C Library," the library's
trig functions are likely to do a better job than the
caller can of throwing out extraneous multiples of pi.
(Elsewhere, this has been called "pi throwing.") The
upshot is that the library's best answer to the O.P.'s
request is probably

void sincos(double x, double *psin, double *pcos) {
*psin = sin(x);
*pcos = cos(x);
}

.... which I imagine the O.P. would find unsatisfactory.

--
Er*********@sun.com
Nov 14 '05 #15

"Eric Sosman" <Er*********@sun.com> wrote in message
news:40***************@sun.com...
CBFalconer wrote:
The
upshot is that the library's best answer to the O.P.'s
request is probably

void sincos(double x, double *psin, double *pcos) {
*psin = sin(x);
*pcos = cos(x);
}

... which I imagine the O.P. would find unsatisfactory.

Unless using a compiler which automatically optimizes this combination.
Another possibility, which political correctness probably forbids
mentioning, is to look up the specific capabilities of the platform in use.
Nov 14 '05 #16
Thanks to everyone who has posted in response to my original message.

Perhaps I should clarify what I am asking. I have a suite of
numerical codes that I recently profiled and found that the standard
sin and cos routines are a huge percentage of my total run time (which
is many days). A colleague told me that the standard math libraries
are optimized for size, not speed, and that since I always call sin
and cos of the same argument, I should look for a speed optimized
sincos routine which shouldn't take much more time than either a sin
or cos take individually while maintaining the same level of accuracy.
BTW, I'm using Borland C++ Builder 6.0 on a Pentium IV in Win2k.

Ideally, this fast sincos would be something already written that has
been speed optimized (without sacrificing accuracy) by people much
smarter about this issue than myself...

Thanks again,

Oc**********@yahoo.com
Nov 14 '05 #17
In article <e2**************************@posting.google.com >,
Oc**********@yahoo.com (OcelotIguana) wrote:
Thanks to everyone who has posted in response to my original message.

Perhaps I should clarify what I am asking. I have a suite of
numerical codes that I recently profiled and found that the standard
sin and cos routines are a huge percentage of my total run time (which
is many days). A colleague told me that the standard math libraries
are optimized for size, not speed, and that since I always call sin =============================

Usually they are optimised for accuracy.
and cos of the same argument, I should look for a speed optimized
sincos routine which shouldn't take much more time than either a sin
or cos take individually while maintaining the same level of accuracy.
BTW, I'm using Borland C++ Builder 6.0 on a Pentium IV in Win2k.


Have you looked at reducing the number of calls to sin and cos? For
example, consecutive values of sin (a + k * b), for k = 0, 1, 2, 3, etc.
can be calculated very easily with a single multiplication and addition.
Anything doing 3D graphics can usually be done with hardly any
trigonometric functions at all.

Do you have values that are very close together?

sin (x + eps) = sin (x) * cos (eps) + sin (eps) * cos (x)
cos (x + eps) = cos (x) * cos (eps) - sin (eps) * sin (x)

(You better check these)

If eps is small enough then you can replace cos (eps) with 1, sin (eps)
with eps and get

sin (x + eps) = sin (x) + eps * cos (x)
cos (x + eps) = cos (x) - eps * sin (x)

Grab the source of an existing implementation of sin and cos. They all
do two steps, for example for sin (x):

Step 1: Given x, find k such that abs (x - k * pi/2) <= pi/4.
Step 2: Let y = x - k * pi/2.
Step 3: Calculate one of sin(y), cos (y), -sin(y), -cos(y),
depending on the last two bits of k, using a polynomial.

By calculating sin and cos simultaneously, you know both will have the
same k and y. You also will have to calculate both sin(y) and cos(y)
using two polynomials, then just pick the right results and apply the
sign. So you win by just merging two such implementations.

If you have many calls, chances are the arguments are close together, so
many consecutive arguments will use the same value k. Try writing a
vectorised function:

void vec_sincos (double s[], double c[], double x[], size_t n);

where you will lose lots of the overhead and give the compiler a chance
of optimising.

BTW. Profilers have been known to lie, especially for small function
calls. Just write a test program that does a billion calls to sin and
cos, profile it, and compare the results with stopwatch results to make
sure you are not going down the wrong path.
Nov 14 '05 #18
OcelotIguana wrote:

Thanks to everyone who has posted in response to my original message.

Perhaps I should clarify what I am asking. I have a suite of
numerical codes that I recently profiled and found that the standard
sin and cos routines are a huge percentage of my total run time (which
is many days). A colleague told me that the standard math libraries
are optimized for size, not speed, and that since I always call sin
and cos of the same argument, I should look for a speed optimized
sincos routine which shouldn't take much more time than either a sin
or cos take individually while maintaining the same level of accuracy.
BTW, I'm using Borland C++ Builder 6.0 on a Pentium IV in Win2k.

Ideally, this fast sincos would be something already written that has
been speed optimized (without sacrificing accuracy) by people much
smarter about this issue than myself...

Thanks again,

Oc**********@yahoo.com


If you don't need lots of precision, you can use table lookup. (basically,
you hash into the tables) as follows: suppose your basic range is

0 to +pi/4 = 45 deg ;

divide that range into--say--91 steps of 0.5 deg, as in Abramowitz & Stegun's
tables. The first thing you do is fill the sin and cos tables. Then, after
converting the angle to fit in the range, multiply by an appropriate factor
(2 in this case) and extract the integer part to get an integer in the range
0 to 89, call it k, plus a fraction x between 0 and 1. Then you evaluate

sin(k) * (1-x) + sin(k+1) * x

and

cos(k) * (1-x) + cos(k+1) * x

for linear interpolation. On modern machines it is best to increase the
number of stored values to increase precision, rather than performing an
interpolation of higher degree. (You are trading space for speed, as usual.)
Julian V. Noble
Professor Emeritus of Physics
jv*@lessspam.virginia.edu
^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"For there was never yet philosopher that could endure the toothache
patiently." -- Wm. Shakespeare, Much Ado about Nothing. Act v. Sc. 1.
Nov 14 '05 #19
Thanks to everyone!
Nov 14 '05 #20
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message news:<c2**********@news.freedom2surf.net>...
but sqrt of a positive number can be positive or negative

e.g. sqrt(4) = +/- 2


This is false. The square root of a positive number is always
positive.

You are thinking of the example they teach you in school:

sqrt((x^2)) = +/- x

This doesn't mean that there are two answers, one positive and one
negative; it's poor notation meaning that the answer is always
positive, even if x is negative.

For example, if x = -2,

sqrt((-2)^2) = 2 (not +/-2)

Thus, the "solution" in this case is "-x".

If you don't believe me, pick up any algebra schoolbook or graphing
calculator and find the graph for f(x) = sqrt(x). The graph clearly
passes the "vertical line test" and is therefore a function (although
I hope you already knew that); and we all SHOULD know that functions
have a UNIQUE y-value for every x.
Nov 14 '05 #21
Robert Futrell wrote:
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
but sqrt of a positive number can be positive or negative

e.g. sqrt(4) = +/- 2


This is false. The square root of a positive number is always
positive.


You are just the man I want to see. I have all these marvellous
proofs, about 1 == 0, and things like that. You will be able to
immediately see that they are accurate and incontrovertible.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #22
In article <53**************************@posting.google.com >,
bo********@aol.com (Robert Futrell) wrote:
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:<c2**********@news.freedom2surf.net>...
but sqrt of a positive number can be positive or negative

e.g. sqrt(4) = +/- 2


This is false. The square root of a positive number is always
positive.


You are simply wrong, read any math textbook.

Nov 14 '05 #23
On 5 Mar 2004 10:43:49 -0800, in comp.lang.c , bo********@aol.com (Robert
Futrell) wrote:
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message news:<c2**********@news.freedom2surf.net>...
but sqrt of a positive number can be positive or negative

e.g. sqrt(4) = +/- 2
This is false. The square root of a positive number is always
positive.


eh? A square has two roots, one positive and one negative.
This doesn't mean that there are two answers, one positive and one
negative;
Well, yes it does.
and we all SHOULD know that functions have a UNIQUE y-value for every x.


garbage.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #24
In article <kc********************************@4ax.com> Mark McIntyre <ma**********@spamcop.net> writes:
On 5 Mar 2004 10:43:49 -0800, in comp.lang.c , bo********@aol.com (Robert
Futrell) wrote:
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message news:<c2**********@news.freedom2surf.net>...
but sqrt of a positive number can be positive or negative

e.g. sqrt(4) = +/- 2


This is false. The square root of a positive number is always
positive.


eh? A square has two roots, one positive and one negative.


Let us put this in perspective. In mathematics, the function "sqrt",
when presented with a positive number gives a positive result.
Otherwise it would not be a function. (In mathematics a function
is something that gives a single result when it is given an argument.
You have to go to Riemann surfaces to have a complete function sqrt.)

The *equation* x^2 = 2 has two roots, one of them is sqrt(2) and the
other is -sqrt(2).

I know about the proofs CBFalconer alludes to. When using the sqrt
function they assume that with complex argument and complex result
sqrt(a * b) = sqrt(a) * sqrt(b),
this equation is false.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 14 '05 #25


Dik T. Winter wrote:

I know about the proofs CBFalconer alludes to. When using the sqrt
function they assume that with complex argument and complex result
sqrt(a * b) = sqrt(a) * sqrt(b),
this equation is false.


Huh?

using pure mathematics ...

If we sqaure both sides of the equation we get

sqrt(a*b) ^ 2 = sqrt(a) ^ 2 * sqrt(b) ^ 2

since sqrt(x)^2 = x for all x
(by definition)

we get a * b = a * b

What am I missing?

As far as I know, the above is true
even for complex numbers, i.e. sqrt(-1).

Please explain.

--
Ñ
"It is impossible to make anything foolproof because fools are so
ingenious" - A. Bloch

Nov 14 '05 #26
CBFalconer <cb********@yahoo.com> wrote in message news:<40**************@yahoo.com>...
Robert Futrell wrote:
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
but sqrt of a positive number can be positive or negative

e.g. sqrt(4) = +/- 2


This is false. The square root of a positive number is always
positive.


You are just the man I want to see. I have all these marvellous
proofs, about 1 == 0, and things like that. You will be able to
immediately see that they are accurate and incontrovertible.


Thanks for the witty reply, but I was taking the notation "sqrt(x)" to
mean you wanted to compute the answer equivalent to math's

--
\/x

function. Here's a source from a professor to back me up on this,
since I know you won't believe me:

http://math.vanderbilt.edu/~schectex/commerrs/#Radicals

In short, positive numbers DO have two roots, but when you write

--
\/x

in math, that literally means "the nonnegative root". That is why the
graph of y=sqrt(x) does NOT have any negative y-values, and why the
square root function IS A FUNCTION. I hope you are not conjecturing
that square root is not a function...

I apologize, my previous post should have been more clear.

And here is a proof for you; see if you can spot an error in it:

Suppose
-----
\/(x^2) = +/- x (both) for x>0. Then

-----
\/(x^2) = -x. Since

-----
\/(x^2) <=> x^(2/2), we have

x^(2/2) = -x

=> x^1 = -x

=> x = -x

which is a contradiction. Thus, it is not true that

-----
\/(x^2) = +/- x (both) for x>0.
Nov 14 '05 #27
Clark Cox <cl*******@mac.com> wrote in message news:<clarkcox3-EF0789.16235505032004@localhost>...
In article <53**************************@posting.google.com >,
bo********@aol.com (Robert Futrell) wrote:
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:<c2**********@news.freedom2surf.net>...
but sqrt of a positive number can be positive or negative

e.g. sqrt(4) = +/- 2


This is false. The square root of a positive number is always
positive.


You are simply wrong, read any math textbook.


I think we're talking about two different things here; I was taking
the notation "sqrt(x)" to mean you wanted to compute the answer
equivalent to math's

--
\/x

function. Here's a source from a professor to back me up on this,
since I know you won't believe me:

http://math.vanderbilt.edu/~schectex/commerrs/#Radicals

In short, positive numbers DO have two roots, but when you write

--
\/x

in math, that literally means "the nonnegative root". That is why the
graph of y=sqrt(x) does NOT have any negative y-values, and why the
square root function IS A FUNCTION. Do you know what a square root
graph looks like? I hope you are not conjecturing that square root is
not a function...

I apologize, my previous post should have been more clear.

And here is a proof for you; see if you can spot an error in it:

Suppose
-----
\/(x^2) = +/- x (both) for x>0. Then

-----
\/(x^2) = -x. Since

-----
\/(x^2) <=> x^(2/2), we have

x^(2/2) = -x

=> x^1 = -x

=> x = -x

which is a contradiction. Thus, it is not true that

-----
\/(x^2) = +/- x (both) for x>0.
Nov 14 '05 #28
Mark McIntyre <ma**********@spamcop.net> wrote in message news:<kc********************************@4ax.com>. ..
On 5 Mar 2004 10:43:49 -0800, in comp.lang.c , bo********@aol.com (Robert
Futrell) wrote:
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message news:<c2**********@news.freedom2surf.net>...
but sqrt of a positive number can be positive or negative

e.g. sqrt(4) = +/- 2


This is false. The square root of a positive number is always
positive.


eh? A square has two roots, one positive and one negative.
This doesn't mean that there are two answers, one positive and one
negative;


Well, yes it does.
and we all SHOULD know that functions have a UNIQUE y-value for every x.


garbage.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---


Not garbage; I was using very strict math here and you (and indeed,
most everybody else) wasn't. I was taking the notation "sqrt(x)" to
mean you wanted to compute the answer equivalent to math's

--
\/x

function. Here's a source from a professor to back me up on this,
since I know you won't believe me:

http://math.vanderbilt.edu/~schectex/commerrs/#Radicals

In short, positive numbers DO have two roots, but when you write

--
\/x

in math, that literally means "the nonnegative root". That is why the
graph of y=sqrt(x) does NOT have any negative y-values, and why the
square root function IS A FUNCTION. I assume you know what a square
root graph looks like. I hope you are not conjecturing that square
root is not a function...

I apologize, my previous post should have been more clear.

And here is a proof for you; see if you can spot an error in it:

Suppose
-----
\/(x^2) = +/- x (both) for x>0. Then

-----
\/(x^2) = -x. Since

-----
\/(x^2) <=> x^(2/2), we have

x^(2/2) = -x

=> x^1 = -x

=> x = -x

which is a contradiction. Thus, it is not true that

-----
\/(x^2) = +/- x (both) for x>0.
Nov 14 '05 #29
CBFalconer <cb********@yahoo.com> wrote in message news:<40**************@yahoo.com>...
Robert Futrell wrote:
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
but sqrt of a positive number can be positive or negative

e.g. sqrt(4) = +/- 2


This is false. The square root of a positive number is always
positive.


You are just the man I want to see. I have all these marvellous
proofs, about 1 == 0, and things like that. You will be able to
immediately see that they are accurate and incontrovertible.


I am curious to see the fallacious proof of 1==0 you have, as long as
the logical fallacy involves the sqrt discussion at hand.

Another thing: If "sqrt" in mathematics doesn't imply a positive-only
root, then why does the quadratic forumula contain "+/-" before the
square-root term? If sqrt really had two solutions in pure math like
you say, wouldn't the "+/-" in front be redundant?

Doesn't prove anything I know, but something for you to ponder.
Nov 14 '05 #30
Nick Landsberg <hu*****@NOSPAM.att.net> writes:
Dik T. Winter wrote:
I know about the proofs CBFalconer alludes to. When using the sqrt
function they assume that with complex argument and complex result
sqrt(a * b) = sqrt(a) * sqrt(b),
this equation is false.


Huh?

using pure mathematics ...

If we sqaure both sides of the equation we get

sqrt(a*b) ^ 2 = sqrt(a) ^ 2 * sqrt(b) ^ 2

since sqrt(x)^2 = x for all x
(by definition)

we get a * b = a * b

What am I missing?


It's always possible to derive a true statement from a false statement.
For example, take the false statement 1 = -1, square both side, and you
arrive at the true statement 1 = 1.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #31
Martin Dickopp wrote:

Nick Landsberg <hu*****@NOSPAM.att.net> writes:
Dik T. Winter wrote:
I know about the proofs CBFalconer alludes to. When using the sqrt
function they assume that with complex argument and complex result
sqrt(a * b) = sqrt(a) * sqrt(b),
this equation is false.


Huh?

using pure mathematics ...

If we sqaure both sides of the equation we get

sqrt(a*b) ^ 2 = sqrt(a) ^ 2 * sqrt(b) ^ 2

since sqrt(x)^2 = x for all x
(by definition)

we get a * b = a * b

What am I missing?


It's always possible to derive a true statement
from a false statement.
For example, take the false statement 1 = -1, square both side,
and you arrive at the true statement 1 = 1.


Squaring equations, typically adds roots to the solution set:
x == 1 /* 1 */
x * x == 1 /* 1, -1 */

The last time I saw somebody try something like that,
they were trying to win drinks in a bar.
It's sophistry, not algebra.

--
pete
Nov 14 '05 #32
Robert Futrell wrote:

Mark McIntyre <ma**********@spamcop.net> wrote in message news:<kc********************************@4ax.com>. ..
On 5 Mar 2004 10:43:49 -0800, in comp.lang.c , bo********@aol.com (Robert
Futrell) wrote:
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message news:<c2**********@news.freedom2surf.net>...

> but sqrt of a positive number can be positive or negative
>
> e.g. sqrt(4) = +/- 2

This is false. The square root of a positive number is always
positive.
eh? A square has two roots, one positive and one negative.
This doesn't mean that there are two answers, one positive and one
negative;


Well, yes it does.
and we all SHOULD know that functions have a UNIQUE y-value for every x.


garbage.

Not garbage; I was using very strict math here and you (and indeed,
most everybody else) wasn't. I was taking the notation "sqrt(x)" to
mean you wanted to compute the answer equivalent to math's

--
\/x

function. Here's a source from a professor to back me up on this,
since I know you won't believe me:

http://math.vanderbilt.edu/~schectex/commerrs/#Radicals

In short, positive numbers DO have two roots, but when you write

--
\/x

in math, that literally means "the nonnegative root".


When I learned math in school
it was called the "principle square root".

http://www.google.com/search?hl=en&i...square+root%22

--
pete
Nov 14 '05 #33
pete <pf*****@mindspring.com> writes:
Martin Dickopp wrote:

Nick Landsberg <hu*****@NOSPAM.att.net> writes:
> Dik T. Winter wrote:
>
>> I know about the proofs CBFalconer alludes to. When using the sqrt
>> function they assume that with complex argument and complex result
>> sqrt(a * b) = sqrt(a) * sqrt(b),
>> this equation is false.
>
> Huh?
>
> using pure mathematics ...
>
> If we sqaure both sides of the equation we get
>
> sqrt(a*b) ^ 2 = sqrt(a) ^ 2 * sqrt(b) ^ 2
>
> since sqrt(x)^2 = x for all x
> (by definition)
>
> we get a * b = a * b
>
> What am I missing?


It's always possible to derive a true statement
from a false statement.
For example, take the false statement 1 = -1, square both side,
and you arrive at the true statement 1 = 1.


Squaring equations, typically adds roots to the solution set:
x == 1 /* 1 */
x * x == 1 /* 1, -1 */


Unlike 1 == -1 or 1 == 1, x == 1 or x*x == 1 are not statements that
are true or false. So, while you're right, this doesn't invalidate my
explanation.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #34
Nick Landsberg <hu*****@NOSPAM.att.net> wrote in message news:<Wi*********************@bgtnsc04-news.ops.worldnet.att.net>...
Dik T. Winter wrote:

I know about the proofs CBFalconer alludes to. When using the sqrt
function they assume that with complex argument and complex result
sqrt(a * b) = sqrt(a) * sqrt(b),
this equation is false.


Huh?

using pure mathematics ...

If we sqaure both sides of the equation we get

sqrt(a*b) ^ 2 = sqrt(a) ^ 2 * sqrt(b) ^ 2

since sqrt(x)^2 = x for all x
(by definition)

we get a * b = a * b

What am I missing?

As far as I know, the above is true
even for complex numbers, i.e. sqrt(-1).

Please explain.


The product rule sqrt(ab) = sqrt(a) * sqrt(b) holds in the reals but
not in the complex plane.
Nov 14 '05 #35
Mark McIntyre <ma**********@spamcop.net> wrote in message news:<kc********************************@4ax.com>. ..
On 5 Mar 2004 10:43:49 -0800, in comp.lang.c , bo********@aol.com (Robert
Futrell) wrote:
and we all SHOULD know that functions have a UNIQUE y-value for every x.


garbage.

--

http://dictionary.reference.com/search?q=function
http://www.nist.gov/dads/HTML/function.html
http://www.blc.edu/fac/rbuelow/commo...m.htm#Function
http://www.math2.org/math/algebra/functions/index.htm
http://www.gpc.peachnet.edu/~mhall/o...functions.html
http://www.wordiq.com/cgi-bin/knowle...mal_Definition
Still don't believe me?
Nov 14 '05 #36
pete <pf*****@mindspring.com> wrote in message news:<40**********@mindspring.com>...
Robert Futrell wrote:

Mark McIntyre <ma**********@spamcop.net> wrote in message news:<kc********************************@4ax.com>. ..
On 5 Mar 2004 10:43:49 -0800, in comp.lang.c , bo********@aol.com (Robert
Futrell) wrote:

>"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message news:<c2**********@news.freedom2surf.net>...
>
>> but sqrt of a positive number can be positive or negative
>>
>> e.g. sqrt(4) = +/- 2
>
>This is false. The square root of a positive number is always
>positive.

eh? A square has two roots, one positive and one negative.

>This doesn't mean that there are two answers, one positive and one
>negative;

Well, yes it does.

>and we all SHOULD know that functions have a UNIQUE y-value for every x.

garbage.

Not garbage; I was using very strict math here and you (and indeed,
most everybody else) wasn't. I was taking the notation "sqrt(x)" to
mean you wanted to compute the answer equivalent to math's

--
\/x

function. Here's a source from a professor to back me up on this,
since I know you won't believe me:

http://math.vanderbilt.edu/~schectex/commerrs/#Radicals

In short, positive numbers DO have two roots, but when you write

--
\/x

in math, that literally means "the nonnegative root".


When I learned math in school
it was called the "principle square root".

http://www.google.com/search?hl=en&i...square+root%22


Yes; I had forgotten the name for it. Thanks.
Nov 14 '05 #37
In article <Wi*********************@bgtnsc04-news.ops.worldnet.att.net> hu*****@NOSPAM.att.net writes:
Dik T. Winter wrote:
I know about the proofs CBFalconer alludes to. When using the sqrt
function they assume that with complex argument and complex result
sqrt(a * b) = sqrt(a) * sqrt(b),
this equation is false.


Huh?


Want to see an example? Set a = b = -1:
1 = sqrt(1) = sqrt(a * b) != sqrt(a) * sqrt(b) = sqrt(-1) * sqrt(-1) =
= i * i = -1.
In mathematics the sqrt function is *not* multiplicative. It is so only
when you restrict yourself to positive real arguments. Similar:
ln(a * b) = ln(a) + ln(b)
only if you restrict yourself to postive real arguments.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 14 '05 #38


Dik T. Winter wrote:
In article <Wi*********************@bgtnsc04-news.ops.worldnet.att.net>hu*****@NOSPAM.att.net writes:
> Dik T. Winter wrote:
> > I know about the proofs CBFalconer alludes to. When using the sqrt
> > function they assume that with complex argument and complex result
> > sqrt(a * b) = sqrt(a) * sqrt(b),
> > this equation is false.

>
> Huh?


Want to see an example? Set a = b = -1:
1 = sqrt(1) = sqrt(a * b) != sqrt(a) * sqrt(b) = sqrt(-1) * sqrt(-1) =
= i * i = -1.
In mathematics the sqrt function is *not* multiplicative. It is so only
when you restrict yourself to positive real arguments. Similar:
ln(a * b) = ln(a) + ln(b)
only if you restrict yourself to postive real arguments.


Thank you!

I guess I'll have to get out the 40 year old math books
and bone up on the idiosyncracies.

Nick L.

- At this age, memory is the second thing to go,
and I forgot what the first is.

--
Ñ
"It is impossible to make anything foolproof because fools are so
ingenious" - A. Bloch

Nov 14 '05 #39
Actually Robert Futrell is technically correct in his original explanation, but
his statements do not clarify the real issue.

ma**********@spamcop.net says...
On 5 Mar 2004 10:43:49 -0800, in comp.lang.c , bo********@aol.com (Robert
Futrell) wrote:
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message news:<c2**********@news.freedom2surf.net>...
but sqrt of a positive number can be positive or negative

e.g. sqrt(4) = +/- 2


This is false. The square root of a positive number is always
positive.


eh? A square has two roots, one positive and one negative.


There can be up to two roots of a square depending on your number system basis.
But the number 0 only has one root which is neither positive or negative. This
is different from the classically described "square root function".
This doesn't mean that there are two answers, one positive and one
negative;


Well, yes it does.
and we all SHOULD know that functions have a UNIQUE y-value for every x.


garbage.


Actually that technically *is* definition of a function. A function can only
have a single range value for any given domain value. And that's the point.
As soon as you call it "the square root *FUNCTION*" then you are talking about
the classic square root function which takes non-negative real numbers and maps
them to other non-negative real numbers.

Robert Futrell original post made true statements, but lacked sufficient
explanation:

- Saying that the square root function takes postiive numbers to other positive
numbers is true, however he *IS* leaving out 0 for some reason.
- The square root function is also always incident with at least one of the
square roots of the numbers its defined in its domain. (He left this out.)
- The square root function is continuous.

The +/- notation is used when, for whatever reason, you need to describe both
roots when a number has two square roots. (Though it works redundantly for 0.)

Why this is all in comp.lang.c is beyond me, however I would like to point out
the following:

if (a < 0) a = -a;

does *NOT* necessarily force the integer variable a to be non-negative (see if
you can figure out how/why). So following it up with:

b = sqrt(a);

will lead to undefined behavior. See:

http://www.azillionmonkeys.com/qed/2scomp.html

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
Nov 14 '05 #40
Paul Hsieh wrote:
.... snip ...
Why this is all in comp.lang.c is beyond me, however I would like
to point out the following:

if (a < 0) a = -a;

does *NOT* necessarily force the integer variable a to be
non-negative (see if you can figure out how/why). ... snip ...


however

if (a > 0) a = -a;

will force it to the range INT_MIN .. 0. Thus this is the proper
operation when translating to text and extracting signs. The
result can now be fearlessly converted into an unsigned.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #41
In article <40***************@yahoo.com>, CBFalconer wrote:
however

if (a > 0) a = -a;

will force it to the range INT_MIN .. 0. Thus this is the proper
operation when translating to text and extracting signs. The
result can now be fearlessly converted into an unsigned.


And what then? That unsigned number cannot be twiddled
to represent the absolute value, according to pete
(pf*****@mindspring.com), who comes up with the example
UINT_MAX == 65535
INT_MIN == -65536
CHAR_BIT == 16
INT_MAX == 65535
sizeof(unsigned) == 2
sizeof(int) == 2
and the generaliziation "unsigned isn't guaranteed to be
able to represent the magnitude of INT_MIN."

I still haven't figured out if this is a purposeful part of
the standard, in order to permit some obscure but extant
implementation, or an unintentional gap -- that should be
closed before the next rev of the DS9000 comes out. ;-)

- Larry
Nov 14 '05 #42
Larry Doolittle wrote:

In article <40***************@yahoo.com>, CBFalconer wrote:
however

if (a > 0) a = -a;

will force it to the range INT_MIN .. 0. Thus this is the proper
operation when translating to text and extracting signs. The
result can now be fearlessly converted into an unsigned.


And what then? That unsigned number cannot be twiddled
to represent the absolute value, according to pete
(pf*****@mindspring.com), who comes up with the example
UINT_MAX == 65535
INT_MIN == -65536
CHAR_BIT == 16
INT_MAX == 65535
sizeof(unsigned) == 2
sizeof(int) == 2
and the generaliziation "unsigned isn't guaranteed to be
able to represent the magnitude of INT_MIN."


No integer type is guaranteed to be able to represent
the magnitude of INT_MIN.
On my very common system:
INT_MIN is -2147483648
LONG_MAX is 2147483647

--
pete
Nov 14 '05 #43
Larry Doolittle wrote:
In article <40***************@yahoo.com>, CBFalconer wrote:
however

if (a > 0) a = -a;

will force it to the range INT_MIN .. 0. Thus this is the proper
operation when translating to text and extracting signs. The
result can now be fearlessly converted into an unsigned.


And what then? That unsigned number cannot be twiddled
to represent the absolute value, according to pete
(pf*****@mindspring.com), who comes up with the example
UINT_MAX == 65535
INT_MIN == -65536
CHAR_BIT == 16
INT_MAX == 65535
sizeof(unsigned) == 2
sizeof(int) == 2
and the generaliziation "unsigned isn't guaranteed to be
able to represent the magnitude of INT_MIN."


Yes it is. Of the legal integer representations, ones, twos
complement, and sign magnitude, there is always a sign bit
(specified). That bit is always available for reuse in an
unsigned, thus automatically doubling the range. The situation
above is not possible.

I repeat - fearlessly :-)

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #44
On Sat, 6 Mar 2004 02:26:11 GMT, in comp.lang.c , "Dik T. Winter"
<Di********@cwi.nl> wrote:
In article <kc********************************@4ax.com> Mark McIntyre <ma**********@spamcop.net> writes:
On 5 Mar 2004 10:43:49 -0800, in comp.lang.c , bo********@aol.com (Robert
Futrell) wrote:
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message news:<c2**********@news.freedom2surf.net>...

> but sqrt of a positive number can be positive or negative
>
> e.g. sqrt(4) = +/- 2

This is false. The square root of a positive number is always
positive.


eh? A square has two roots, one positive and one negative.


Let us put this in perspective.


sure, but lets also be relevant. The discussion was about whether the
square root of a positive number is always positive, not about whether some
mathematical function could have two, one or zero results. If you define
the square root in the usual way, then both -2 and +2 are square roots of
4.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #45
On Sun, 07 Mar 2004 04:39:51 GMT, in comp.lang.c , qe*@pobox.com (Paul
Hsieh) wrote:
ma**********@spamcop.net says...
On 5 Mar 2004 10:43:49 -0800, in comp.lang.c , bo********@aol.com (Robert
Futrell) wrote:

>and we all SHOULD know that functions have a UNIQUE y-value for every x.


garbage.


Actually that technically *is* definition of a function.


Its concievably the definition of a function in pure maths but this is a)
irrelevant to C and b) irrelevant to the real world, since given this
definition sqrt() is not a function.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #46

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.