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

PropBasic - Multiplication

camelot2camelot2 Posts: 54
edited 2010-04-07 00:14 in Propeller 1
How does one do the following multiplication in PropBasic?
··· answer = 42.94967296 x 1_000_000
thanks for your help

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-04-06 19:31
    You can't ... because PropBasic is integer-only and one of your numbers is not an integer.

    There is a very nice floating point library available from the Object Exchange written in a combination of Spin and assembly where the various floating point operations are all called as functions. It would be possible to get this to work with PropBasic, but it would take a bit of work and the floating point operations would still have to be done using function calls.
  • Christof Eb.Christof Eb. Posts: 1,245
    edited 2010-04-06 20:11
    Hi camelot2,
    I am not aware, in which direction you are aiming.
    With 32 bit integer you can do a lot too, if you use some sort of scaling. For example you could decide, that some variables are scaled to 1/100 like percent values.
    After the multiplication of numbers with the same scaling a division with the scale factor has to follow.
    answer:= A * B / scalefactor - of course you have to be aware of the maximum possible numbers.
    answer:= 100 * 100 / 100 for percent values for example.
    Good luck, Christof
  • BeanBean Posts: 8,129
    edited 2010-04-06 21:44
    camelot2,
    It depends where the number come from and where they are going.

    In other words where did the "42.94967296" and the "1_000_000" come from ? And what do you want to do with the result ?

    Assuming the 42.94967296 is some kind of scale, and 1_000_000 is a variable, you could do:

    value = 1_000_000
    answer = value */ 2814750 ' 42.94967296 * 65536 = 2814750

    ' Answer = 42949676 is that close enough ?

    The */ operator works by multiplying the two 32 bit values providing a 64 bit temporary result. The 64 bit temporary result is then shifted right 16 bits (divided by 65536).

    I hope this helps,

    Bean

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Use BASIC on the Propeller with the speed of assembly language.
    PropBASIC thread http://forums.parallax.com/showthread.php?p=867134

    March 2010 Nuts and Volts article·http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp5.pdf
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    There are two rules in life:
    · 1) Never divulge all information
  • camelot2camelot2 Posts: 54
    edited 2010-04-06 22:12
    thanks Mike, Christof and Bean for the reply
    I was hoping someone could lead me to some examples using PropBasic.
    I am trying to setup· COUNTERA to generate different frequencies and have to calculate frqa = freq * (2^32)/clkfreq. My clkfreq is 100MHz and the freq
    I want for example is 1MHz.· 42.94967296 = (2^32)/clkfreq. Now I want
    to multiply 42.94967296 * 1_000_000. Are there any PropBasic examples that
    I could study ?· thanks again
  • BeanBean Posts: 8,129
    edited 2010-04-07 00:14
    Okay, now you have given us the information we need.

    Yeah, just do use the */ like I have done above. Then do "frqa = answer".

    Bean

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Use BASIC on the Propeller with the speed of assembly language.
    PropBASIC thread http://forums.parallax.com/showthread.php?p=867134

    March 2010 Nuts and Volts article·http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp5.pdf
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    There are two rules in life:
    · 1) Never divulge all information
Sign In or Register to comment.