Shop OBEX P1 Docs P2 Docs Learn Events
Need some help on some math issues in spin — Parallax Forums

Need some help on some math issues in spin

radialrandyradialrandy Posts: 78
edited 2011-01-14 20:38 in Propeller 1
rpm1 := ((1_200_000_000/(b-a))* scale)*2 This is giving me results that Im not happy with. this calculation gives me "rpm1" results that incriments of 8 in decimal. example 5060, 5068....even though the "b-a" gives big numbers. I attached a snapshot of my debug terminal results so u can see the varying b-a numbers and the rpm1 numbers. I would think the calculation should be accurate enough to give me results to the 1 decimal. example 5060,5061,5062..... What am i missing here?
thanks
208 x 602 - 156K

Comments

  • AndreLAndreL Posts: 1,004
    edited 2011-01-14 20:31
    Without checking your math, remember all calcs are done in 32-bit integers not fractions or decimals, so just run thru some examples by hand and check these 4 cases, 0, 1, mid range, full range. Do them by hand with integer math in the order of operations of your expression.

    For example, right off the bat, you are multiplying your results by 2, so no matter what you are always going to get something that is a multiple of 2, same goes for scale, so the idea here is to make sure that you pre-divide anything out yourself, so that the results are to the 1's place.

    Another idea if you want more accuracy is to use fixed point math or the floating point library.

    But, this is a simple matter of you are scaling your results after the fact that's why you have this *8 in there, anything *8 is *8. There are no fractions or decimals here, so that's what is causing the issue.

    Andre'
  • JasonDorieJasonDorie Posts: 1,930
    edited 2011-01-14 20:38
    You're multiplying by scale, then 2, after doing the divide. I'm guessing scale is 4?

    Since you're using integer math, doing the scale up at the end is losing intermediate precision.

    Try scaling (b-a) down instead of scaling the result back up, like this:

    rpm1 := 1_200_000_000 / ((b-a) / (scale*2))
Sign In or Register to comment.