Shop OBEX P1 Docs P2 Docs Learn Events
Scale? — Parallax Forums

Scale?

MacGeek117MacGeek117 Posts: 747
edited 2007-12-24 21:53 in Propeller 1
How do I scale a number in Spin or Asm? (i.e. */)
RoboGeek

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"I reject your reality and subsitute my own!"

Adam Savage, Mythbusters
www.parallax.com
www.goldmine-elec.com
www.expresspcb.com
www.startrek.com
·

Comments

  • deSilvadeSilva Posts: 2,967
    edited 2007-12-22 20:54
    Can you give an example?
  • Mike GreenMike Green Posts: 23,101
    edited 2007-12-22 20:59
    Since all arithmetic is 32 bit, you mostly do it yourself since you've got lots of significant digits to play with.
    For example, to keep 3 decimal digits, multiply all integers by 1000 and scale multiplication and division appropriately.

    a = 5 * x * x - 3 * y + 2 becomes

    a = 5 * x * x / 1000 - 3 * y + 2000 when a, x and y are in units of 1/1000

    To take the integer part of a, just use a/1000
  • MacGeek117MacGeek117 Posts: 747
    edited 2007-12-22 21:06
    I'm using a byte variable. (Looking to convert an ADC reading to a more usable form, i.e. 0-255 into 0-100.)
    RoboGeek

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "I reject your reality and subsitute my own!"

    Adam Savage, Mythbusters
    www.parallax.com
    www.goldmine-elec.com
    www.expresspcb.com
    www.startrek.com
    ·
  • Mike GreenMike Green Posts: 23,101
    edited 2007-12-22 21:16
    byteVariable = (adcReading * 100) >> 8 ' for a truncated value

    byteVariable = (adcReading * 100 + 128) >> 8 ' for a rounded up value
  • MacGeek117MacGeek117 Posts: 747
    edited 2007-12-22 22:32
    Thanks, Mike! The ADC output is 8 bit, just remove the >>8 part, right?
    RoboGeek

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "I reject your reality and subsitute my own!"

    Adam Savage, Mythbusters
    www.parallax.com
    www.goldmine-elec.com
    www.expresspcb.com
    www.startrek.com
    ·
  • Mike GreenMike Green Posts: 23,101
    edited 2007-12-22 22:43
    Why remove the ">>8"? What I wrote takes a value from 0-255 and transforms it into a value from 0-99.
    If you leave out the ">>8", you get a value from 0 to 25500.
  • MacGeek117MacGeek117 Posts: 747
    edited 2007-12-23 20:29
    Never mind that one, my head was elsewhere. Thanks for your help!
    RoboGeek

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "I reject your reality and subsitute my own!"

    Adam Savage, Mythbusters
    www.parallax.com
    www.goldmine-elec.com
    www.expresspcb.com
    www.startrek.com
    ·
  • Tracy AllenTracy Allen Posts: 6,660
    edited 2007-12-24 20:47
    In Spin, you can also use the ** operator. For example
    adcReading := adcReading * 100/256
    is the same as
    adcReading := adcReading ** 1_677_721_600

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • deSilvadeSilva Posts: 2,967
    edited 2007-12-24 21:53
    Tracy is just kidding smile.gif
    His puzzle becomes more approachable when you rewrite it:
    adcReading **= 16_777_216 * 100
Sign In or Register to comment.