log2
-
just a curiosity and "personal culture" question... what should we need a Base-2 log for ?? very thank you
TOXCCT >>> GEII power
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)
-
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)
-
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
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)
-
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)
-
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
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)
-
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)
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
-
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
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)
-
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
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.
-
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)
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."
-
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."
Robert A. T. Káldy wrote: ...with float logarithm function The
log2()
function I demonstrated was theoretical. Were it to be implemented,int
s would surely be used overdouble
s. 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)