GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Dec-2004, 11:41
fwongmc fwongmc is offline
New Member
 
Join Date: Nov 2004
Posts: 26
fwongmc is on a distinguished road

Problem on sin(C programming)


Dear all,

I am writing a part of function using sin in math,h
I need to use any array to store sinx where x=0 and +0.1 everytime,but the reuslt is failure.I can't get the result,thus I just put in a small program:
CPP / C++ / C Code:
#include <stdio.h>
#include <math.h>

void main(){

double input=0.1;
double result;

result=sin(input);

}

The result 1.7*10^(-03),but the answer gives out 0.9833 but not the result,can anyone tell me where I made the mistake?
  #2  
Old 09-Dec-2004, 12:03
BobbyMurcerFan BobbyMurcerFan is offline
Member
 
Join Date: May 2004
Posts: 103
BobbyMurcerFan is on a distinguished road
I believe it's b/c C/C++ assume the input for trigonometric funcitons is in radians, not degrees. So you have to convert input from degrees to radians before passing it to sin().

HTH.
  #3  
Old 09-Dec-2004, 12:57
fwongmc fwongmc is offline
New Member
 
Join Date: Nov 2004
Posts: 26
fwongmc is on a distinguished road
how?I guess what I declear is right

[c]double sin (double)

isn't it?Can anyone give me a hand on it?
  #4  
Old 09-Dec-2004, 15:15
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by fwongmc
how?I guess what I declear is right
isn't it?Can anyone give me a hand on it?

Try this: Note the answer:

sin of .1 degrees = 1.745328e-03

sin of .1 radians = 9.983342e-02



CPP / C++ / C Code:
#include <stdio.h>
#include <math.h>

int main()
{
  double xrad; /* angle in radians */
  double xdeg; /* angle in degrees */
  double result;

  double pi;

  pi = 4.0 * atan(1.0); /* get pi without having to type in 16 digits */

  xrad = 0.1;
  xdeg = xrad * 180.0 / pi;
  result = sin(xrad);
  printf("For x = %f radians (%f degrees), sin(x) = %e\n", xrad, xdeg, result);

  xdeg = 0.1;
  xrad = xdeg * pi / 180.0;
  result = sin(xrad);
  printf("For x = %f degrees (%f radians), sin(x) = %e\n", xdeg, xrad, result);
  return 0;
}



Regards,

Dave
  #5  
Old 09-Dec-2004, 22:53
fwongmc fwongmc is offline
New Member
 
Join Date: Nov 2004
Posts: 26
fwongmc is on a distinguished road
Thanks Dave!

Here comes a question:
I need to calculate sin(x) where x initial is 0.0,and add 0.1 itself after the executoin.The reuslt were store on the array func[]

Here comes my code:
CPP / C++ / C Code:
#include <stdio.h>
#include <math.h>

int main ()	{

double func[99];
int choice;
double in_value=0.1;
int num;
double trans=0.0;
double pi;

pi = 4.0 * atan(1.0); /* get pi without having to type in 16 digits */

printf ("Enter function to be plotted:\n");
printf ("For f(x) = sin(x), enter choice 1\nFor f(x) = exp(-0.5x)sin(3x), enter choice 2\n");
scanf ("%d",&choice); 
	if (choice==1)	{	
	 		for (num=0;num<=100;num++) {
				func[num]=sin((trans*pi)/180);
					trans+=in_value;
						printf ("func[%d]=%f\n",num,func[num]);	}
					
			printf ("abcdefg");}
else  
	printf ("You choose 2!\n");
	
}

The result is correct(I have to thanks Dave!)But after Ixecute with Vc++,after execution,a warning message told me that my exe program have problems and ask me to send a report to M$,does my code getting error?(It's a bit urgent!
  #6  
Old 09-Dec-2004, 23:01
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by fwongmc
Thanks Dave!

Here comes a question:
I need to calculate sin(x) where x initial is 0.0,and add 0.1 itself after the executoin.The reuslt were store on the array func[]

Here comes my code:
CPP / C++ / C Code:

double func[99];

	 		for (num=0;num<=100;num++) {
				func[num]=sin((trans*pi)/180);
					trans+=in_value;
						printf ("func[%d]=%f\n",num,func[num]);	}


The result is correct(I have to thanks Dave!)But after Ixecute with Vc++,after execution,a warning message told me that my exe program have problems and ask me to send a report to M$,does my code getting error?(It's a bit urgent!


You declare func to be an array with 99 elements. They are func[0], func[1], ... func[98].

Then you try to store something in func[99] and func[100]. That is, the program is running past the end of the array.

Just declare

CPP / C++ / C Code:
double func[101]

Then you can go from func[0] up to and including func[100].


Regards,

Dave
  #7  
Old 10-Dec-2004, 09:12
fwongmc fwongmc is offline
New Member
 
Join Date: Nov 2004
Posts: 26
fwongmc is on a distinguished road
Thanks everyone here.I discover that this is an array problem and finally I fix it.
Can anyone tell me how can I correct floating number in double into nearest integer?

I have an array which pass the calculated result to a new array:

CPP / C++ / C Code:
for(i=0;i<=81;i++){
temp[i]=(calculations+0.5)}

what the array temp stores is numbers in double (eg 71.2323232..../69.56566),how can I round it up into(71/69)?I tried to convert the result into the int
CPP / C++ / C Code:
temp[i]=(int)(calculations+0.5);
,but failure.?(it return result on 0.00000000)May I ask only float can change its type by int?,help...!!
  #8  
Old 10-Dec-2004, 10:23
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by fwongmc
Thanks everyone here.I discover that this is an array problem and finally I fix it.
Can anyone tell me how can I correct floating number in double into nearest integer?

I have an array which pass the calculated result to a new array:

CPP / C++ / C Code:
for(i=0;i<=81;i++){
temp[i]=(calculations+0.5)}

what the array temp stores is numbers in double (eg 71.2323232..../69.56566),how can I round it up into(71/69)?I tried to convert the result into the int
CPP / C++ / C Code:
temp[i]=(int)(calculations+0.5);
,but failure.?(it return result on 0.00000000)May I ask only float can change its type by int?,help...!!

First note that if you convert a floating point number (float or double) to an int, the fractional part is truncated.

If you want to round to the nearest integer, here is a way

If the number is greater than zero, add 0.5 and convert to an integer
If the number is less than zero, subtract -.5 and convert to an integer.


If you started with a double and you want the result to be a double, just convert the result back to a double.

CPP / C++ / C Code:
  double x;
  int tempint;

  x = 12.56;
  tempint = x + 0.5; /* convert 13.06 to an int */
  x = tempint;       /* convert back to double  */

  x = -12.56        
  tempint = x - 0.5; /* convert -13.06 to an int */
  x = tempint;       /* convert back to a double */


Now, if you want to round to the nearest tenth instead of the nearest integer:

Multiply the original by 10.0
Then round off this number to the nearest int (following the steps above)
Then divide the integer value by 10.0 to get a double that has been rounded to hthe nearest tenth.

CPP / C++ / C Code:
  x = 12.56;
  tempint = x * 10.0 + 0.5; /* convert 126.1 to an int */
  x = tempint / 10.0;        /* convert back to double  */
 
  x = -12.56;
  tempint = x * 10.0 - 0.5; /* convert -126.1 to an int */
  x = tempint / 10.0;        /* convert back to a double */
 

You don't actually have to store values in a temporary int; you could use casting to perform all operations in a single statement. You could, of course make functions that have these calculations, then you could say something like
CPP / C++ / C Code:
  x = roundint(x);

Where the operations for rounding to the nearest integer are in your function roundint(), and roundint() could look something like

CPP / C++ / C Code:
double roundint(double arg)
{
  if (arg > 0) {
    return (double)((int)(arg + 0.5));
  }
  else{
    return (double)((int)(arg - 0.5));
  }
}


Regards,

Dave
  #9  
Old 10-Dec-2004, 11:08
fwongmc fwongmc is offline
New Member
 
Join Date: Nov 2004
Posts: 26
fwongmc is on a distinguished road
thanks for Dave and all of you again and again
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache / PHP problem, maybe output length? HaganeNoKokoro Apache Web Server Forum 3 07-Aug-2008 05:42
[Tutorial] GUI programming with FLTK dsmith FLTK Forum 10 03-Oct-2005 16:41
GUI programming crystalattice C++ Forum 5 14-Sep-2004 13:17
C I/O problem kelly80 C Programming Language 4 27-Apr-2004 21:15
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 08:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:32.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.