Shop OBEX P1 Docs P2 Docs Learn Events
how do i convert a VAR string to an Array? NOW a MATH problem — Parallax Forums

how do i convert a VAR string to an Array? NOW a MATH problem

grasshoppergrasshopper Posts: 438
edited 2008-06-29 02:08 in Propeller 1
How can i convert a string that is a long into a array. I wish to convert a number 2263 into 22.63? so I was thinking that i convert 2263 into an array then manuplate the array so that i have 22.63.

Post Edited (grasshopper) : 6/18/2008 10:13:19 PM GMT

Comments

  • ColeyColey Posts: 1,110
    edited 2008-06-18 20:01
    Hi,

    Have a look at the format object in the object exchange, I think you'll find some routines in there that will do what you want.

    obex.parallax.com/objects/230/

    Regards,

    Coley

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    PropGFX Forums - The home of the Hybrid Development System and PropGFX Lite
  • grasshoppergrasshopper Posts: 438
    edited 2008-06-18 22:10
    Well thanks for the link however i found a more suiting object, the FloatString. I remain boggled still in the fact that i can use this code and it works fine


    pub Calc_Slope | B, M, subtracted, slope, invert
    
      M :=  135
      B :=  35693
      subtracted := f.FSub(lm135, B)                        'Y - B
       
      slope := f.FDiv(subtracted,M)                         'Y - B / M
    
      lcd.pos(1,1)
      lcd.writestr(@screen1)
      
      lcd.writestr(fs.Floattostring(slope))
      lcd.pos(1,15)
      lcd.writestr(@screen0) 
    
    
    



    But when I replace M with the value of

    
      M :=  135.25
    
    
    



    My math is all incorrect.

    Please help me i have tryed several varations of the

    
     slope := f.FDiv(subtracted,M)                         'Y - B / M
    
    
    



    i.e.

    
     slope := f.foat(f.FDiv(subtracted,M))                         'Y - B / M
    
    
    
  • Mike GreenMike Green Posts: 23,101
    edited 2008-06-18 22:13
    Your problem is that you're trying to do floating point arithmetic on 32-bit integers. Try assigning 35693.0 to B as well as 135.0 to M.
  • grasshoppergrasshopper Posts: 438
    edited 2008-06-18 22:21
    By the way i am using the Float32 object.

    Mike my life saver:

    I did as you asked but no luck. I got a negative [noparse][[/noparse] -264. ] when the number should be around 23.62.

    Any thing else? Please help
  • grasshoppergrasshopper Posts: 438
    edited 2008-06-18 22:30
    ** SOLVED thanks Mike you always trigger something that allows me to solve the problem

    
      M :=  135.5
      B :=  35693
      
      subtracted := F.FSub(lm135,B)                        'Y - B
      
      slope := F.FDiv(f.ffloat(subtracted),M)                        'Y - B / M
      
      lcd.pos(1,1)
      lcd.writestr(@screen1)
      
      lcd.writestr(fs.Floattostring(slope))
      lcd.pos(1,15)
      lcd.writestr(@screen0) 
    
    
    
  • Mike GreenMike Green Posts: 23,101
    edited 2008-06-18 22:35
    The problem is not solved. You still have things wrong. Even though the result may look correct, it's wrong. You need:
    M := 135.5
    B := 35693.0
    subtracted := F.FSub(lm135,B) ' assuming lm135 is floating point
    slope := F.FDiv(subtracted,M)
    
  • grasshoppergrasshopper Posts: 438
    edited 2008-06-19 01:41
    Mike:

    I think it works fine the lm135 is not floating point, so i assume that i can just subtract the two. Please let me know your concerns. And thanks a million for the help.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-06-19 01:47
    You can't use F.FSub to subtract non-floating point values. It may work sometimes, but it's a fluke of the way that floating point values are represented and manipulated. Use the integer operators like "+", "-", "*", "/", then use F.FFloat to convert the result to floating point.
  • grasshoppergrasshopper Posts: 438
    edited 2008-06-19 02:10
    Mike:

    It works as you requested and again i thank you for the wisdom that you reflect upon me. For the record, perhaps others will benefit the final code is below.

    
    pub Calc_Slope | B, M, subtracted, slope
    
    
      M :=  135.51
      B :=  35693
      
      subtracted := lm135 - B                                                 'Y - B
      
      slope := F.FDiv(f.ffloat(subtracted),M)                                       'Y - B / M
      
      lcd.pos(1,1)
      lcd.writestr(@screen1)
      
      lcd.writestr(fs.Floattostring(slope))
      lcd.pos(1,15)
      lcd.writestr(@screen0) 
    
    
    




    ** as a side note the 2x20 display i am using does not like the F.S.Floattostring(slope) statement. It displays 5-6 characters after the number is shown. The characters are not any thing i have ever seen but i fix this by clearing the letters with more code after the F.F.Floattostring(slope) statement **
  • Mike GreenMike Green Posts: 23,101
    edited 2008-06-19 02:16
    Did you make sure to change the "FloatMath" in "FloatString" to match what you're using for the other floating point?

    You didn't show how you positioned the output string on the LCD display. The "writestr" just puts out what's in the string and anything left on the display from before is left unchanged. You at least need to erase the rest of the current line. When I've done similar things, I save the pointer in a local variable and do STRSIZE on it to get the number of characters. I then output enough spaces to clear the rest of the current display line.

    Post Edited (Mike Green) : 6/19/2008 2:21:56 AM GMT
  • grasshoppergrasshopper Posts: 438
    edited 2008-06-19 02:20
    Yes i replaced the F with a G and still weird characters. Good catch though.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-06-19 02:26
    As is usually true, you would have to post your whole program to get specific advice on this. This sort of problem is usually due to something not shown in the little fragments you've posted.
  • grasshoppergrasshopper Posts: 438
    edited 2008-06-19 02:33
    Ok here it is

    _clkmode = xtal1 + pll16x
        _xinfreq = 6_000_000 
      
    CON     
    
    
      AD_CS         = 9             'AD Chip Select
      AD_Din        = 10            'AD Data IN
      AD_Dout       = 11            'AD Data out
      AD_Dclk       = 8             'AD clock
      
      Chan0         = %10010111     'board temp
      Chan1         = %11010111     'lm135                            
      Chan2         = %10100111     'l                             
      Chan3         = %11100111     'e
      
      eos           = $ff
      cr            = $0D
                     
      
    var
    
      long  counter
      long  Lm135
      long  setpoint
      long  slope  
      long  temp_data 
    
      
    OBJ
    
      BASIC         : "BS2_Functions"
      lcd           : "LcdDriver"
      G             : "Floatmath"
      fs            : "floatString"
        
    
    pub start | X
    
      init
     
      repeat
         
          repeat x from 0 to 25
          
              AtoD
              Calc_Slope
              
    
    
          Display_it
          
                           
    Pub Init
      
     ' G.start                                               'Start math obj
      lcd.init                                              'Init display
      lcd.cls                                               'Clear display
    
      
    Pub AtoD | Temp, X                        ' Read A to D
    
        temp := 0 
        lm135 := 0 
       repeat X from 0 to 24
          outa[noparse][[/noparse]AD_CS]~                                                                'Chip Select active low "ON"   
          Basic.SHIFTOUT(AD_Din,AD_DClk,Chan1,BASIC#MSBFIRST,8)                       'Chan 1 is Lm134                        
          Temp := BASIC.SHIFTIN(AD_Dout,AD_DCLK,BASIC#MSBPOST,16)                     'Shift in 16 bit
          outa[noparse][[/noparse]AD_CS]~~                                                               'Turn it OFF" 
          lm135 := Temp + lm135
    
      lm135 := lm135 / 25
        
    Pub Calc_Slope | B, M, subtracted
    
    
      M :=  135.51
      B :=  35693
      
      subtracted := lm135 - B                                                 'Y - B
      
      slope := G.FDiv(G.ffloat(subtracted),M)                                       'Y - B / M
    
        
    Pub Display_it
    
      lcd.cls
      lcd.home
      lcd.pos(1,1)
      lcd.writestr(@screen1)
      
      lcd.writestr(fs.Floattostring(slope))
      lcd.pos(1,15)
      lcd.writestr(@screen3)
    
                     
    dat
    
      screen1      byte "Holder = ",eos
      screen2      byte "Target = ",eos
      screen3      byte " ",$DF,"C",eos
    
    
    
  • Mike GreenMike Green Posts: 23,101
    edited 2008-06-19 02:39
    The problem you're running into is that strings are terminated with a zero byte and you're using $ff as "eos". Change the definition of "eos" to zero and things should work better.
  • grasshoppergrasshopper Posts: 438
    edited 2008-06-19 03:38
    hate to be a pain but that made it real bad. I got some real cool space age flashing characters flopping around real fast.


    
    con 
    
    eos   = $00
    
    
    



    Then i fixed it in the LCD object and now works great. Thanks again Mike. I owe you a few...

    Post Edited (grasshopper) : 6/19/2008 3:44:26 AM GMT
  • grasshoppergrasshopper Posts: 438
    edited 2008-06-28 20:17
    Mike I have a little problem with the change you mentioned 00 for eos vs. $FF for eos. The display reads a correct value but it looks horrible for example

    lcd reads 20.3 instead of 20.30 i want it to look uniform throughout the numbers

    Any ideas ?
  • Mike GreenMike Green Posts: 23,101
    edited 2008-06-28 21:30
    Use FloatToFormat instead of FloatToString. Read the comments in FloatString for details.
  • grasshoppergrasshopper Posts: 438
    edited 2008-06-29 02:08
    Thanks Mr.Green. Apparently I had an outdated object.

    Again Thanks
Sign In or Register to comment.