Re: Why isn't math universal?

From: Paul Keinanen (keinanen_at_sci.fi)
Date: 09/01/04


Date: Wed, 01 Sep 2004 23:31:09 +0300

On Wed, 01 Sep 2004 16:38:47 GMT, "Thomas Magma"
<somewhere@overtherainbow.com> 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?

No doubt you are going to be in trouble taking sin(x) if x is float
and x is a few millions. Since the mantissa in the IEEE float is only
24 bits and if you have a value around 1E6, the integer part requires
about 20 bits and only 4 bits is available for the decimal part, thus
you can have arguments at about every ten degrees only. You can not
represent any angle in between.

If you are going to need double precession for the argument, which
usually means more than 50 bits for the mantissa. Assuming you have an
int(x) function that will take the integer part (truncate) of a double
argument x and return a 32 bit integer value. If the double argument X
is positive and less than about 12E9, the following should give a
reasonable accuracy.

double frac_x, X, x1, y ;

x1 = X / TWO_PI ;
frac_x = x1 - int(x1) ;
y = sin(TWO_PI*frac_x) ;

Even at 12E9, there will be about 20 meaningful fractional bits, so
you will still get different values for each arc second. If you need
larger arguments than 12E9, you need an int function that returns a 64
bit value.

How the negative values of X are handled, should be quite obvious.

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

While the MFC deserves a lot of criticism, I do not understand why
your problems with sin(x) should justify that judgement. I have
patched up some mathematical libraries written in assembler, so
clearly the language is not an issue in writing bad math functions.

Paul



Relevant Pages

  • Re: Why isnt math universal?
    ... From the point of view of the sine ... >> there's nothing left to salvage. ... At full IEEE double-precision floating point (i.e. 52 bits of mantissa ... sin() any more. ...
    (comp.arch.embedded)
  • Re: Why isnt math universal?
    ... Thomas Magma wrote: ... > Apparently EVC has a problem with the sin() of large numbers. ... Even if all the snow were burnt, ashes would remain. ...
    (comp.arch.embedded)
  • Re: Why isnt math universal?
    ... Thomas Magma 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 sinof large numbers. ... I need to sin() large numbers. ...
    (comp.arch.embedded)
  • Why isnt math universal?
    ... 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. ...
    (comp.arch.embedded)
  • Why isnt math universal?
    ... 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. ...
    (microsoft.public.pocketpc.developer)