473,414 Members | 1,627 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,414 software developers and data experts.

tanjent function - can someone check my code

#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

Nov 14 '05 #1
17 1362
Zach wrote:
[snip]

I think I see a troll.

--
Thomas.
Nov 14 '05 #2
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

Nov 14 '05 #3

Zach wrote:
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

Nov 14 '05 #4

Zach wrote:
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

Nov 14 '05 #5
Zach wrote:

#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
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #6
Zach wrote:
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.

--
Thomas, took the bait once. And only once.
Nov 14 '05 #7
CBFalconer wrote:
Zach wrote:
#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

Nov 14 '05 #8
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

Nov 14 '05 #9
"Zach" <ne****@gmail.com> writes:
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".)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #10
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

Nov 14 '05 #11
Zach wrote:
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

Nov 14 '05 #12
thanks for additional comments thomas.
i will think about these. i wish to calculate tangent
for 0 to 360 degrees.

zach

Nov 14 '05 #13
Zach wrote:
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[i][0] = tan(theta);
tbl[i][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;

}

Nov 14 '05 #14
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

Nov 14 '05 #15
In article <Mw*****************@newsread2.news.atl.earthlink. net>,
Martin Ambuhl <ma*****@earthlink.net> wrote:
: tbl[i][0] = tan(theta);
: tbl[i][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.
--
Suppose there was a test you could take that would report whether
you had Free Will or were Pre-Destined. Would you take the test?
Nov 14 '05 #16
Walter Roberson wrote:

In article <Mw*****************@newsread2.news.atl.earthlink. net>,
Martin Ambuhl <ma*****@earthlink.net> wrote:
: tbl[i][0] = tan(theta);
: tbl[i][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
Nov 14 '05 #17
Walter Roberson wrote:
In article <Mw*****************@newsread2.news.atl.earthlink. net>,
Martin Ambuhl <ma*****@earthlink.net> wrote:
: tbl[i][0] = tan(theta);
: tbl[i][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... :-/
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #18

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

Similar topics

14
by: deancoo | last post by:
Hello all, I have a class member, which calls another member from the same class. The problem I'm experiencing is that the nested function executes fine, but causes a segmentation fault as it...
8
by: Falc2199 | last post by:
Hi, Does anyone know how to make this work? var sectionId = 5; repeat_section_sectionId(); function repeat_section_5(){ alert("firing"); }
17
by: Michael | last post by:
Hi, Could you pleaes let me know when I need to use virtual destctor function in the base class? Thanks in advance, Michael
4
by: hanseymoon | last post by:
Dear newsgroup: I've got this long function, which works good overall to spell check words from a dictionary and I am not in a position to replace it. Can someone please see where or how it...
28
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
13
by: Peter Michaux | last post by:
encodeURIComponent is commonly used to serialize forms for use with XMLHttpRequest requests. For a perfect simulation of browser form requests, the goal is to serialize form data in the <URL:...
44
by: climber.cui | last post by:
Hi all, Does anyone have experience on the thread-safty issue with malloc()? Some people said this function provided in stdlib.h is not thread- safe, but someone said it is thread safe. Is it...
55
by: lovecreatesbea... | last post by:
Do you check all error conditions for all library calls in you code? Is it necessary to check all errors? Is it convenient to check all errors (or how to make code clean and readable with mass of...
3
by: gmdune | last post by:
Hi All, I have written a program that doesn't seem to work and I can't figure out why it's not working. It compiles correctly, but when I run it I get prompted by the first couple of printf...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.