Shop OBEX P1 Docs P2 Docs Learn Events
reciprocal calculation — Parallax Forums

reciprocal calculation

ArchiverArchiver Posts: 46,084
edited 2004-07-08 17:02 in General Discussion
Hi,
I need to calculate the following:
Given S and T
0<=S<=16
1<=T<57000
calculate
(T*10) / ((1<<S)*2.54) which is always >1 and must be rescaled to 65536/F

Question: how do I calculate F?

65536/F can be written as I + (R/65536)
Question: how do I calculate I and R from F

All used vars are 16bit unsigned.
At Tracy Allen's site I found calculating a reciprocal value using 24bit
(shortcut #3)
http://www.emesystems.com/BS2math2.htm#Reciprocal
There is a input contraint that F must be >= 256.
In my case F is frequently <256. How to workaround that?

regards peter

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2004-07-08 17:02
    Hi Peter,

    Use binary long division per this URL
    http://www.emesystems.com/BS2math2.htm#longdivision


    The integer part comes directly from your calculation:
    D = ((1<<S)*2.54)
    I = (T*10) / D
    and the fraction numerator N from,
    N = (T*10) // D ' note //
    then apply the long division loop,
    '
    binary division loop
    for J=15 to 0 ' 16 bits
    N=N//D<<1 ' remainder*2
    R.bit0(J)=N/D ' next bit
    next
    '
    The result is the value R you want, with the 65536 implied.

    If you need to re-scale R/65536 to be a decimal fraction (say for
    display), then use,
    R = R ** 10000 ' 0.0000 to 0.9999


    -- Tracy


    >Hi,
    >I need to calculate the following:
    >Given S and T
    >0<=S<=16
    >1<=T<57000
    >calculate
    >(T*10) / ((1<<S)*2.54) which is always >1 and must be rescaled to 65536/F
    >
    >Question: how do I calculate F?
    >
    >65536/F can be written as I + (R/65536)
    >Question: how do I calculate I and R from F
    >
    >All used vars are 16bit unsigned.
    >At Tracy Allen's site I found calculating a reciprocal value using 24bit
    >(shortcut #3)
    >http://www.emesystems.com/BS2math2.htm#Reciprocal
    >There is a input contraint that F must be >= 256.
    >In my case F is frequently <256. How to workaround that?
    >
    >regards peter
Sign In or Register to comment.