Shop OBEX P1 Docs P2 Docs Learn Events
A little help with negative numbers please..? — Parallax Forums

A little help with negative numbers please..?

HughHugh Posts: 362
edited 2010-08-09 10:51 in Propeller 1
Hi,

I am trying to get my brain away integer maths with negative numbers...
[B]VAR[/B]
[B]long [/B]v, maxVal, minVal
[B]long [/B]q

[B]PUB [/B]Main
minVal [COLOR=DarkGreen]:=[/COLOR] [COLOR=Blue]0[/COLOR]
maxVal [COLOR=DarkGreen]:=[/COLOR] [COLOR=Blue]100[/COLOR]
v [COLOR=DarkGreen]:=[/COLOR] [COLOR=Blue]40[/COLOR]
callDoStuff

[B]PUB [/B]callDoStuff


q [COLOR=DarkGreen]:=[/COLOR] (v [COLOR=DarkGreen]-[/COLOR] minVal) [COLOR=DarkGreen]/[/COLOR] (maxVal - minVal) * [COLOR=Blue]270[/COLOR]
In other words, getting a value proportionate to the value v over a specified range as a proportion of 270. i.e., I am looking for the result from v= -40 to be -(the result of v=+40).

This works correctly when V >= 0 but not when v < 0 (or for that matter when minVal < 0).

Am I missing something, or do I have to define somewhere that the longs are ± numbers, not positive integers?

I don't have the spare cogs/RAM for any maths functions!

Comments

  • ErNaErNa Posts: 1,796
    edited 2010-08-08 05:32
    I would alway write
    q = (270 * ( a-b)) / (c-b)
    

    but that will not bring the expected result, as I fear.

    What should be the output? A value from 0 to 270 for a number between min and max val, independ of sign?
    q = (sign(v) * 270 * (abs(v) - minval) ) / (maxval - minval)
    

    ??
  • Duane DegnDuane Degn Posts: 10,588
    edited 2010-08-08 06:23
    Multiply by 270 first.

    I don't think this is a negative number problem. Try to keep you divisions as a last step.

    40/100 = 0 in Spin.

    Use
    q := ((v - minVal) * 270) / ((maxVal - minVal) * 270)

    You could also multiply all your numbers by 1000 and then divide your final answer by 1000.

    Look for pseudoreal numbers in Labs.

    Duane
  • HughHugh Posts: 362
    edited 2010-08-09 10:51
    I'm still struggling, but I think it is because I am using the sin / cos of the result, which complicates things further.

    Thanks for the [excuse the pun] pointers - I'll keep on trying. ;-)
Sign In or Register to comment.