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

Sine function

Can someone give me a rough idea how a value is obtained for a trigonometric
function like Math.Sin? What is the algorithm? I'm finding that putting a
large number in this function ( > 8000 ) really slows things down. Why is
that?

Thanks,

Owkmann
Feb 28 '06 #1
10 3067

Sin(360) = Sin(0).

do this.

Sin( largeNumber % 360 );

Feb 28 '06 #2
First of all, Math.Sin takes its argument in radians, not degrees, so
360 is the wrong denominator.

Second, the argument is a double, for which there is no modulo
operator, even if you could get a reasonably accurate value for 2p.
Not even Math.DivRem provides an overload for doubles.

Feb 28 '06 #3
Trust me to reply without thinking the logic through.

The angle is measured in radians, so it shouldn't be 360, but 2 pi.

Disclaimer:
Untested code.

jliu
johnliu.net

Mar 1 '06 #4
And Bruce's right. % or DivRem won't work.

I found double Math.IEEERemainder(double x, doubly y) though.

So the idea should be:

Math.Sin( Math.IEEERemainder( largeNumber, 2* Math.PI ));

Disclaimer:
Untested code.

jliu
johnliu.net

Mar 1 '06 #5
Jeez, instead of criticizing you could have been a little more helpful.

Math.Sin(Math.IEEERemainder(LargeNum, 6.28318))

Should do the trick. (Hint: provide a more accurate number of the number of
radians in a full circle to get a better answer).

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
First of all, Math.Sin takes its argument in radians, not degrees, so
360 is the wrong denominator.

Second, the argument is a double, for which there is no modulo
operator, even if you could get a reasonably accurate value for 2p.
Not even Math.DivRem provides an overload for doubles.

Mar 1 '06 #6
> Math.Sin(Math.IEEERemainder(LargeNum, 6.28318))

Actually to get a better number for this, you could use

Math.Sin(Math.IEEERemainder(LargeNum, (double)360 / (double)2 / Math.PI))

But that assumes the compiler is smart enough to not evaluate the equation
every time (or better yet, evaluate it at compile time)... It might be a
safe assumption to make these days, but just to make sure you might want to
turn that into a constant.
Mar 1 '06 #7
Tried this in snippet compiler, because I was worried about the error.
DateTime start = DateTime.Now;
WL("Math.Sin(10000) = {0}", Math.Sin(10000));
TimeSpan ts = DateTime.Now - start;
WL("Time Taken: {0}", ts);

DateTime start2 = DateTime.Now;
WL("Math.Sin(Math.IEEERemainder(10000, 2*Math.PI)) = {0}",
Math.Sin(Math.IEEERemainder(10000, 2*Math.PI)));
TimeSpan ts2 = DateTime.Now - start2;
WL("Time Taken: {0}", ts2);
RL();

Out:
Math.Sin(10000) = -0.305614388888252
Time Taken: 00:00:00
Math.Sin(Math.IEEERemainder(10000, 2*Math.PI)) = -0.305614388888028
Time Taken: 00:00:00

Math.Sin(1000000) = -0.349993502171292
Time Taken: 00:00:00
Math.Sin(Math.IEEERemainder(1000000, 2*Math.PI)) = -0.349993502093929
Time Taken: 00:00:00

There seems to be a increasing error as the number gets bigger. The
error may be too high to be acceptable in your situation.

I'm not seeing this slowness problem though. May be I need to run it
1000 times.

jliu
johnliu.net

Mar 1 '06 #8
Yup, a classical case of magnified errors due to scale vs precision... It
would be much smarter to get the number down to 0 >= x >= 360 before calling
the sine function...

"John Liu" <jo******@gmail.com> wrote in message
news:11**********************@t39g2000cwt.googlegr oups.com...
Tried this in snippet compiler, because I was worried about the error.
DateTime start = DateTime.Now;
WL("Math.Sin(10000) = {0}", Math.Sin(10000));
TimeSpan ts = DateTime.Now - start;
WL("Time Taken: {0}", ts);

DateTime start2 = DateTime.Now;
WL("Math.Sin(Math.IEEERemainder(10000, 2*Math.PI)) = {0}",
Math.Sin(Math.IEEERemainder(10000, 2*Math.PI)));
TimeSpan ts2 = DateTime.Now - start2;
WL("Time Taken: {0}", ts2);
RL();

Out:
Math.Sin(10000) = -0.305614388888252
Time Taken: 00:00:00
Math.Sin(Math.IEEERemainder(10000, 2*Math.PI)) = -0.305614388888028
Time Taken: 00:00:00

Math.Sin(1000000) = -0.349993502171292
Time Taken: 00:00:00
Math.Sin(Math.IEEERemainder(1000000, 2*Math.PI)) = -0.349993502093929
Time Taken: 00:00:00

There seems to be a increasing error as the number gets bigger. The
error may be too high to be acceptable in your situation.

I'm not seeing this slowness problem though. May be I need to run it
1000 times.

jliu
johnliu.net

Mar 1 '06 #9
> I'm not seeing this slowness problem though. May be I need to run it
1000 times.

jliu
johnliu.net


Yah. 16384 times (2^14).

public static double[] SineWave(double frequency, double phase, double
magnitude, int N_samples, double SampleRate)
{
const double PI = Math.PI;
double[] Xreal = new double[N_samples]; // N_samples = 16384; Gets really
slow
double Freq = frequency;
double Phase = phase;
double Mag = magnitude;
double Sample_Rate = SampleRate;
double Operand = 2 * PI * Freq * 1 / Sample_Rate + Phase;

for (int i = 0; i < N_samples; i++)
{
// This equation returns an array of doubles that simulate
// an A/D sampling a sine wave at a given sample rate

Xreal[i] = Mag * Math.Sin(Operand * i);
}
return Xreal;
}

That's why I was wondering how Math.Sin calculates its number.

There's got to be an easier way!

Owkmann
Mar 1 '06 #10
> Jeez, instead of criticizing you could have been a little more helpful.

Math.Sin(Math.IEEERemainder(LargeNum, 6.28318))

Should do the trick.


I wasn't more helpful because I didn't know about IEEERemainder(). :)

Mar 1 '06 #11

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

Similar topics

143
by: suri | last post by:
Hello I downloaded glibc and tried looking for the code that implements the sine function i couldnt find the file. i went to the math directory and found math.h.. i guess that needs to be...
9
by: Water Cooler v2 | last post by:
What is the inverse of the sine function? I've been pulling my hair and I have no clue nor memory of how to go about solving this little expression: sine-1 (read sine inverse) of (1-(x/y)) An...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...

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.