tanjent function - can someone check my code

Z

Zach

#include <stdio.h>
#include <math.h>

int i=0, j=0, *z=0;
double tanjent=0.0;

void main(int *)
{

for(;;)
{
tanjent[&z] = sin(i) / cos (j);
*z=&z+1

}

printf(tanjent[k],"%d);
return &z;

}

Regards,
Zach
 
Z

Zach

no! i am not at troll - how rude.
i posted my code and asked for feedback.
if you cannot reply constructively please dont reply accusing people of
trolling when they arent!

zach
 
D

Default User

Zach said:
no! i am not at troll - how rude.
i posted my code and asked for feedback.
if you cannot reply constructively please dont reply accusing people of
trolling when they arent!


You aren't helping yourself much with this whiny tirade. There are so
many weird things in your code that it's pretty reasonable to suspect
it's some sort of troll.

You don't explain what problems you have. It's obvious that you didn't
compile it. Here are a few questions:

1. What do you think this does?
*z=&z+1

2. Where did k come from?

3. What do you think the result of dividing 0 by 0 would be?

4. What does this declare?
*z=0;

5. What is this?
&z


Brian
 
D

Default User

Zach said:
no! i am not at troll - how rude.
i posted my code and asked for feedback.
if you cannot reply constructively please dont reply accusing people of
trolling when they arent!

You aren't helping yourself much with this whiny tirade. There are so
many weird things in your code that it's pretty reasonable to suspect
it's some sort of troll.

You don't explain what problems you have. It's obvious that you didn't
compile it. Here are a few questions:

1. What do you think this does?
*z=&z+1

2. Where did k come from?

3. How would i and j be anything but 0?

4. What does this declare?
*z=0;

5. What is this?
&z

6. What does this do?
for(;;)


Brian
 
C

CBFalconer

Zach said:
#include <stdio.h>
#include <math.h>

int i=0, j=0, *z=0;
double tanjent=0.0;

1> void main(int *)
{
for(;;)
{
2> tanjent[&z] = sin(i) / cos (j);
3> *z=&z+1
}
4> printf(tanjent[k],"%d);
5> return &z;
}

1> main returns int, and has parameters of type int and char**
2> attempt to index NULL, doomed.
2> sin(0) / cos(0) == 0 / 1 == 0
3> attempt to store a pointer in an int. No final semi.
4> Bad arguments to printf, and unterminated string. k undeclared.
5> returning a pointer in place of an int.

The "for(;;)" line is correct, as are the isolated braces. So I
would say it falls a tad short of perfection. Your compiler should
have showed these up.

[1] c:\c\junk>cc junk.c
junk.c:8: warning: return type of `main' is not `int'
junk.c:8: warning: first argument of `main' should be `int'
junk.c:8: warning: `main' takes only zero or two arguments
junk.c: In function `main':
junk.c:7: parameter name omitted
junk.c:11: subscripted value is neither array nor pointer
junk.c:13: warning: assignment makes integer from pointer without a
cast
junk.c:13: parse error before '}' token
junk.c:14:21: warning: multi-line string literals are deprecated
junk.c:14:21: missing terminating " character
junk.c:14:21: possible start of unterminated string literal
 
T

Thomas Stegen

Zach said:
no! i am not at troll - how rude.
i posted my code and asked for feedback.
if you cannot reply constructively please dont reply accusing people of
trolling when they arent!

This is as constructive as I can be:

Start learning C from scratch. Again. You might find it
easier the second time around. Your code is so wrong that
I find it hard to believe that it is not deliberate.
 
T

Thomas Matthews

CBFalconer said:
Zach said:
#include <stdio.h>
#include <math.h>

int i=0, j=0, *z=0;
double tanjent=0.0;

1> void main(int *)
{
for(;;)
{
2> tanjent[&z] = sin(i) / cos (j);
3> *z=&z+1
}
4> printf(tanjent[k],"%d);
5> return &z;
}


1> main returns int, and has parameters of type int and char**
2> attempt to index NULL, doomed.
2> sin(0) / cos(0) == 0 / 1 == 0
3> attempt to store a pointer in an int. No final semi.
4> Bad arguments to printf, and unterminated string. k undeclared.
5> returning a pointer in place of an int.

The "for(;;)" line is correct, as are the isolated braces. So I
would say it falls a tad short of perfection. Your compiler should
have showed these up.

Here are some additional items:
1. The "for" loop is infinite and the printf and return will
never be executed.

2. The code inside the "for" loop will, after writing to address 0,
will continue to write to entire address space of the platform,
providing that the operating system and processor allow it.

3. The "tanjent" variable is one double precision value.
The "for" loop treats it as an array.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
Z

Zach

Hi,

I wish to make an iterative program that uses recursion to continually
calculate the value of tanjent function and store the values in an
array. I wished to use z as return argument and have it passed back to
main and main gets called again and again yet with value being
increased each time.

Thanks for feedback!
zach
 
K

Keith Thompson

Zach said:
I wish to make an iterative program that uses recursion to continually
calculate the value of tanjent function and store the values in an
array.

Why do you want to use recursion?
I wished to use z as return argument and have it passed back to
main and main gets called again and again yet with value being
increased each time.

Calling main recursively is rarely useful.

(BTW, it's spelled "tangent", not "tanjent".)
 
Z

Zach

hi keith,

ok i suppose i don't HAVE to use recursion - i thought it would be fun.

ok yes tangent. i will think some more about this code.

thanks!
zach
 
T

Thomas Matthews

Zach said:
hi keith,

ok i suppose i don't HAVE to use recursion - i thought it would be fun.

ok yes tangent. i will think some more about this code.

thanks!
zach

How big is your table?

What is the resolution of your table (i.e. distance between successive
degrees or radians)?

Do the values change after the table is created?

How does your tangent function differ from a library version?

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
Z

Zach

thanks for additional comments thomas.
i will think about these. i wish to calculate tangent
for 0 to 360 degrees.

zach
 
M

Martin Ambuhl

Zach said:
thanks for additional comments thomas.
i will think about these. i wish to calculate tangent
for 0 to 360 degrees.


#include <math.h>

inline long double radians(int i)
{
return i * 0.0174532925199432958L;
}

#define TBLMAX 360

int main(void)
{
double tbl[TBLMAX + 1][2];
double theta;
int i;

/* fill tables */
for (i = 0; i <= TBLMAX; i++) {
theta = radians(i);
tbl[0] = tan(theta);
tbl[1] = sin(theta) / cos(theta);
/* To be portable, we should check that theta is not PI * (k +
1/2) where k is an integer */
}
return 0;

}
 
Z

Zach

wow, thanks martin. i never heard of "inline" before - this seems to be
a macro.
i never used 2D array either! wow good stuff. i will pour over it!

zach
 
W

Walter Roberson

: tbl[0] = tan(theta);
: tbl[1] = sin(theta) / cos(theta);
: /* To be portable, we should check that theta is not PI * (k +
: 1/2) where k is an integer */

You would also want to check that cos(theta) is not 0 before you
do the division -- in the general case, you could be taking the
cos of something close enough to one of the magic points that the
cos rounded to zero.

What value -should- be stored when cos(theta) rounds to 0 or
when theta is close to one of the magic points is left as
an exercise to Zach... as is the problem of determining in advance
whether theta is "close enough" to one of the magic points *before*
taking it's tan().


For portability, there is another consideration, which is that
rounding behaviours are implimentation dependant, especially rounding
of negative values. A program which had a particular requirement
for high accuracy would need to take those behaviours into account.
 
I

infobahn

Walter said:
: tbl[0] = tan(theta);
: tbl[1] = sin(theta) / cos(theta);
: /* To be portable, we should check that theta is not PI * (k +
: 1/2) where k is an integer */

You would also want to check that cos(theta) is not 0 before you
do the division


And don't forget to meet the condition that fabs(sin(theta)) < 1 while
you're at it.

FCOL
 
M

Michael Mair

Walter said:
: tbl[0] = tan(theta);
: tbl[1] = sin(theta) / cos(theta);
: /* To be portable, we should check that theta is not PI * (k +
: 1/2) where k is an integer */

You would also want to check that cos(theta) is not 0 before you
do the division -- in the general case, you could be taking the
cos of something close enough to one of the magic points that the
cos rounded to zero.


How does cos(theta)=0 within the range where we really can resolve
"PI/180" differ from "theta = PI*(k+1/2)" apart from the former being
more expensive but a little bit safer to check?
What value -should- be stored when cos(theta) rounds to 0 or
when theta is close to one of the magic points is left as
an exercise to Zach... as is the problem of determining in advance
whether theta is "close enough" to one of the magic points *before*
taking it's tan().

For portability, there is another consideration, which is that
rounding behaviours are implimentation dependant, especially rounding
of negative values. A program which had a particular requirement
for high accuracy would need to take those behaviours into account.

Well, wherever that is a problem, you probably will have to replace the
library routines, too. Then, it probably is best(*) to work with
integers which represent degrees or parts thereof and work modulo 180
degree (where 90 degree is one of your "magic points, of course) and
just have a table in full precision.


Cheers
Michael

(*) best in the sense that you can whip up a correct solution in short
time. The rounding error can still be present in the table s.th. you
have to make it symmetric w.r.t. 0/180 degree.
Controlling the error even of a known rounding behaviour can be much
work and you easily make a mistake which only is found on the next
machine or at the most inconvenient time... :-/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top