![]() |
|
#1
|
|||
|
|||
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:
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
|
|||
|
|||
|
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
|
|||
|
|||
|
how?I guess what I declear is right
[c]double sin (double) isn't it?Can anyone give me a hand on it? |
|
#4
|
|||
|
|||
|
Quote:
Try this: Note the answer: sin of .1 degrees = 1.745328e-03 sin of .1 radians = 9.983342e-02 CPP / C++ / C Code:
Regards, Dave |
|
#5
|
|||
|
|||
|
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:
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
|
|||
|
|||
|
Quote:
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:
Then you can go from func[0] up to and including func[100]. Regards, Dave |
|
#7
|
|||
|
|||
|
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:
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:
|
|
#8
|
|||
|
|||
|
Quote:
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:
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:
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:
Where the operations for rounding to the nearest integer are in your function roundint(), and roundint() could look something like CPP / C++ / C Code:
Regards, Dave |
|
#9
|
|||
|
|||
|
thanks for Dave and all of you again and again
|
Recent GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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