EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

Why isn't math universal?

Started by Thomas Magma September 1, 2004
Hi,

I just wrote some DSP code in Java and ported it over to EVC for a PDA.
Apparently EVC has a problem with the sin() of large numbers. What's up with
this? I need to sin() large numbers. Any suggestions?

Am I the only one that thinks that C, C++, and the MS foundation classes are
a mess, and should be abolished?

Thomas



Thomas Magma wrote:

> Hi, > > I just wrote some DSP code in Java and ported it over to EVC for a PDA. > Apparently EVC has a problem with the sin() of large numbers. What's up with > this? I need to sin() large numbers. Any suggestions? > > Am I the only one that thinks that C, C++, and the MS foundation classes are > a mess, and should be abolished? > > Thomas >
Why not use sin( x % TWO_PI ); where TWO_PI is 6.283185..... assuming x is in radians?
Thomas Magma <somewhere@overtherainbow.com> wrote:

> I just wrote some DSP code in Java and ported it over to EVC for a PDA. > Apparently EVC has a problem with the sin() of large numbers. What's up with > this?
Quite impossible to tell, until you specify what "large" is supposed to mean, in this context. From the point of view of the sine function, an argument of 100 may already appear somewhat large, and an argument in anywhere outside the range of +/- 1/DBL_EPSILON will mean there's nothing left to salvage.
> I need to sin() large numbers. Any suggestions?
Double-check your needs. Carefully. -- Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de) Even if all the snow were burnt, ashes would remain.
Could you explain that to me slowly. Are you suggesting that sin(x%*2*pi) =
sin(x*2*pi)?

"Dave Rooney" <rooney@adi.com> wrote in message
news:ch51aj$jt3$1@bluegill.adi.com...
> > > Thomas Magma wrote: > > > Hi, > > > > I just wrote some DSP code in Java and ported it over to EVC for a PDA. > > Apparently EVC has a problem with the sin() of large numbers. What's up
with
> > this? I need to sin() large numbers. Any suggestions? > > > > Am I the only one that thinks that C, C++, and the MS foundation classes
are
> > a mess, and should be abolished? > > > > Thomas > > > Why not use sin( x % TWO_PI ); where TWO_PI is 6.283185..... assuming > x is in radians? > > >
Well it's giving me the old -1.#IND00 at around sin(220000000.0). Shouldn't
the sin of any real number (double) equal something real? Java doesn't have
a problem with this.


"Hans-Bernhard Broeker" <broeker@physik.rwth-aachen.de> wrote in message
news:2pmh1jFmkd0fU2@uni-berlin.de...
> Thomas Magma <somewhere@overtherainbow.com> wrote: > > > I just wrote some DSP code in Java and ported it over to EVC for a PDA. > > Apparently EVC has a problem with the sin() of large numbers. What's up
with
> > this? > > Quite impossible to tell, until you specify what "large" is supposed > to mean, in this context. From the point of view of the sine > function, an argument of 100 may already appear somewhat large, and an > argument in anywhere outside the range of +/- 1/DBL_EPSILON will mean > there's nothing left to salvage. > > > I need to sin() large numbers. Any suggestions? > > Double-check your needs. Carefully. > > -- > Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de) > Even if all the snow were burnt, ashes would remain.
Nor does a 3 dollar calculator!!!

"Thomas Magma" <somewhere@overtherainbow.com> wrote in message
news:qfoZc.294621$gE.93374@pd7tw3no...
> Well it's giving me the old -1.#IND00 at around sin(220000000.0).
Shouldn't
> the sin of any real number (double) equal something real? Java doesn't
have
> a problem with this. > > > "Hans-Bernhard Broeker" <broeker@physik.rwth-aachen.de> wrote in message > news:2pmh1jFmkd0fU2@uni-berlin.de... > > Thomas Magma <somewhere@overtherainbow.com> wrote: > > > > > I just wrote some DSP code in Java and ported it over to EVC for a
PDA.
> > > Apparently EVC has a problem with the sin() of large numbers. What's
up
> with > > > this? > > > > Quite impossible to tell, until you specify what "large" is supposed > > to mean, in this context. From the point of view of the sine > > function, an argument of 100 may already appear somewhat large, and an > > argument in anywhere outside the range of +/- 1/DBL_EPSILON will mean > > there's nothing left to salvage. > > > > > I need to sin() large numbers. Any suggestions? > > > > Double-check your needs. Carefully. > > > > -- > > Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de) > > Even if all the snow were burnt, ashes would remain. > >
Thomas Magma <somewhere@overtherainbow.com> wrote:


> "Hans-Bernhard Broeker" <broeker@physik.rwth-aachen.de> wrote in message > news:2pmh1jFmkd0fU2@uni-berlin.de...
> > Quite impossible to tell, until you specify what "large" is supposed > > to mean, in this context. From the point of view of the sine > > function, an argument of 100 may already appear somewhat large, and an > > argument in anywhere outside the range of +/- 1/DBL_EPSILON will mean > > there's nothing left to salvage.
[Please don't top-post&full-quote blindly. Fixed.]
> Well it's giving me the old -1.#IND00 at around sin(220000000.0). Shouldn't > the sin of any real number (double) equal something real?
In a perfect world: yes. Reality doesn't work that way though.
> Java doesn't have a problem with this.
Well, then try sin(1e20) in Java or in your calculator. Does the result remind of something you saw recently? At full IEEE double-precision floating point (i.e. 52 bits of mantissa precision), sin(1e20) returns "undefined value". So your embedded platform, which quite probably has fewer bits to spare, already freaks out at 2.2e9 --- I can't say that has me particularly surprised. Actually, that's quite exactly the region where I would expect a 32bit mantissa to fail. The problem is that for a number so large that a single bit step in the mantissa moves the argument by more than one period of the sine function, there *is* no sensible result that could be returned by sin() any more. That's like asking which atom in the floor tiling you would hit as you stamp down your foot. -- Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de) Even if all the snow were burnt, ashes would remain.
"Thomas Magma" <somewhere@overtherainbow.com> wrote in message
news:D8oZc.280449$M95.142025@pd7tw1no...
> Could you explain that to me slowly. Are you suggesting that sin(x%*2*pi)
=
> sin(x*2*pi)?
I think the point was that sin(), by definition, takes an angle - which is conventionally in the range 0-360 degrees. If you're dealing with angles of
> 1 cycle, consider converting to 0-360 degrees (or the radian equivalent)
using e.g. the modulus function (i.e. "%"). HTH, Steve http://www.fivetrees.com
On Wed, 01 Sep 2004 13:36:18 -0400, the renowned Dave Rooney
<rooney@adi.com> wrote:

> > >Thomas Magma wrote: > >> Hi, >> >> I just wrote some DSP code in Java and ported it over to EVC for a PDA. >> Apparently EVC has a problem with the sin() of large numbers. What's up with >> this? I need to sin() large numbers. Any suggestions? >> >> Am I the only one that thinks that C, C++, and the MS foundation classes are >> a mess, and should be abolished? >> >> Thomas >> >Why not use sin( x % TWO_PI ); where TWO_PI is 6.283185..... assuming >x is in radians?
Of course you lose significant digits when you take the sin() of a large number. Especially, since MS stuff is broken so it typically uses 64 bits rather than the 80 bits the FPU is capable of, this may be an issue. Best regards, Spehro Pefhany -- "it's the network..." "The Journey is the reward" speff@interlog.com Info for manufacturers: http://www.trexon.com Embedded software/hardware/analog Info for designers: http://www.speff.com

Thomas Magma wrote:

> Could you explain that to me slowly. Are you suggesting that sin(x%*2*pi) = > sin(x*2*pi)? > > "Dave Rooney" <rooney@adi.com> wrote in message > news:ch51aj$jt3$1@bluegill.adi.com... > >> >>Thomas Magma wrote: >> >> >>>Hi, >>> >>>I just wrote some DSP code in Java and ported it over to EVC for a PDA. >>>Apparently EVC has a problem with the sin() of large numbers. What's up > > with > >>>this? I need to sin() large numbers. Any suggestions? >>> >>>Am I the only one that thinks that C, C++, and the MS foundation classes > > are > >>>a mess, and should be abolished? >>> >>>Thomas >>> >> >>Why not use sin( x % TWO_PI ); where TWO_PI is 6.283185..... assuming >>x is in radians? >> >>
What I meant was that the sin() function repeats for every 2 * pi radians of the input argument so if you take the modulus 2 * pi of the input argument and take the sin() if that you should get the correct answer. I should have said sin( fmod( x, TWO_PI ) ); I used the integer modulus operator (%) in my original post. As someone else pointed out you have a problem with loosing resolution as the angles get very large. Dave Rooney

The 2024 Embedded Online Conference