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 The Lady of Shallots

    Hello everyone, I was wondering if there was a way to calculate the log2 of a number. I know there are functions for calculating the ln, log and log10, but don't see one for log2...maybe I'm missing it or there's a way to calc it using one of the other functions? Thanks! "It seems that we're getting to the bottom of the barrel of squirrels." - my boss

    N Offline
    N Offline
    Navin
    wrote on last edited by
    #3

    Use the Magic Log Forumla: Logb x = Loga x/Loga b A funcion that can get an arbitrary log:

    double MyLog(double base, double x)
    {
      return log10(x) / log10(base);
    }
    
    ...
    double a = MyLog(2, 16);      // should be 4
    

    Sometimes I feel like I'm a USB printer in a parallel universe.

    1 Reply Last reply
    0
    • M Mike Dimmick

      Logarithms in different bases can be done using the formula logy x = logz x / logz y So you can use the log or log10 function to produce an approximate log2 function:

      double log2(double val)
      {
      return log(val) / log(2.0);
      }

      Stability. What an interesting concept. -- Chris Maunder

      N Offline
      N Offline
      Navin
      wrote on last edited by
      #4

      Argh, beat me to it! You must have posted that while I was in the process of replying. :-D Sometimes I feel like I'm a USB printer in a parallel universe.

      1 Reply Last reply
      0
      • M Mike Dimmick

        Logarithms in different bases can be done using the formula logy x = logz x / logz y So you can use the log or log10 function to produce an approximate log2 function:

        double log2(double val)
        {
        return log(val) / log(2.0);
        }

        Stability. What an interesting concept. -- Chris Maunder

        T Offline
        T Offline
        The Lady of Shallots
        wrote on last edited by
        #5

        Perfect! Thanks so much! "It seems that we're getting to the bottom of the barrel of squirrels." - my boss

        1 Reply Last reply
        0
        • T The Lady of Shallots

          Hello everyone, I was wondering if there was a way to calculate the log2 of a number. I know there are functions for calculating the ln, log and log10, but don't see one for log2...maybe I'm missing it or there's a way to calc it using one of the other functions? Thanks! "It seems that we're getting to the bottom of the barrel of squirrels." - my boss

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

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


          TOXCCT >>> GEII power

          J D 2 Replies Last reply
          0
          • T toxcct

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


            TOXCCT >>> GEII power

            J Offline
            J Offline
            jmkhael
            wrote on last edited by
            #7

            A whole world of it: Check on google log neperien In breif: Solving mathematical equations Mathematical modeling of the movement of protons Spatial Interpolation, Mechanical modeling Probability and statistics .... Papa while (TRUE) Papa.WillLove ( Bebe ) ;

            T 1 Reply Last reply
            0
            • J jmkhael

              A whole world of it: Check on google log neperien In breif: Solving mathematical equations Mathematical modeling of the movement of protons Spatial Interpolation, Mechanical modeling Probability and statistics .... Papa while (TRUE) Papa.WillLove ( Bebe ) ;

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

              wow woww stop it man, did you read my post ??? i didn't ask for log neperien (which is a Base-e log), but the need for having a log2 one !!!!


              TOXCCT >>> GEII power

              J 1 Reply Last reply
              0
              • T toxcct

                wow woww stop it man, did you read my post ??? i didn't ask for log neperien (which is a Base-e log), but the need for having a log2 one !!!!


                TOXCCT >>> GEII power

                J Offline
                J Offline
                jmkhael
                wrote on last edited by
                #9

                Sorry for missreading your post log2 is used in music among others for as octave utility and for floating point arithmetics: "By knowing that the exponent and mantissa are both two's compliment values and also knowing that a floating point number is a very close approximation of the same number expressed in log2 form, a quick inversions and square roots can be implimented with simple functions. For example a quick inverse is calculated by negating the exponent and mantissa bits." Papa while (TRUE) Papa.WillLove ( Bebe ) ;

                T 1 Reply Last reply
                0
                • J jmkhael

                  Sorry for missreading your post log2 is used in music among others for as octave utility and for floating point arithmetics: "By knowing that the exponent and mantissa are both two's compliment values and also knowing that a floating point number is a very close approximation of the same number expressed in log2 form, a quick inversions and square roots can be implimented with simple functions. For example a quick inverse is calculated by negating the exponent and mantissa bits." Papa while (TRUE) Papa.WillLove ( Bebe ) ;

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

                  powerful ! (paweurfoul :-D:):cool::doh: thnk u vry much


                  TOXCCT >>> GEII power

                  1 Reply Last reply
                  0
                  • 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