Results 1 to 11 of 11

Thread: Divide and multiply

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    15

    Divide and multiply

    What is the correct method of dividing and then multiplying in a statement? The following is what I've used but doesn't work.

    SheetsRequired = Pages / 4 * 2

  2. #2

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    15

    Re: Divide and multiply

    Ok the following worked

    SheetsRequired = (Pages / 4) * 2

  3. #3
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,451

    Re: Divide and multiply

    That is the correct method. Are you sure you are using the forward slash in your actual code? If you are using the backslash instead (integer divide), then you will get a different result (because the multiplication will take place before the integer division).

    If you are using the exact statement as written above, maybe you could clarify what "doesn't work" means? What value is in Pages, and what result are you getting in SheetsRequired vs. what you are expecting?

    Lastly, what are the variable types of Pages and SheetsRequired?

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Divide and multiply


  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Divide and multiply

    Sadly MSDN Online seems to have removed the correct VB6 article on operator precedence.

    But instead of relying on the VFred link given above you should look up the topic in your MSDN Library CDs you should have installed locally. Ideally you have the October 2001 release installed, but even something a little earlier would be good.

    The information will be more accurate and far less misleading.

  6. #6
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,451

    Re: Divide and multiply

    Maybe my coffee hasn't kicked in yet, but I can't think of a situation where X/4*2 would not equal (X/4)*2. Can anyone clue me in?

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Divide and multiply

    There should be no difference in the result from post 1 and post 2 All division and multiplication are preformed in the order it appears from left to right so x/y*z is the same as (x/y)*z

    Of course given your code in the OP there is no need for both. x/4*2 = x/2

    so
    Code:
    SheetsRequired = Pages / 2
    Would yield the same result

  8. #8
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Divide and multiply

    Quote Originally Posted by dilettante View Post
    Sadly MSDN Online seems to have removed the correct VB6 article on operator precedence.
    Well, maybe the "VFred" link is not completely understandable (as it doesn't fully 'explain' the precedence order of the combinations shown), but it makes sense to me. But, I suppose the best advice IS to use the resource you suggested.

    Anyway,from that link-Order of Precedence:

    Attachment 125739

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Divide and multiply

    Yepper, that integer division one has bitten me in the past...

    (6 / 2 * 3) <> (6 \ 2 * 3)
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Divide and multiply

    There is also the range issue, which is why Kernel32 exports the MulDiv() entrypoint.

  11. #11
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Divide and multiply

    Quote Originally Posted by dilettante View Post
    Sadly MSDN Online seems to have removed the correct VB6 article on operator precedence.
    The VBA version (which is exactly the same as the VB6 version) of the Operator Precedence article is still available though.

    Quote Originally Posted by DataMiser View Post
    Of course given your code in the OP there is no need for both. x/4*2 = x/2

    so
    Code:
    SheetsRequired = Pages / 2
    Would yield the same result
    Another equivalent expression is:

    Code:
    SheetsRequired = Pages * 0.5
    Supposedly, multiplication is a lot faster than division, but I don't know if that's still true with the latest CPUs.

    Quote Originally Posted by dilettante View Post
    There is also the range issue, ...
    That doesn't appear to be a problem since integral operands will be first converted to Double before being divided.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width