Shop OBEX P1 Docs P2 Docs Learn Events
Math/Floating point math HELP — Parallax Forums

Math/Floating point math HELP

marzec309marzec309 Posts: 146
edited 2009-09-14 11:11 in Propeller 1
OK. The PROP is still fairly new to me and I'm trying to get it to solve some sin equations using the Float32Full Object. I just cant seem to make any sense of it. I think I'm overflowing. Here's what I'm attempting to do.

Find the sin of a degree 1 to 90, then Multiply the result by the peak volts. This will run on a cog by itself to produce a Sine Wave.

Simplified code:

Vp := 2047

Repeat


repeat index from 1 to 90
SIN := math.sin(Index)
v := math.FMul(Vp,SIN)
Vout := v + 2047

repeat index from 89 to 0
SIN := math.sin(Index)
v := math.FMul(Vp,SIN)
Vout := v + 2047

repeat index from 1 to 90
SIN := math.sin(Index)
v := math.FMul(Vp,SIN)
Vout := 2047 - v

repeat index from 89 to 0
SIN := math.sin(Index)
v := math.FMul(Vp,SIN)
Vout := 2047 - v



Is there a better way to do this, Or is there a way to limit the resolution to prevent an overflow. I know assembly would be better at this but I have never used it.

So far I've got it doing Half of what i want. I Hard coded every value from 0 to 360 degrees with a simple waitcnt command between each line of code, to control the frequency but i would like to be able to calculate those values at run time so i can have the prop regulate the Vp value.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-08-13 15:47
    Your biggest problem is that you're mixing integer and floating point arithmetic. "+" is an integer operation and "index" is an integer value. Try the following:
    vp := 2047.0
    repeat index from 1 to 90
       sin := math.sin(math.ffloat(index))
       vout := math.fadd(2047.0,math.fmul(vp,sin))
    


    Note that adding a decimal point (".0") makes constants into floating point.

    Post Edited (Mike Green) : 8/13/2009 3:52:47 PM GMT
  • marzec309marzec309 Posts: 146
    edited 2009-08-13 16:28
    That may have been what i was missing. Thanks I will give it a shot tonight and let you know if it works.

    My Goal is to create a 12VAC source, that i can step up to 120VAC with a transformer. By monitoring the High voltage side of the transformer, the prop will adjust the amplitude of the low voltage AC. Thus giving me a regulated 120VAC True Sine Wave inverter. I don't think I will be able to push much current as the dc current will be roughly 10 times the ac load. But should do the trick. will post more info as it progresses or if i run into another problem, I'm sure i will.
  • yarisboyyarisboy Posts: 245
    edited 2009-09-12 14:22
    My Propeller manual is perhaps the wrong book to look up the things spoken of here. I don't find any reference in the index, the reserved work list, or the commands list for spin for things like math.sin, math.ffloat, or math.fmul. I also have looked in the library list for objects that might shed some light on this. No joy. RTFM? That all I do any more. I've already put the hardware away until I find the stuff I haven't downloaded yet apparently.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    MOORE'S LAW: The capabilities of electronics shall double every 18 months.
    cloyd's corollary: Hardware is easy, software is hard.
  • John AbshierJohn Abshier Posts: 1,116
    edited 2009-09-12 14:42
    The objects you want are Float32.spin and Float32Full.spin. In the Examples\Library folder is a PDF on their use. You could also use FloatMath.spin, but it is pure spin and slow. I think that all the spin programs and the pdf come in the starndard distribution of the Spin compiler.

    John Abshier
  • localrogerlocalroger Posts: 3,452
    edited 2009-09-12 14:54
    Floating point math is a very poor way to accomplish what you're doing, marzec309; it's very computationally intensive and introduces all kinds of variable delays. The Prop has an integer sine table in its ROM which is intended for just this sort of thing, and is much easier and faster to use than the floating point object. Google coughed this up: forums.parallax.com/forums/attach.aspx?a=33123
  • marzec309marzec309 Posts: 146
    edited 2009-09-14 11:11
    Yes, I did come to the conclusion that floating point is way to slow. I finally resorted to a lookup table for the SIN constants and just off set them as required. it worked pretty good.
Sign In or Register to comment.