Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   math.h library (http://www.chiefdelphi.com/forums/showthread.php?t=23172)

Rickertsen2 03-01-2004 11:05

Re: math.h library
 
We have a floating point trig lib, but its not exactly as optimized as we would like. we can post it if you want though. Later versions will be MUCH better. The final version will have floating point trig as well as 16-bit brad trig(Which should be about at least 20 times faster than their floatign point counterparts). If there is demand, i will post out math libs under the condition that any work derived from our libraries be posted as well.

EricS-Team180 03-01-2004 18:49

Re: math.h library
 
Quote:

Originally Posted by Mike Betts
In our case, an integer in 2FX14 format.


ummm...that's a new one me or maybe I'm just missing something obvious...

just what is an FX format? I typically think of x has hex and f as floating point???

Thanks,
Eric

Joe Johnson 03-01-2004 19:36

Re: math.h library
 
Quote:

Originally Posted by EricS-Team180
ummm...that's a new one me or maybe I'm just missing something obvious...

just what is an FX format? I typically think of x has hex and f as floating point???

Thanks,
Eric


I am not sure of the notation, but if I were writing the sin & cos routines, I would return 16 bit signed number that would be be 2^15 (a.k.a 32768) times the "true" value of the functions. For example, while -1<= Sin <= 1, I would have -32768 <= DrJoeSin <= 32768. Where DrJoeSin = 32768 * Sin

As to Tan this is trickier. My first thought would be to just to just use the definition of tan = sin/cos which would mean tan = DrJoeSin/DrJoeCos. In principle this is okay, but in integer math this is pretty aweful...

...Tan has another problem in that cos can equal zero (or tan can equal infinity). If we are going to return a 16 bit answer, infinity, in this case, is going to have to be 2^15 I suppose.

That still leaves us with a problem. If we are going to use the same basic trick of defining DrJoeTan = ScaleFactor * Tan.

The practical problem is that you don't know where the user of the tan function is going to want his/her function to be accurate. If ScaleFactor is 32768 as in sin and cos, then whenever tan is greater than 1 (that is, whenever abs(theta) > pi/4), DrJoeTan is going to return 32768 (a.k.a. infinity)... ...not exactly a very good result. But then again, in the range that it works (whenever abs(theta) < pi/4), it returns the most data you can expect from a 16 bit number.

Is this a case were we would like to have a modified floating point number format? Perhaps something where we return an exponent and a mantissa? Perhaps it would be even better to have a combined Tan/Sec function that returns a flag telling you if it is returning Sec or Tan and then it returns a 16 bit value for either Tan or Sec depending on which one is less than "infinity"? <-- a crazy idea, but it could be made to work I suppose.

I suppose that this is all a solved problem but I'll be dag nabbed if I have worked it all out already... ...and kickoff is less than a week away...

As another exercise for the user, I have been using radians as my argument for sin, cos, tan, etc. but of course this is non-sense in interger land. Should we define a 16 bit equivalent to PBasic's Brads (all funtions are defined from -32767 to 32768 where 32768 = pi and -32768 = -pi)?

Just more grist for the mill...

Joe J.

P.S. Do you think FIRST really thought all this through for us when the made the leap to C? I suppose not. This is going to be a tough year for folks to learn on. Buckle your seatbelts, it is going to be a bumpy ride...

Anthony Kesich 07-01-2004 00:16

inverse trig functions
 
i found another point of the taylor series to give arcsin.

arcsin(x)= x + (1/2)(x^3/3)+(1/2)(3/4)(x^5/5)+(1/2)(3/4)(5/6)(x^7/7)...

from this we can then derive both arc cos and arctan.

for arccos, use the first and most basic trig Identity:

sin^2(x)+cos^2(x)=1
sin^2(x)=1-cos^2(x)
sin(x)=sqrt(1-cos^2(x))

so just plug the cos value (now called c) into sqrt(1-c^2) then evaluate arcsin using taylor series with the value you pulled out of sqrt(1-c^2).

so arccos(x)=c+(1/2)(c^3/3)+(1/2)(3/4)(c^5/5)+(1/2)(3/4)(5/6)(c^7/7) where c=sqrt(1-cos^2(x)).

now for arctan. I used a triangle.

/|C
/ |
/ |
sqrt(x^2+1) / |
/ | x
/ |
/ |
/ |
A -------B
1

(if the triangle displays correctly, good for you, but if it doesn't, it is set up such that Leg AB is 1, Leg BC is x, angle B is 90 degrees, and hypotenuse AC is sqrt(x^2+1).)

therefore sin(A)=x/sqrt(x^2+1) and tan(A)=x/1=x
so then A=arcsin(x/sqrt(x^2+1)) and A=arctan(x)
therefore arctan(x)=arcsin(x/sqrt(x^2+1))

hope you have fun with this
-Kesich

deltacoder1020 15-01-2004 12:42

Re: math.h library
 
Quote:

Originally Posted by Kevin Watson
Well, I shouldn't have made such a generalization. Integer division by powers of two is very fast (if the compiler is designed for such an optimization) because you only need to do a binary right shift to divide a number by two (multiplication works by shifting left). Division of two eighty-bit IEEE floating-point numbers on an eight-bit microcontroller is very painful.

you could always hardcode the bitshift for the division to make sure that the optimization is in, even if the compiler doesn't automatically do it - just replace "x / 16" with "x >> 4"

Mike Betts 15-01-2004 15:37

Re: math.h library
 
Quote:

Originally Posted by deltacoder1020
you could always hardcode the bitshift for the division to make sure that the optimization is in, even if the compiler doesn't automatically do it - just replace "x / 16" with "x >> 4"

"x / 16" with "x >> 3"

Greg Ross 15-01-2004 16:10

Re: math.h library
 
Quote:

Originally Posted by Mike Betts
"x / 16" with "x >> 3"

Oops, Mike. 4 is the right answer. :p

x >> 0 == x / 1
x >> 1 == x / 2
x >> 2 == x / 4
x >> 3 == x / 8
x >> 4 == x / 16

Mike Betts 15-01-2004 16:17

Re: math.h library
 
Quote:

Originally Posted by gwross
Oops, Mike. 4 is the right answer

I stand corrected...

deltacoder1020 15-01-2004 21:16

Re: math.h library
 
also, if you use bitshift to do multiplication by powers of two, make sure you check bounds first, lest you lose significant bits in an overflow.

ShadowKnight 17-01-2004 13:10

Re: math.h library
 
Quote:

Originally Posted by Mike Betts
Since our team will be using integer arithmetic exclusively on the PIC, we generated a table using Excel (saved as a CSV file) and imported into the PIC code as a table of constants existing in ROM space.


How do you imprt into the ROM space :confused: ? I didn't see anything in the reference guide about that.


All times are GMT -5. The time now is 18:00.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi