Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. Division Algorithm

Division Algorithm

Scheduled Pinned Locked Moved Algorithms
helpc++graphicsalgorithms
18 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Are Jay

    I'm working on a similar class LongMath. Have you had any luck with division? I've completed add, subtract and multiplication of negative/position whole and non-whole numbers but have been stuck on the division. I can divide negative/positive numbers but only whole numbers and I'd like to compare notes. Interested?

    B Offline
    B Offline
    Bagaturia david
    wrote on last edited by
    #5

    "Interested?" Yes I am. About your problem: Use integer division for your non integer Number. Ex: 121/11 = 11 if 1.21/11 = (121/11)*0.01 or 14/3 = 4+2/3 = 4+(20/3)*0.1 = 4+(6+2/3)*0.1 = 4+(6+(20/3)*0.1)*0.1= =...=4+0.6+0.06+0.006+0.0006+..... it is idea and math.I hope you like this.:) Can you send my division algorithm?

    A U 3 Replies Last reply
    0
    • B Bagaturia david

      "Interested?" Yes I am. About your problem: Use integer division for your non integer Number. Ex: 121/11 = 11 if 1.21/11 = (121/11)*0.01 or 14/3 = 4+2/3 = 4+(20/3)*0.1 = 4+(6+2/3)*0.1 = 4+(6+(20/3)*0.1)*0.1= =...=4+0.6+0.06+0.006+0.0006+..... it is idea and math.I hope you like this.:) Can you send my division algorithm?

      A Offline
      A Offline
      Are Jay
      wrote on last edited by
      #6

      I'll apply the above to my class and I'll email you my source or I'll email you a link to it.
      Class Diagram::LongMath Addition Add(sourceA As String, sourceB As String) As String Subtraction Subtract(sourceA As String, sourceB As String) As String Multiplication Multiply(sourceA As String, sourceB As String) As String Division Divide(divisor As String, dividend As String) As String This may take me a few days with the holidays here and all.

      U 1 Reply Last reply
      0
      • B Bagaturia david

        Hi all. I am David and I am writing "big integer" class. In my class I use "long long Number[_NUMBER]" ( in C++) and I need algorithm of division, it is not problem if this algorithm will use bit set and not "long long" type, I need that it was fast. In other words I need algorithm that divides one vector onto another this vector can be vector of bits ( Ex: [1, 1, 1, 0, 0, 1, 0, 1, 0, 1] ) or vector of number ( Ex: [1258, 16546, 54646846, 54685, 088946, 555468, 8864, 11213, 1] ) Thanks for any help.

        D Offline
        D Offline
        djlove
        wrote on last edited by
        #7

        Hi, I was playing around with a BigInteger class recently and so have come across the same problem. The fastest way I could think of is below, if you say x / y = z (with remainder = r). 1. Find the leading 64 bits of your big number (x) 2. Find the leading 32 bits of your divisor (y). 3. Divide (1) by (2) to give you the leading 32 bits of z. 4. Multiply (3) by y and take this number from x Continually applying steps 1-4 reduces the size of x until you are within +/- 1 of the correct value of z. This is fast because all operations are O(n) or less if you code them properly! Hope this is of some use & good luck! Russ

        U 1 Reply Last reply
        0
        • B Bagaturia david

          "Interested?" Yes I am. About your problem: Use integer division for your non integer Number. Ex: 121/11 = 11 if 1.21/11 = (121/11)*0.01 or 14/3 = 4+2/3 = 4+(20/3)*0.1 = 4+(6+2/3)*0.1 = 4+(6+(20/3)*0.1)*0.1= =...=4+0.6+0.06+0.006+0.0006+..... it is idea and math.I hope you like this.:) Can you send my division algorithm?

          A Offline
          A Offline
          Are Jay
          wrote on last edited by
          #8

          I've posted my source code to my LongMath dll. link to my profile where I've posted it.[^] After re-reading your prior post I'm not sure if this is what your looking for. If not, I'm sorry.:-O

          U 1 Reply Last reply
          0
          • B Bagaturia david

            Hi all. I am David and I am writing "big integer" class. In my class I use "long long Number[_NUMBER]" ( in C++) and I need algorithm of division, it is not problem if this algorithm will use bit set and not "long long" type, I need that it was fast. In other words I need algorithm that divides one vector onto another this vector can be vector of bits ( Ex: [1, 1, 1, 0, 0, 1, 0, 1, 0, 1] ) or vector of number ( Ex: [1258, 16546, 54646846, 54685, 088946, 555468, 8864, 11213, 1] ) Thanks for any help.

            D Offline
            D Offline
            Dan2178
            wrote on last edited by
            #9

            Hi, I can't help specifically with c++ code to do long division .. but I wrote code in vb to do arbitrary precision floating point math. The division method that I used turned out to be suprisingly fast and much easier to implement than addition or subtraction with signed floating point numbers. The method is called the Newton-Raphson method or just Newton's method. I won't go into it here, just search for it, you'll find numerous examples. I don't know how well it would would with integers only since the method uses multiplication, addition and subtraction iteratively but principal is sound anyway. The guts of the divide method I wrote is this: Do While Compare <> Guess Compare = Guess Guess = Left(Multiply(Subtract("2", Multiply(y, Guess)), Guess), Precision) Loop If your multiply, add and subtract methods can handle large numbers (this one uses strings) and you provide code to prevent the length of the result with each iteration from getting too long then sometime like may work. Each iteration will double the accuracy. With another 50 or so lines of code this method can divide numbers thousands of digits long in milliseconds. I also read somewhere where the Newton method is the fastest for divison.

            U 1 Reply Last reply
            0
            • D djlove

              Hi, I was playing around with a BigInteger class recently and so have come across the same problem. The fastest way I could think of is below, if you say x / y = z (with remainder = r). 1. Find the leading 64 bits of your big number (x) 2. Find the leading 32 bits of your divisor (y). 3. Divide (1) by (2) to give you the leading 32 bits of z. 4. Multiply (3) by y and take this number from x Continually applying steps 1-4 reduces the size of x until you are within +/- 1 of the correct value of z. This is fast because all operations are O(n) or less if you code them properly! Hope this is of some use & good luck! Russ

              U Offline
              U Offline
              User 12346520
              wrote on last edited by
              #10

              thanks: https://movied.org

              1 Reply Last reply
              0
              • D Dan2178

                Hi, I can't help specifically with c++ code to do long division .. but I wrote code in vb to do arbitrary precision floating point math. The division method that I used turned out to be suprisingly fast and much easier to implement than addition or subtraction with signed floating point numbers. The method is called the Newton-Raphson method or just Newton's method. I won't go into it here, just search for it, you'll find numerous examples. I don't know how well it would would with integers only since the method uses multiplication, addition and subtraction iteratively but principal is sound anyway. The guts of the divide method I wrote is this: Do While Compare <> Guess Compare = Guess Guess = Left(Multiply(Subtract("2", Multiply(y, Guess)), Guess), Precision) Loop If your multiply, add and subtract methods can handle large numbers (this one uses strings) and you provide code to prevent the length of the result with each iteration from getting too long then sometime like may work. Each iteration will double the accuracy. With another 50 or so lines of code this method can divide numbers thousands of digits long in milliseconds. I also read somewhere where the Newton method is the fastest for divison.

                U Offline
                U Offline
                User 12346520
                wrote on last edited by
                #11

                thanks: https://movied.org

                1 Reply Last reply
                0
                • A Are Jay

                  I'll apply the above to my class and I'll email you my source or I'll email you a link to it.
                  Class Diagram::LongMath Addition Add(sourceA As String, sourceB As String) As String Subtraction Subtract(sourceA As String, sourceB As String) As String Multiplication Multiply(sourceA As String, sourceB As String) As String Division Divide(divisor As String, dividend As String) As String This may take me a few days with the holidays here and all.

                  U Offline
                  U Offline
                  User 12346520
                  wrote on last edited by
                  #12

                  thanks: https://movied.org

                  1 Reply Last reply
                  0
                  • B Bagaturia david

                    "Interested?" Yes I am. About your problem: Use integer division for your non integer Number. Ex: 121/11 = 11 if 1.21/11 = (121/11)*0.01 or 14/3 = 4+2/3 = 4+(20/3)*0.1 = 4+(6+2/3)*0.1 = 4+(6+(20/3)*0.1)*0.1= =...=4+0.6+0.06+0.006+0.0006+..... it is idea and math.I hope you like this.:) Can you send my division algorithm?

                    U Offline
                    U Offline
                    User 12346520
                    wrote on last edited by
                    #13

                    thanks: https://movied.org

                    1 Reply Last reply
                    0
                    • A Are Jay

                      I've posted my source code to my LongMath dll. link to my profile where I've posted it.[^] After re-reading your prior post I'm not sure if this is what your looking for. If not, I'm sorry.:-O

                      U Offline
                      U Offline
                      User 12346520
                      wrote on last edited by
                      #14

                      thanks: https://movied.org

                      1 Reply Last reply
                      0
                      • S Sebastian Schneider

                        I apologize. I missed the fact that you already posted this in the Math-Forum. Thats what you get when you usually only read the VC-forums.

                        U Offline
                        U Offline
                        User 12346520
                        wrote on last edited by
                        #15

                        thanks: https://movied.org

                        1 Reply Last reply
                        0
                        • A Are Jay

                          I'm working on a similar class LongMath. Have you had any luck with division? I've completed add, subtract and multiplication of negative/position whole and non-whole numbers but have been stuck on the division. I can divide negative/positive numbers but only whole numbers and I'd like to compare notes. Interested?

                          U Offline
                          U Offline
                          User 12346520
                          wrote on last edited by
                          #16

                          thanks: https://movied.org

                          1 Reply Last reply
                          0
                          • B Bagaturia david

                            Hi all. I am David and I am writing "big integer" class. In my class I use "long long Number[_NUMBER]" ( in C++) and I need algorithm of division, it is not problem if this algorithm will use bit set and not "long long" type, I need that it was fast. In other words I need algorithm that divides one vector onto another this vector can be vector of bits ( Ex: [1, 1, 1, 0, 0, 1, 0, 1, 0, 1] ) or vector of number ( Ex: [1258, 16546, 54646846, 54685, 088946, 555468, 8864, 11213, 1] ) Thanks for any help.

                            U Offline
                            U Offline
                            User 12346520
                            wrote on last edited by
                            #17

                            thanks: https://movied.org

                            1 Reply Last reply
                            0
                            • S Sebastian Schneider

                              You could check out libGMP (GNU Multiple Precision Library), which is Open Source (zlib-licensed, IIRC) and does the same thing. Alternatively, you could model the thing by re-implementing what you would do when you divide one number by another. Just do some divisions and model those steps. That, however, probably is not the best way to do divisions. You should check the "Math"-Forum for better algorithms.

                              U Offline
                              U Offline
                              User 12346520
                              wrote on last edited by
                              #18

                              thanks: https://movied.org

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups