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. C / C++ / MFC
  4. log2

log2

Scheduled Pinned Locked Moved C / C++ / MFC
question
20 Posts 8 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.
  • T toxcct

    just a curiosity and "personal culture" question... what should we need a Base-2 log for ?? very thank you


    TOXCCT >>> GEII power

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #11

    toxctt wrote: what should we need a Base-2 log for ?? Since computers use base-2 numbers, binary logarithms come in very handy. For example, what is the next highest power of two for the number 79?

    int x = 1 + (int) log2(79); // assuming log2() returns a double

    If a binary tree has N nodes, what is the minimum number of levels in the tree? log2(13) + 1 yields the answer of 4. http://www.encyclopedia4u.com/b/binary-logarithm.html


    "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

    T R 2 Replies Last reply
    0
    • D David Crow

      toxctt wrote: what should we need a Base-2 log for ?? Since computers use base-2 numbers, binary logarithms come in very handy. For example, what is the next highest power of two for the number 79?

      int x = 1 + (int) log2(79); // assuming log2() returns a double

      If a binary tree has N nodes, what is the minimum number of levels in the tree? log2(13) + 1 yields the answer of 4. http://www.encyclopedia4u.com/b/binary-logarithm.html


      "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #12

      oh, that's nice ! lol but since it's very useful, how to find such expression ? did you find such by yourself, or with docs ?


      TOXCCT >>> GEII power

      D 1 Reply Last reply
      0
      • T toxcct

        oh, that's nice ! lol but since it's very useful, how to find such expression ? did you find such by yourself, or with docs ?


        TOXCCT >>> GEII power

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #13

        toxcct wrote: oh, that's nice ! lol Are you laughing facetiously? toxcct wrote: how to find such expression ? What expression?


        "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

        T 1 Reply Last reply
        0
        • D David Crow

          toxcct wrote: oh, that's nice ! lol Are you laughing facetiously? toxcct wrote: how to find such expression ? What expression?


          "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #14

          DavidCrow wrote: Are you laughing facetiously? no, i promise you i find all these very nice... DavidCrow wrote: What expression? such expression : int x = 1 + (int) log2(79);


          TOXCCT >>> GEII power

          D 1 Reply Last reply
          0
          • T toxcct

            DavidCrow wrote: Are you laughing facetiously? no, i promise you i find all these very nice... DavidCrow wrote: What expression? such expression : int x = 1 + (int) log2(79);


            TOXCCT >>> GEII power

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #15

            toxcct wrote: such expression : int x = 1 + (int) log2(79); I just made that statement up. A log2() function does not exist in the standard library, so if you needed one, it would have to be created.


            "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

            T 1 Reply Last reply
            0
            • D David Crow

              toxcct wrote: such expression : int x = 1 + (int) log2(79); I just made that statement up. A log2() function does not exist in the standard library, so if you needed one, it would have to be created.


              "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #16

              DavidCrow wrote: log2() function does not exist in the standard library yes, that was implicit and understood. what i want to know is about the entiere expression (int x = 1 + (int) log2(79);) ; did you find it after been documented, or by a thinking of your own ?


              TOXCCT >>> GEII power

              D A 2 Replies Last reply
              0
              • T toxcct

                DavidCrow wrote: log2() function does not exist in the standard library yes, that was implicit and understood. what i want to know is about the entiere expression (int x = 1 + (int) log2(79);) ; did you find it after been documented, or by a thinking of your own ?


                TOXCCT >>> GEII power

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #17

                toxcct wrote: did you find it after been documented, or by a thinking of your own ? The latter. Since log2(79) is 6.3, I needed to take the integer part of that and add one, yielding 7. Thus 27 is 128, the next highest power of 2 for the number 79.


                "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                1 Reply Last reply
                0
                • T toxcct

                  DavidCrow wrote: log2() function does not exist in the standard library yes, that was implicit and understood. what i want to know is about the entiere expression (int x = 1 + (int) log2(79);) ; did you find it after been documented, or by a thinking of your own ?


                  TOXCCT >>> GEII power

                  A Offline
                  A Offline
                  Antti Keskinen
                  wrote on last edited by
                  #18

                  This expression is a solution to the problem DavidCrow presented: "-- What is the next highest power of two for the number 79?" The expression first evaluates the power to which 2 must be raised to reach 79, then adds 1 to this. It all bases on the way logarithms are defined: "Base N logarithm from number Y will result in the power to which N must be raised to result in Y". Most obviously, he just made up a quick problem, and solved it, to give an example. So, he thought it on his own, made it up. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.

                  1 Reply Last reply
                  0
                  • D David Crow

                    toxctt wrote: what should we need a Base-2 log for ?? Since computers use base-2 numbers, binary logarithms come in very handy. For example, what is the next highest power of two for the number 79?

                    int x = 1 + (int) log2(79); // assuming log2() returns a double

                    If a binary tree has N nodes, what is the minimum number of levels in the tree? log2(13) + 1 yields the answer of 4. http://www.encyclopedia4u.com/b/binary-logarithm.html


                    "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                    R Offline
                    R Offline
                    Robert A T Kaldy
                    wrote on last edited by
                    #19

                    Aaaarrrrgggghhh!! you really want to compute binary logarithm of integers with float logarithm function? I think that integer approach would be much much more quicker:

                    int log2(int x)
                    {
                    int lg;
                    // if (x <= 0) throw something...
                    for(lg = -1; x != 0; x /= 2, lg++);
                    return lg;
                    }

                    Robert-Antonio "Science is a differerntial equation. Religion is a boundary condition."

                    D 1 Reply Last reply
                    0
                    • R Robert A T Kaldy

                      Aaaarrrrgggghhh!! you really want to compute binary logarithm of integers with float logarithm function? I think that integer approach would be much much more quicker:

                      int log2(int x)
                      {
                      int lg;
                      // if (x <= 0) throw something...
                      for(lg = -1; x != 0; x /= 2, lg++);
                      return lg;
                      }

                      Robert-Antonio "Science is a differerntial equation. Religion is a boundary condition."

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #20

                      Robert A. T. Káldy wrote: ...with float logarithm function The log2() function I demonstrated was theoretical. Were it to be implemented, ints would surely be used over doubles. Robert A. T. Káldy wrote: I think... But without actual metrics, we don't really know.


                      "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                      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