Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

In thisthis post, the user offers a more than satisfactory review, but he's also made a few mistakes. The user claims:

log(2.0) is 0.3 so dividing by that is equivalent to multiplying by 3.3. That seems a bit large

That statement is wrong, in C/C++ log(x) is the natural logarithm log_e (also known as ln). The result of ln(2.0) is 0.69, not 0.3. The user was under the false assumption that log(x) was log10(x). I'm willing to bet this is because he used the log function on his calculator, not ln and forgot that the defaults for a calculator are not the same as C/C++.

The asker's code is actually correct, although, it does do more processing than it really needs to. Let's try calculating it by hand:

Given

size is 10

Log = ceil(log((double) size) / log(2.0)

is equivalent to

Log = ceil(2.302 / 0.693)

is equivalent to

Log = ceil(3.321)

Thusly

Log == 4

_capacity is then set to 1 << 4 which means capacity is 16. 

As you can see, the asker's code correctly calculates size * golden ratio. Now, if he wanted to optimise the code, and thusly making it faster, he could simply do the following:

/* Define in a header somewhere */
#define GOLDEN_RATIO 1.61803398875

_capacity = (double)size * GOLDEN_RATIO;

I wanted to simply edit the user's answer and tell the asker that his only problem is that he's unnecessarily recalculating the golden ratio each call instead of statically defining it. If someone could edit the answer to prevent misinforming people who stumbled across it, I'd greatly appreciate it.

In this post, the user offers a more than satisfactory review, but he's also made a few mistakes. The user claims:

log(2.0) is 0.3 so dividing by that is equivalent to multiplying by 3.3. That seems a bit large

That statement is wrong, in C/C++ log(x) is the natural logarithm log_e (also known as ln). The result of ln(2.0) is 0.69, not 0.3. The user was under the false assumption that log(x) was log10(x). I'm willing to bet this is because he used the log function on his calculator, not ln and forgot that the defaults for a calculator are not the same as C/C++.

The asker's code is actually correct, although, it does do more processing than it really needs to. Let's try calculating it by hand:

Given

size is 10

Log = ceil(log((double) size) / log(2.0)

is equivalent to

Log = ceil(2.302 / 0.693)

is equivalent to

Log = ceil(3.321)

Thusly

Log == 4

_capacity is then set to 1 << 4 which means capacity is 16. 

As you can see, the asker's code correctly calculates size * golden ratio. Now, if he wanted to optimise the code, and thusly making it faster, he could simply do the following:

/* Define in a header somewhere */
#define GOLDEN_RATIO 1.61803398875

_capacity = (double)size * GOLDEN_RATIO;

I wanted to simply edit the user's answer and tell the asker that his only problem is that he's unnecessarily recalculating the golden ratio each call instead of statically defining it. If someone could edit the answer to prevent misinforming people who stumbled across it, I'd greatly appreciate it.

In this post, the user offers a more than satisfactory review, but he's also made a few mistakes. The user claims:

log(2.0) is 0.3 so dividing by that is equivalent to multiplying by 3.3. That seems a bit large

That statement is wrong, in C/C++ log(x) is the natural logarithm log_e (also known as ln). The result of ln(2.0) is 0.69, not 0.3. The user was under the false assumption that log(x) was log10(x). I'm willing to bet this is because he used the log function on his calculator, not ln and forgot that the defaults for a calculator are not the same as C/C++.

The asker's code is actually correct, although, it does do more processing than it really needs to. Let's try calculating it by hand:

Given

size is 10

Log = ceil(log((double) size) / log(2.0)

is equivalent to

Log = ceil(2.302 / 0.693)

is equivalent to

Log = ceil(3.321)

Thusly

Log == 4

_capacity is then set to 1 << 4 which means capacity is 16. 

As you can see, the asker's code correctly calculates size * golden ratio. Now, if he wanted to optimise the code, and thusly making it faster, he could simply do the following:

/* Define in a header somewhere */
#define GOLDEN_RATIO 1.61803398875

_capacity = (double)size * GOLDEN_RATIO;

I wanted to simply edit the user's answer and tell the asker that his only problem is that he's unnecessarily recalculating the golden ratio each call instead of statically defining it. If someone could edit the answer to prevent misinforming people who stumbled across it, I'd greatly appreciate it.

added 1 character in body; edited tags; edited title
Source Link
Jamal Mod
  • 35.2k
  • 2
  • 54
  • 106

Misinformation In An Answerin an answer

In this post, the user offers a more than satisfactory review, but he's also made a few mistakes. The user claims: "log(2.0) is 0.3 so dividing by that is equivalent to multiplying by 3.3. That seems a bit large."

log(2.0) is 0.3 so dividing by that is equivalent to multiplying by 3.3. That seems a bit large

That statement is wrong, in C/C++ log(x) is the natural logarithm log_e (also known as ln). The result of ln(2.0) is 0.69, not 0.3. The user was under the false assumption that log(x) was log10(x). I'm willing to bet this is because he used the log function on his calculator, not ln and forgot that the defaults for a calculator are not the same as C/C++.

The Asker'sasker's code is actually correct, although, it does do more processing than it really needs to. Let's try calculating it by hand:

Given

  • size is 10

Log = ceil(log((double) size) / log(2.0) size is 10

Log = ceil(log((double) size) / log(2.0)

is equivalent to

Log = ceil(2.302 / 0.693)

Log = ceil(2.302 / 0.693)

is equivalent to

Log = ceil(3.321).

Log = ceil(3.321)

Thusly

Log == 4

_capacity is then set to 1 << 4 which means capacity is 16.

Log == 4

_capacity is then set to 1 << 4 which means capacity is 16. 

As you can see, the Asker'sasker's code correctly calculates size * golden ratio. Now, if he wanted to optimise the code, and thusly making it faster, he could simply do the following:

/* Define in a header somewhere */
#define GOLDEN_RATIO 1.61803398875

_capacity = (double)size * GOLDEN_RATIO;

I wanted to simply edit the User'suser's answer and tell the Askerasker that his only problem is that he's unnecessarily recalculating the golden ratio each call instead of statically defining it. If someone could edit the answer to prevent misinforming people who stumbled across it, I'd greatly appreciate it.

Misinformation In An Answer

In this post, the user offers a more than satisfactory review, but he's also made a few mistakes. The user claims: "log(2.0) is 0.3 so dividing by that is equivalent to multiplying by 3.3. That seems a bit large." That statement is wrong, in C/C++ log(x) is the natural logarithm log_e (also known as ln). The result of ln(2.0) is 0.69, not 0.3. The user was under the false assumption that log(x) was log10(x). I'm willing to bet this is because he used the log function on his calculator, not ln and forgot that the defaults for a calculator are not the same as C/C++.

The Asker's code is actually correct, although, it does do more processing than it really needs to. Let's try calculating it by hand:

Given

  • size is 10

Log = ceil(log((double) size) / log(2.0)

is equivalent to

Log = ceil(2.302 / 0.693)

is equivalent to

Log = ceil(3.321).

Thusly

Log == 4

_capacity is then set to 1 << 4 which means capacity is 16.

As you can see, the Asker's code correctly calculates size * golden ratio. Now, if he wanted to optimise the code, and thusly making it faster, he could simply do the following:

/* Define in a header somewhere */
#define GOLDEN_RATIO 1.61803398875

_capacity = (double)size * GOLDEN_RATIO;

I wanted to simply edit the User's answer and tell the Asker that his only problem is that he's unnecessarily recalculating the golden ratio each call instead of statically defining it. If someone could edit the answer to prevent misinforming people who stumbled across it, I'd greatly appreciate it.

Misinformation in an answer

In this post, the user offers a more than satisfactory review, but he's also made a few mistakes. The user claims:

log(2.0) is 0.3 so dividing by that is equivalent to multiplying by 3.3. That seems a bit large

That statement is wrong, in C/C++ log(x) is the natural logarithm log_e (also known as ln). The result of ln(2.0) is 0.69, not 0.3. The user was under the false assumption that log(x) was log10(x). I'm willing to bet this is because he used the log function on his calculator, not ln and forgot that the defaults for a calculator are not the same as C/C++.

The asker's code is actually correct, although, it does do more processing than it really needs to. Let's try calculating it by hand:

Given

size is 10

Log = ceil(log((double) size) / log(2.0)

is equivalent to

Log = ceil(2.302 / 0.693)

is equivalent to

Log = ceil(3.321)

Thusly

Log == 4

_capacity is then set to 1 << 4 which means capacity is 16. 

As you can see, the asker's code correctly calculates size * golden ratio. Now, if he wanted to optimise the code, and thusly making it faster, he could simply do the following:

/* Define in a header somewhere */
#define GOLDEN_RATIO 1.61803398875

_capacity = (double)size * GOLDEN_RATIO;

I wanted to simply edit the user's answer and tell the asker that his only problem is that he's unnecessarily recalculating the golden ratio each call instead of statically defining it. If someone could edit the answer to prevent misinforming people who stumbled across it, I'd greatly appreciate it.

Source Link

Misinformation In An Answer

In this post, the user offers a more than satisfactory review, but he's also made a few mistakes. The user claims: "log(2.0) is 0.3 so dividing by that is equivalent to multiplying by 3.3. That seems a bit large." That statement is wrong, in C/C++ log(x) is the natural logarithm log_e (also known as ln). The result of ln(2.0) is 0.69, not 0.3. The user was under the false assumption that log(x) was log10(x). I'm willing to bet this is because he used the log function on his calculator, not ln and forgot that the defaults for a calculator are not the same as C/C++.

The Asker's code is actually correct, although, it does do more processing than it really needs to. Let's try calculating it by hand:

Given

  • size is 10

Log = ceil(log((double) size) / log(2.0)

is equivalent to

Log = ceil(2.302 / 0.693)

is equivalent to

Log = ceil(3.321).

Thusly

Log == 4

_capacity is then set to 1 << 4 which means capacity is 16.

As you can see, the Asker's code correctly calculates size * golden ratio. Now, if he wanted to optimise the code, and thusly making it faster, he could simply do the following:

/* Define in a header somewhere */
#define GOLDEN_RATIO 1.61803398875

_capacity = (double)size * GOLDEN_RATIO;

I wanted to simply edit the User's answer and tell the Asker that his only problem is that he's unnecessarily recalculating the golden ratio each call instead of statically defining it. If someone could edit the answer to prevent misinforming people who stumbled across it, I'd greatly appreciate it.