Divide by zero.

Discussion in 'C++' started by shuisheng, Sep 25, 2006.

  1. shuisheng

    shuisheng Guest

    Dear all,

    Assume I have two big arrays A and B. And I want to element-wise divide
    A by B. When an element of B is zero, the results are also zero.

    Such as A = { 2, 4, 0, 6}
    B = { 1, 0, 0, 2}
    -----------------------------------------
    R = { 2, 0, 0, 3}

    Any fast way to avoid the slow 'if' operation.

    Thanks,

    Shuisheng
     
    shuisheng, Sep 25, 2006
    #1
    1. Advertising

  2. shuisheng wrote:
    > Assume I have two big arrays A and B. And I want to element-wise
    > divide A by B. When an element of B is zero, the results are also
    > zero.
    >
    > Such as A = { 2, 4, 0, 6}
    > B = { 1, 0, 0, 2}
    > -----------------------------------------
    > R = { 2, 0, 0, 3}
    >
    > Any fast way to avoid the slow 'if' operation.


    You like to avoid things, don't you? :)

    How slow is it? If you take two arrays such that B doesn't have
    any zeros in it, how much does 'if' slow you down? Is it really
    so significant? AFAIUI, you cannot avoid some kind of checking
    against 0 to prevent division by 0, right? You could try playing
    tricks with arithmetic, but it is most likely just as expensive
    as comparing B with 0.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 25, 2006
    #2
    1. Advertising

  3. shuisheng posted:

    > Dear all,
    >
    > Assume I have two big arrays A and B. And I want to element-wise divide
    > A by B. When an element of B is zero, the results are also zero.
    >
    > Such as A = { 2, 4, 0, 6}
    > B = { 1, 0, 0, 2}
    > -----------------------------------------
    > R = { 2, 0, 0, 3}
    >
    > Any fast way to avoid the slow 'if' operation.



    The fastest code I can think of at the moment is the following (but
    ofcourse, it tests a conditional):

    #include <cstddef>
    #include <cassert>

    void Divide(int *pA, int const *pB, std::size_t const len)
    {
    assert(pA); assert(pB); assert(len);

    int register temp;

    int const *const poverA = pA + len;

    do (temp = *pB++) ? (*pA++ /= temp) : (*pA++ = 0);
    while(poverA!=pA);
    }

    Or maybe if you're working with a machine which has a single instruction
    which takes a pointer plus an offset:

    void Divide(int *const pA, int const *const pB, std::size_t const len)
    {
    assert(pA); assert(pB); assert(len);

    int register temp;

    std::size_t register i = 0;

    do (temp = pB) ? (pA /= temp) : (pA = 0);
    while(++i!= len);
    }

    --

    Frederick Gotham
     
    Frederick Gotham, Sep 25, 2006
    #3
  4. shuisheng

    shuisheng Guest

    Victor Bazarov 写é“:

    > How slow is it? If you take two arrays such that B doesn't have
    > any zeros in it, how much does 'if' slow you down? Is it really
    > so significant? AFAIUI, you cannot avoid some kind of checking
    > against 0 to prevent division by 0, right? You could try playing
    > tricks with arithmetic, but it is most likely just as expensive
    > as comparing B with 0.


    The difference is very small: sevral percents and some times even the
    same. Thanks!!!

    Shuisheng
     
    shuisheng, Sep 25, 2006
    #4
  5. shuisheng wrote:
    > Victor Bazarov ??:
    >
    >> How slow is it? If you take two arrays such that B doesn't have
    >> any zeros in it, how much does 'if' slow you down? Is it really
    >> so significant? AFAIUI, you cannot avoid some kind of checking
    >> against 0 to prevent division by 0, right? You could try playing
    >> tricks with arithmetic, but it is most likely just as expensive
    >> as comparing B with 0.

    >
    > The difference is very small: sevral percents and some times even the
    > same. Thanks!!!


    Hey, don't mention it!

    OK, how much of your application does division of the two arrays take?
    If it's about 10 percent total (from start to finish), then if you
    shave off another 10 percent of that by removing (magically) the 'if',
    what is the final improvement for the application? 1 percent at best?
    Is it worth the time you spend trying to find the magic to remove the
    pesky 'if'?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 25, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zhiqiang Ye
    Replies:
    53
    Views:
    10,273
    Dan Pop
    Jun 28, 2004
  2. Neo

    Divide By Zero

    Neo, Dec 27, 2006, in forum: C++
    Replies:
    5
    Views:
    483
    Glen Dayton
    Dec 27, 2006
  3. Amish Rughoonundon

    divide by zero error from XILINX ISE

    Amish Rughoonundon, Jun 10, 2011, in forum: VHDL
    Replies:
    2
    Views:
    1,223
    Gabor Sz
    Jun 11, 2011
  4. John Kopanas
    Replies:
    5
    Views:
    124
    Rob Biedenharn
    Jan 24, 2007
  5. Griff
    Replies:
    2
    Views:
    110
    Uri Guttman
    Aug 27, 2004
Loading...

Share This Page