Shop OBEX P1 Docs P2 Docs Learn Events
Complex equations?? — Parallax Forums

Complex equations??

James LongJames Long Posts: 1,181
edited 2006-09-30 01:20 in Propeller 1
Ok....what is the proper way to input complex equations into the IDE?

Like the following · √return_value * 2 / 1.225

would it be : showvalue := ^^((return_value * 2)/1.225)

I also know this would require the Float to string object. Because the result it a floating point number.

James L
·

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2006-09-24 16:27
    The IDE can only do floating point constant calculations. You have to use the floating point library for run-time floating point calculations and that comes out as a set of function calls. To do "showvalue := (return_value * 2.0)/1.225" you would have to write "showvalue := flt.FDiv(flt.FMul(return_value,2.0),1.225)" where the floating point object is defined as "OBJ flt : Float32".
  • James LongJames Long Posts: 1,181
    edited 2006-09-24 17:12
    Mike,

    Wouldn't I actually have to do·3 steps.....FMul...... FDiv....then FSqr?

    Because I'm doing three different floating point calculations.

    James
  • Mike GreenMike Green Posts: 23,101
    edited 2006-09-24 17:32
    I wasn't sure what the "^^" was for since you had a "√" in front. I couldn't tell if it was a mistake or square root, but yes, you then have to do an FSqr step too. In terms of anything other than a constant calculation, the IDE treats floating point numbers like 32 bit integers. If you want to imbed a constant floating point computation outside of the CON section, use the CONSTANT(...) function.
  • James LongJames Long Posts: 1,181
    edited 2006-09-24 23:31
    Yea...it was to be a square root symbol......but you know how things go. Hard to tell sometimes.



    I'll have to work· up the equation and see how it comes out.



    James
  • James LongJames Long Posts: 1,181
    edited 2006-09-25 02:22
    Well....it's done...but....(there is always a but) no decimal point. That is really strange......hmmmm....this is the first time I've use floating point numbers on a micro.

    I'm displaying by the hyperterminal....maybe it's my method of displaying them.

    Seems to dsiplay the right number....I really need to check.....but it doesn't have a decimal point.....is this usual?



    Man there are so many things that I'm trying to do....that I've never done...

    James L
  • Mike GreenMike Green Posts: 23,101
    edited 2006-09-25 04:24
    Usual? If you want an opinion or advice, you're going to have to supply more information. In particular, how are you getting the number into string form for display?
  • James LongJames Long Posts: 1,181
    edited 2006-09-25 22:12
    Mike,

    To be honest...I'm outputing the value with a dec command. I'm not outputing with a string.

    I'm concerned the program is loosing the decimal point location. If I convert to a string will the decimal point be in the right place?

    Hmmm.....more learning.

    James
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-09-25 22:38
    Using the dec command won't work, thats for decimals (ie integers). To display the value correctly you should send it in string format to Hyperterminal using the FloatString.FloatToString function, then send it as a string via the FullDuplex object. This conversion is just to get it into a format so it will display properly (including the decimal point).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.

    Post Edited (Paul Baker (Parallax)) : 9/25/2006 10:41:51 PM GMT
  • James LongJames Long Posts: 1,181
    edited 2006-09-26 01:14
    Paul,

    I'm already using the PC_debug item that uses the fullduplex.

    That will help......I'll post my results.



    James
  • James LongJames Long Posts: 1,181
    edited 2006-09-26 17:53
    Paul,



    That didn't work....the floattostring says it needs a urinary......hmmm

    Oh well,

    James
  • Mike GreenMike Green Posts: 23,101
    edited 2006-09-26 18:09
    James,
    Try serial.str(floatString.FloatToString(flt.FSqr(flt.FDiv(flt.FMul(return_value,2.0),1.225)))) where serial is the FullDuplexSerial driver, floatString is the FloatString object and flt is the FloatMath object. This should transmit an ASCII string with the value of the result of sqrt(return_value*2.0/1.225)
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-09-26 18:11
    Can you post your relevant code?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • James LongJames Long Posts: 1,181
    edited 2006-09-26 20:15
    Mike said...
    James,
    Try serial.str(floatString.FloatToString(flt.FSqr(flt.FDiv(flt.FMul(return_value,2.0),1.225)))) where serial is the FullDuplexSerial driver, floatString is the FloatString object and flt is the FloatMath object. This should transmit an ASCII string with the value of the result of sqrt(return_value*2.0/1.225)
    Wow....didn't know you could do all of that at once.....I will try it.
    Paul said...
    Can you post your relevant code
    Here it is......didn't know I could put it all into one....

    ····· ShowValue1 := f.fmul(Return_Value , 1.22)
    ····· ShowValue1 := f.fdiv(ShowValue1 , 450)
    ····· ShowValue1 := f.fmul(1000.000 , ShowValue1)
    ····· ShowValue1 := f.fmul( ShowValue1 , 2.000)
    ····· ShowValue1 := f.fdiv( ShowValue1 , 1.225)
    ····· ShowValue1 := f2.floattostring(^^ShowValue1)
    ····· debug.str(string(ShowValue1))

    Do I have to convert the "Return_Value" to a float before I do all of this?

    This is a mixed up mess.....so bear with my messy code. I've tried a few things (that didn't work). f = float32· f2 = floattostring

    James
  • James LongJames Long Posts: 1,181
    edited 2006-09-27 19:45
    Ok...so I have all of the value as listed above in, and seem to be working.



    I have another equation....I'm not sure how to put it:

    Z = --26216 x ln( P(kPa) / 101.304 )



    What has me is the "--26216".....can't figure out how to place that in an equation.



    James
  • Mike GreenMike Green Posts: 23,101
    edited 2006-09-27 22:06
    Since you can have floating point constant expressions evaluated by the IDE, how about using CONSTANT(-26216.0)?

    You'd have FMul(constant(-26216.0),FDiv(...,101.304)) where the ... is the P(kPa).
  • James LongJames Long Posts: 1,181
    edited 2006-09-27 22:22
    Mike,

    I've done some research...and I think the "--" symbol means a negative exponent, but I'm not too sure.
  • James LongJames Long Posts: 1,181
    edited 2006-09-28 01:44
    Nope....I was wrong....the "--26216" was the power to the -26216


    Someone check me on my input of this to make sure i'm not doing something wrong.
    input.ChangeControl(%10011110)··································································· 'Airspeed control bit set··
    ····· outa[noparse][[/noparse]16] := 1························································································· ·'Start ADC Loop
    ····· debug.dec(return_value)
    ····· debug.str(string(tab))···················································································
    ····· Kpa_value := full.fdiv(full.fmul(full.ffloat(return_value),1.22070),51.0000)
    ····· debug_value1 := floatstring.floattoformat(full.pow(full.log(full.fdiv((Kpa_value), 101.304)),-26216),3,1)
    ····· debug.str(debug_value1))··········································································'Show current Value
    ····· outa[noparse][[/noparse]16] := 0··························································································· 'Stop the ADC Loop
    ····· debug.str(string(Tab, Tab))······································································· ·'Place some space on screen
    ····· delay.pause1ms(250)·············································································· · 'Pause for 250 m seconds
    ····· input.ChangeControl(%10001110)································································'Altitude control bit set
    ····· outa[noparse][[/noparse]16] := 1···························································································· 'Start ADC Loop
    ····· debug.dec(Return_Value)
    ····· debug.str(string(tab))
    ····· Kpa_value := full.fdiv(full.fmul(full.ffloat(Return_Value), 1.22), 450)
    ····· debug_value2 := floatString.FloatToformat(full.FSqr(full.FDiv(full.FMul(full.fmul((Kpa_value),1000.0),2.0),1.225)),7,1)
    ····· debug.str(debug_value2)·········································································· ·'Show current value
    ····· outa[noparse][[/noparse]16] := 0··························································································· ·'Stop· the ADC Loop
    ····· delay.pause1ms(250)················································································ ·'Pause for 250 m seconds
    ····· debug.str(string(CR))················································································· ·'Carriage· Return
    I'm getting raw values.....but only 0.0 for the computated values.

    Hmmmmm.....Still getting close.....just need someone to check my syntax....and make sure I'm not missing something....which I would have to be.

    James
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-09-28 02:42
    James, I think its the equation thats off, anything to the -26216 power will be zero. Single precision·roughly has the range of 10^-38 to 10^38, your equation is dividing the result into an astonomically small (or should I say quantumly small) number that cannot fit into 32 bits, so its rounded to 0.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • James LongJames Long Posts: 1,181
    edited 2006-09-28 03:42
    Paul,

    Yea.....I'm still confused about the equation. I took that part of it out...and actually got a neg number.

    I really don't have a clue at this point.....but I'll find out.



    I'll do more research......Blahhh.......



    James
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-09-28 04:00
    The result being negative means the division inside the ln() is greater than 0 but less than the base of the logorithm which is e or 2.718....

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • James LongJames Long Posts: 1,181
    edited 2006-09-28 12:13
    Paul,

    To be frank (whoever that is) it's been a long time since I did any equations that complex. I don't remember much, because I didn't do many like that way back when.

    I'm having to ask a friend that has done these equations for a project a few years ago.

    James
  • James LongJames Long Posts: 1,181
    edited 2006-09-30 01:20
    Just for an update....I thought I would post the final results of my code that is now working....some of it was just my error in Propeller....and some was misunderstanding of the equation in question. (wow some 50 cent words in that statement.)

    NOTE:I have not confirmed that this is accurate.....just that I get numbers out.


    ·repeat
    ····· input.ChangeControl(%10011110)··································································· 'Airspeed control bit set··
    ····· outa[noparse][[/noparse]16] := 1···················································································· 'Start ADC Loop···············································································································································································
    ····· debug_value1 := floatString.FloatToformat(full.fmul(full.FSqr(full.FDiv(full.FMul(full.fmul(full.fdiv(full.fmul(full.fsub(full.ffloat(Return_Value), 185.0),1.220),450.0),1000.0),2.0),1.2250)),2.236936),7,2)
    ····· debug.str(debug_value1)········································································· 'Show current Value
    ····· debug.str(string("mph"))
    ····· outa[noparse][[/noparse]16] := 0···················································································· 'Stop the ADC Loop
    ····· debug.str(string(Tab, Tab))······································································ 'Place some space on screen
    ····· delay.pause1ms(1)·············································································· 'Pause for 500 m seconds
    ····· input.ChangeControl(%10001110)··································································· 'Altitude control bit set
    ····· outa[noparse][[/noparse]16] := 1···················································································· 'Start ADC Loop
    ····· debug_value2 := floatstring.floattoformat(full.fmul(full.fdiv(full.fdiv(full.fmul(full.pow(full.fdiv(full.fdiv(full.fmul(full.ffloat(Return_Value),0.81),65.0),101.325),0.190261),288.15),- 288.15),-0.0065),3.28),7,1)
    ····· debug.str(debug_value2)·········································································· 'Show current value
    ····· debug.str(string("ft"))
    ····· outa[noparse][[/noparse]16] := 0···················································································· 'Stop· the ADC Loop
    ····· delay.pause1ms(1)·············································································· 'Pause for 500 m seconds
    ····· debug.str(string(CR))············································································ 'Carriage· Return
    ··················
    Trust me when I say.....it took a a few tries to get it right.

    James
Sign In or Register to comment.