Shop OBEX P1 Docs P2 Docs Learn Events
StarSlash in Propeller — Parallax Forums

StarSlash in Propeller

NewzedNewzed Posts: 2,503
edited 2006-10-26 11:56 in Propeller 1
As far as I know, there is no equivalent of the PBasic·' */· '·operator in Spin.· If there is, this is wasted effort.

Here is a method that lets you multiply a number by a decimal:

PUB starslash(n,i,f)····
·· z := n*i + ((n*f)/1000)

"n" is your number, "i" is the integer portion of the multiplier and "f" is the decimal portion of the multiplier without the decimal point.· "z" is a LONG global variable.

If you want to multiply 77 by 3.375 you would call:

··· starslash(77,3,375)·· ' 0.3 percent error

If you want to multiply 77 by 0.375 you would call:

··· starslash(77,0,375)·· '· 3 percent error

The error percentage decreases sharply with larger results for "z".· For example:

··· starslash(475,3 375)

has an error of .007 percent.

Hope this helps someone.

Sid

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
Don't have VGA?

Newzed@aol.com
·

Comments

  • Tracy AllenTracy Allen Posts: 6,656
    edited 2006-10-22 17:17
    However, there is the ** operator, which returns the upper 32 bits of a 32x32 multiply. That makes a very accurate fractional multiplaction, a 32 bit binary fraction.

    f := 1610612736 ' 0.375 = 1610612736/(2^32), the 32 bit binary fraction representing 0.375. Very accurate
    ' ....
    PUB star4star(n,i,f) ' multiplicand, integer part of multiplier, fractional part of multiplier.
    z := (n * i) + (n ** f)


    You can make your method more accurate by including more decimal digits in the multiplier, e.g. f/100000 = 37567/100000, so long as n*f does not overflow 32 bits. Or use binary long division to approximate the fraction by a ** multiplier, e.g. 37567 / 100000 = 1613490364/(2^32).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Dennis FerronDennis Ferron Posts: 480
    edited 2006-10-26 03:55
    Newzed, when I read your post title, I thought you were talking about a video game!

    Would you mind if I use the name "StarSlash" for a real Propeller video game?
  • NewzedNewzed Posts: 2,503
    edited 2006-10-26 11:56
    Be my guest, Dennis.

    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Don't have VGA?

    Newzed@aol.com
    ·
Sign In or Register to comment.