Shop OBEX P1 Docs P2 Docs Learn Events
PropBasic - Multiply — Parallax Forums

PropBasic - Multiply

camelot2camelot2 Posts: 54
edited 2012-01-04 12:10 in Propeller 1
hi, I am trying to calculate frqa for a pulse generator project. The frequency I am trying to program is 2222Hz, the sysfreq is 100_000_000Hz. The formula I am using is:
frqa = myfreq X (2^32/sysfreq) = 2222 X 42.94967296 = 95434.17331712 and rounded frqa = 95434. But the program gives 93321 !!!!!
Attached is the PropBasic program. WHAT AM I DOING WRONG ?
thanks for your help - Dave

Comments

  • Martin_HMartin_H Posts: 4,051
    edited 2012-01-04 06:48
    Please post your code as a compiler/interpreter can either truncate the float or promote the integer depending upon how the code is written. I would bet you are mixing integer and floating point operations and getting bit by a truncation error in an intermediate step of the calculation. It's happened to me more times than I can count over the years and has been a source of numerous obscure bugs.

    Update: Sorry didn't see the attachment. After looking at it, this is the math you are trying to do:

    Result = 42 * 2222 + 0.94967296 * 2222
    Result = 93324 + 2110 (Actually 2110.17331712 but truncated to an integer)
    Result = 95434

    You are computing the whole quantity correctly, but the fractional compution is likely wrong and is coming out negative. Likely because of this step:

    value_dec = value ** 4078814305

    In reading the prop basic manual I think that code looks good and I'm not sure what is wrong with it. If you just do that operation what value do you get?
  • BeanBean Posts: 8,129
    edited 2012-01-04 10:07
    In the code you have
     'multiply 2222 X 42.94967296 from Win calculator
     '0.94967296 x 65536 x 65536 = 4078814305.09551616
      value = 42
      value_dec = value ** 4078814305
      value_whole = 2222 * value
      value_total = value_whole + value_dec
    

    I think you want
     'multiply 2222 X 42.94967296 from Win calculator
     '0.94967296 x 65536 x 65536 = 4078814305.09551616
      value = 2222
      value_dec = value ** 4078814305 ' 2110
      value_whole = value * 42 ' 93324
      value_total = value_whole + value_dec ' 95434
    

    Bean
  • camelot2camelot2 Posts: 54
    edited 2012-01-04 11:20
    thank you both for the replies.
    Bean, I replaced my code with your code, see attached
    results:
    value_whole = 93324
    value_dec = 4294967184
    value_total = 93212

    It still is not giving the correct result.
    Any thoughts as to why ? thanks for helping
  • BeanBean Posts: 8,129
    edited 2012-01-04 11:32
    Oh, I see the problem. The value 4078814305 is seen as a negative value (since bit 32 is set) by the multiply routine.

    You'll have to use half the value, then shift the result.
      value = 2222
      value_dec = value ** 2039407152 ' 1055
      value_dec = value_dec * 2 ' 2110
      value_whole = value * 42 ' 93324
      value_total = value_whole + value_dec ' 95434
    

    Bean
  • BeanBean Posts: 8,129
    edited 2012-01-04 11:44
    You could also shift the original value up, then do the ** with the constant shifted down the same amount.
      value = 2222
      value_total = value << 7 ' (* 128)
      value_total = value_total ** 1441151881 '  (42.94967296 / 128) * 65536 * 65536
    

    Bean
  • pedwardpedward Posts: 1,642
    edited 2012-01-04 11:44
    ugh, so much easier in SPIN:

    FRQA := 2222 / CLKFREQ << 32

    Well, that's algebra for ya! (yeah, sorry, wrong!)
  • BeanBean Posts: 8,129
    edited 2012-01-04 11:49
    pedward wrote: »
    ugh, so much easier in SPIN:

    FRQA := 2222 / CLKFREQ << 32

    Hmmm, I don't think it is quite that easy... Try it.

    Bean
  • camelot2camelot2 Posts: 54
    edited 2012-01-04 11:50
    thanks Bean, it now WORKS.
    Last question - How do I know if a value is seen as a negative value ?
  • pedwardpedward Posts: 1,642
    edited 2012-01-04 11:52
    Bean wrote: »
    Hmmm, I don't think it is quite that easy... Try it.

    Bean

    Doh, you read my reply before I thought about it too hard. Yeah, you caught me using Algebra!
  • BeanBean Posts: 8,129
    edited 2012-01-04 11:54
    If it has bit 32 set, it is seen as negative.
    > $7FFF_FFFF or 2147483647

    Bean
  • pedwardpedward Posts: 1,642
    edited 2012-01-04 12:10
    Bean wrote: »
    If it has bit 32 set, it is seen as negative.
    > $7FFF_FFFF or 2147483647

    Bean

    Just to clarify some fine points, if you start with a small value and want to bit shift it above $7FFF_FFFF, it will not consider sign, you will just end up with the bits shifted (which will look funny when interpreted as a signed value).

    I do wish the prop had the ability to specify unsigned in calculations, when using SPIN.
Sign In or Register to comment.