Results 1 to 9 of 9

Thread: [RESOLVED] CAUTION: When Multiplying Versus Dividing

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    897

    Resolved [RESOLVED] CAUTION: When Multiplying Versus Dividing

    I was taught (rightly or wrongly) that output is faster if one multiplies versus divides.
    However, take caution, as the result may Not be what you expect. The number of significant digits matter when multiplying.

    Code:
    Private Sub Form_Load()
    
        Dim a As Double
        Dim b As Double
        Dim c As Double
        Dim d As Double
        Dim e As Double
        Dim f As Double
    
        'divide
        a = 5617.41 / 3
    
        'multiply
        b = 5617.41 * 0.33
        c = 5617.41 * 0.333
        d = 5617.41 * 0.3333
        e = 5617.41 * 0.33333
        f = 5617.41 * 0.333333
    
        Debug.Print a, b, c, d, e, f
    
    End Sub
    'Print Result
    'Divide
    a = 1872.47

    'Multiply
    b = 1853.7453
    c = 1870.59753
    d = 1872.282753
    e = 1872.4512753
    f = 1872.46812753

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,318

    Re: CAUTION: When Multiplying Versus Dividing

    haha, well sure it does.

    0.33 is only a very rough approximation of 1#/3#. So, when we multiply by 0.33, we're only doing a rough estimate of what N/3# will be, as shown by your example.

    IEEE Double Precision (aka, VB6 Double) has 15 or 16 base-10 digits of precision. So, if you want to use multiplication to divide by three, you should be using:

    Code:
    0.333333333333333
    ... thereby maxing out your precision.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  3. #3
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,273

    Re: CAUTION: When Multiplying Versus Dividing

    This of course isn't limited to VB6, or to multiplication for that matter.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    897

    Re: CAUTION: When Multiplying Versus Dividing

    Thought it was worth a post, as I just got caught (e.g. post #1) mulitplying using a hand calculator.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,265

    Re: CAUTION: When Multiplying Versus Dividing

    How old are you? Multiplication was (and may still be) faster, especially for integer operations up through the 486. Once floating point units got included on all CPUs, multiplication might still have been a bit faster than division for floating point values, and were a bit faster than division...but it's all irrelevant by now. That level of optimization mostly mattered back in the 90s and earlier. The difference is only a number of clock ticks per instruction. Prior to the advances that showed up with the Pentium, stuff like that could matter if you wanted to squeeze every last cycle out of the CPU. These days, with branch prediction, pre-fetch, multiple pipes, multiple cores, and other hardware improvements, even bothering with a change like this is nuts. I doubt you can even say which is faster anymore. You'd have to understand the context of the exact multiplication and exact division if you want to figure out which individual operation in each individual situation is faster.

    It used to be, "this instruction takes N cycles." Now, it's more a case of, "this instruction can take this range of cycles, including zero, depending on where in the code it is located."
    My usual boring signature: Nothing

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,880

    Re: CAUTION: When Multiplying Versus Dividing

    Shaggy and his blatant ageism. Pay not attention to Shaggy, he is clearly a 20 year old junior dev


    Only joking in case that wasn't obvious.

    Shaggy's right. While technically speaking multiplication is less complex, the amount of gain you'd get (if any) is absolutely trivial.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,273

    Re: CAUTION: When Multiplying Versus Dividing

    Quote Originally Posted by vb6forever View Post
    Thought it was worth a post, as I just got caught (e.g. post #1) mulitplying using a hand calculator.
    It's a good reminder. It's also why there's a concept of significant digits (the bane of high school physics students) when performing calculations involving imprecise measurements.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    897

    Re: CAUTION: When Multiplying Versus Dividing

    Shaggy Hiker:

    How old are you?
    Older than dirt. I started programming in 1969 on a CDC 6400.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,265

    Re: [RESOLVED] CAUTION: When Multiplying Versus Dividing

    Yeah, that makes sense. I didn't start quite that early, as I was only 2, and didn't do any programming until I was perhaps 11 or 12. Still, it does make sense. Your information was valid for decades, but is no longer valid. The timing of multiplication and division is now too complicated a subject for there to be able to state that one is faster than the other. At best, you can say that on average, multiplication will be marginally faster than division, but in the performance of each is so situational that they appear as a normal distribution, where the curves for each closely, though not perfectly, overlap.
    My usual boring signature: Nothing

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