OT: is the math sine function in C# periodic?

Hi,

I was wondering if code like x = Convert.ToInt32(Math.Sin(a) for 'a' being an integer from 0 to n is periodic ie in C# code? I was curious and tested it but the output sequence was not periodic (at least until I ran out of memory at about 10 million sequence length.

This is the line of code I used in C# to generate a sinewave from 0 to

9 with 10 million samples etc:

int[] Sequence1 = Enumerable.Range(10000000).Select(a => Convert.ToInt32(Math.Sin(a) * 4.5 + 4.5)).ToArray();

If the sequence is not periodic then is it because of the implementation of the C# math functions? I think eventually it should become periodic though..

cheers, Jamie

Reply to
Jamie M
Loading thread data ...

I presume Math.Sin(a) uses _double a_ in units of radians?

Then the answer is definitely no, because the argument is periodic every

2*pi, which is transcendental and therefore cannot be expressed by any finite integer number. (Strictly speaking, it's never periodic anyway, because the _double_ expresses a fixed precision rational number. Since the returned value is similarly limited precision, this doesn't matter in practice.)

Practically speaking, some ratios will come close, such as 22/7 or

355/113, but none will be truly periodic.

Computational limits probably make things worse than that, because the underlying datatype has finite accuracy. A sufficiently large exponent (i.e., more than 10^15 or so) will skip over entire cycles from single LSB changes -- smooth calculations (such as is possible from computational methods) are impossible.

Functions that need large angle arguments into trigonometric functions are usually poorly formed, or abuse the purpose or nature of the function, mathematically or computationally. Such edge cases may get very implementation- or platform-dependent results.

If you're using it as a "scrambling" function on the int32 argument, it'll probably work, but there should be better methods (more random, more computationally efficient?) of converting that into a 0-9 range.

Tim

--
Seven Transistor Labs 
Electrical Engineering Consultation 
Website: http://seventransistorlabs.com 

"Jamie M"  wrote in message  
news:mh6h12$7en$1@speranza.aioe.org... 
> Hi, 
> 
> I was wondering if code like x = Convert.ToInt32(Math.Sin(a) for 'a' 
> being an integer from 0 to n is periodic ie in C# code?  I was curious 
> and tested it but the output sequence was not periodic (at least until 
> I ran out of memory at about 10 million sequence length. 
> 
> This is the line of code I used in C# to generate a sinewave from 0 to 
> 9 with 10 million samples etc: 
> 
> int[] Sequence1 = Enumerable.Range(10000000).Select(a =>  
> Convert.ToInt32(Math.Sin(a) * 4.5 + 4.5)).ToArray(); 
> 
> If the sequence is not periodic then is it because of the 
> implementation of the C# math functions?  I think eventually it 
> should become periodic though.. 
> 
> cheers, 
> Jamie
Reply to
Tim Williams

pi is transcendental as is is defined by the math unit to more places than an integer can hold, so for intgers it will be aperiodic.

could it be anything else?

I can't see why it should be

the period of sin is 2*pi

pretend your sequence repeats after 710 ,

710/133 is close enough to 2pi for most purposes.
--
umop apisdn
Reply to
Jasen Betts

As Tim says, the period of the sine function is transcendental, so it can't be periodic in computer arithmetic.

You can form sin(2 pi x), which will be periodic except for roundoff.

Expecting FP calculations not to have roundoff is doomed except in very restricted cases.

Cheers

Phil Hobbs

Reply to
Phil Hobbs

Hi,

Thanks, the reason I was asking is I noticed there are very long matching sequences of the digits in different parts of the sequence.

Ie.

int[] subSequence1 = Enumerable.Range(0, 10000).Select(a => Convert.ToInt32(Math.Sin(a) * 4.5 + 4.5)).ToArray();

int[] superSequence1 = Enumerable.Range(10000, 10000000).Select(a => Convert.ToInt32(Math.Sin(a) * 4.5 + 4.5)).ToArray();

That generates two sequences, and then I found a match of 10000 consecutive digits from 93992 to 103992 in both sequences, even though they have different input indexes, one is a from 0 to 10000 and one is a from 10000 to 10000000.

There are likely multiple areas that match in the sequences too, but I only checked for the first match.

If I make the sequences larger, then also there are larger matching sections too.

Thoughts?

cheers, Jamie

Reply to
Jamie M

For the same reason, identities such as sin(pi-x)=x and sin(2*pi-x)=-x don't hold for computer arithmetic.

Related:

formatting link

Reply to
Nobody

Why not create your own sine function, by using Taylor's series expansion -- simple, and very easy to program.

Reply to
dakupoto

the floating point unit works with a 64 bit msantissa unless there have been improvements there since the 80486 there's no loss of precision on the way in from long integer.

I think you mean sin(pi-x)=sin(x) and sin(-x) = -sin(x)

--
umop apisdn
Reply to
Jasen Betts

It will require quite long series for any significant accuracy.

In practice, sin/cos has been implemented with 3-4th order polynomials for IEEE single precision (32 bit) or 6-8th order for double precision (64 bit).

Depending of the number of segments that the circle is divided into (each segments with different coefficients) 6-8th order is the typical compromise between speed and constant table sizes for double precision.

Reply to
upsidedown

Hi,

Cool, do you have any links for more info about this?

cheers, Jamie

Reply to
Jamie M

It is inevitable that you will get the odd chance match sequence for a few terms and that nearby you might get a few more but the longer term sequence necessarily drifts because of the irrational nature of pi.

That is what you would expect but it will *never* be periodic any more than the much simpler expression N mod 2pi.

Pi is irrational - get over it.

--
Regards, 
Martin Brown
Reply to
Martin Brown

With reduction of domain, using trig identities. The reference I have (Hart et al., "Computer Approximations", 1968ish) uses the half-angle tangent identities. Computing the tangent is easier because it has only odd-order terms. Rational functions do a bit better than polynomials.

Cheers

Phil Hobbs

--
Dr Philip C D Hobbs 
Principal Consultant 
ElectroOptical Innovations LLC 
Optics, Electro-optics, Photonics, Analog Electronics 

160 North State Road #203 
Briarcliff Manor NY 10510 

hobbs at electrooptical dot net 
http://electrooptical.net
Reply to
Phil Hobbs

In the 1960's, there was a book: Ralston & Wilf: Mathematical Methods for Digital Computers, with many approximations and the math behind them.

Sorry, cannot remember the publisher or ISBN.

--

-TV
Reply to
Tauno Voipio

Hi,

10000 or more consecutive digits in a row match multiple times in two C# sinewave sequences, that is more than a few digits, it is a periodicity overlap that increases in size the larger the sequences are.

cheers, Jamie

Reply to
Jamie M

The Fortran IV Plus compiler manual for PDP-11 had some information about the implementation but not the actual coefficients. All transcendent functions were implemented with polynomials.

The problem with early computers was the limited address space (64 KiB on PDP-11) so the run time library could not use large tables e.g. to split the angle to a large number of sectors with different coefficients.

Today, with usually lot of memory to burn, you could use

sin (X + y) = sin (X)*cos(y) + cos (X)*sin(y)

and express the angle e.g. as milliradians as X.y in which is the integer part of the angle in milliradians and y as the fractional part. sin(X) and cos(X) can be taken directly from a 6283 element table and only calculate sin(y) and cos(y) in any traditional way. With a big table y is very small and often sin(y) can be approximated as y and cos(y) is very close to 1 so a simple polynomials should be sufficient.

Reply to
upsidedown

I think the matching digit areas of two sequences of math.sin() for different input ranges, might be related to the "signature sequence" of Pi, but I'm not sure, just came across "signature sequences" yesterday, it is interesting a signature sequence for a transcendental number is a fractal sequence.

formatting link

Actually I think I'm all wrong about this since a signature sequence seems like it is just arranging the digits of an irrational number in increasing order..?

cheers, Jamie

Reply to
Jamie M

Indeed ;)

That's what I get for posting at 3am.

Reply to
Nobody

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.