square root efficiency


    Mar 20 2007 | 12:06 am
    hi,
    i am calculating the distance between two points in 3d space using this formula:
    Distance = √((distanceX*distanceX) + (distanceY*distanceY) + (distanceZ*distanceZ))
    i have read (elsewhere) that taking a square root can use a lot of cpu power, and as this calculation is being done repeatedly i want to make it as efficient as possible, and perhaps explore alternative formula for this calculation.
    so my questions are:
    1. is taking a square root a particularly cpu intensive calculation in maxmsp? (or is this hardware dependent?)
    and
    2. is writing a javascript function to do the calculation going to add or detract from efficiency?
    thanks andrew

    • Mar 20 2007 | 12:16 am
      If you don't need actual distance but just some distance metric, you may nod need to take the square root but can just use the square of distance. Otherwise, if you're doing alot of sqrt calls, perhaps you can use jit.matrix.
      wes
      On 3/19/07, Andrew wrote: > > hi, > > i am calculating the distance between two points in 3d space using this formula: > > Distance = √((distanceX*distanceX) + (distanceY*distanceY) + (distanceZ*distanceZ)) > > i have read (elsewhere) that taking a square root can use a lot of cpu power, and as this calculation is being done repeatedly i want to make it as efficient as possible, and perhaps explore alternative formula for this calculation. > > so my questions are: > > 1. is taking a square root a particularly cpu intensive calculation in maxmsp? (or is this hardware dependent?) > > and > > 2. is writing a javascript function to do the calculation going to add or detract from efficiency? > > thanks > andrew >
    • Mar 20 2007 | 1:44 am
      thanks wes,
      you are so right, all i need is the relative difference in a position so the actual distance is not really needed, just a number relative to this distance that can be compared to another on the same scale. so yes, i don't need to take the square root after all.
      thanks for your insight. andrew