Unary operator in C or C++ for the second power of a number
Given a double x
, it is known that it is more efficient to use x*x
instead of pow(x,2)
. Imagine for simplicity that we have to calculate the square root of x
: as it is a unary operation, for this purpose we have sqrt(x)
. Now, also raising x
to the second power is a unary operation, but we have nothing like (as far as I know) pow2(x)
.
I implemented my own pow2
as:
inline double pow2(double a){return a*a;}
which should be still better than pow(a,2)
, but it is based on the *
operator that is not unary.
How to implement a genuine unary implementation of pow2
? Would it be the most efficient way to obtain the second power of a double
?
NOTE: I am aware of the fact that every real power of a positive real is an unary operation and that it is nonsense to define an infinite number of pow2
, pow3
, pow3.14
... from the practical point of view I'm very happy with pow(double, double)
.
c++ c double cmath unary-function
add a comment |
Given a double x
, it is known that it is more efficient to use x*x
instead of pow(x,2)
. Imagine for simplicity that we have to calculate the square root of x
: as it is a unary operation, for this purpose we have sqrt(x)
. Now, also raising x
to the second power is a unary operation, but we have nothing like (as far as I know) pow2(x)
.
I implemented my own pow2
as:
inline double pow2(double a){return a*a;}
which should be still better than pow(a,2)
, but it is based on the *
operator that is not unary.
How to implement a genuine unary implementation of pow2
? Would it be the most efficient way to obtain the second power of a double
?
NOTE: I am aware of the fact that every real power of a positive real is an unary operation and that it is nonsense to define an infinite number of pow2
, pow3
, pow3.14
... from the practical point of view I'm very happy with pow(double, double)
.
c++ c double cmath unary-function
1
Are you sure about the efficiency claim?
– Shafik Yaghmour
Nov 15 '18 at 18:31
godbolt.org/z/gmj1nn Not really surprising.
– Eugene Sh.
Nov 15 '18 at 18:40
For example here (about the efficiency claim): stackoverflow.com/questions/6321170/…
– mrc ntn
Nov 15 '18 at 19:05
Visual studio, on the other hand, godbolt.org/z/U95PKq
– user4581301
Nov 15 '18 at 19:07
add a comment |
Given a double x
, it is known that it is more efficient to use x*x
instead of pow(x,2)
. Imagine for simplicity that we have to calculate the square root of x
: as it is a unary operation, for this purpose we have sqrt(x)
. Now, also raising x
to the second power is a unary operation, but we have nothing like (as far as I know) pow2(x)
.
I implemented my own pow2
as:
inline double pow2(double a){return a*a;}
which should be still better than pow(a,2)
, but it is based on the *
operator that is not unary.
How to implement a genuine unary implementation of pow2
? Would it be the most efficient way to obtain the second power of a double
?
NOTE: I am aware of the fact that every real power of a positive real is an unary operation and that it is nonsense to define an infinite number of pow2
, pow3
, pow3.14
... from the practical point of view I'm very happy with pow(double, double)
.
c++ c double cmath unary-function
Given a double x
, it is known that it is more efficient to use x*x
instead of pow(x,2)
. Imagine for simplicity that we have to calculate the square root of x
: as it is a unary operation, for this purpose we have sqrt(x)
. Now, also raising x
to the second power is a unary operation, but we have nothing like (as far as I know) pow2(x)
.
I implemented my own pow2
as:
inline double pow2(double a){return a*a;}
which should be still better than pow(a,2)
, but it is based on the *
operator that is not unary.
How to implement a genuine unary implementation of pow2
? Would it be the most efficient way to obtain the second power of a double
?
NOTE: I am aware of the fact that every real power of a positive real is an unary operation and that it is nonsense to define an infinite number of pow2
, pow3
, pow3.14
... from the practical point of view I'm very happy with pow(double, double)
.
c++ c double cmath unary-function
c++ c double cmath unary-function
asked Nov 15 '18 at 18:30
mrc ntnmrc ntn
1
1
1
Are you sure about the efficiency claim?
– Shafik Yaghmour
Nov 15 '18 at 18:31
godbolt.org/z/gmj1nn Not really surprising.
– Eugene Sh.
Nov 15 '18 at 18:40
For example here (about the efficiency claim): stackoverflow.com/questions/6321170/…
– mrc ntn
Nov 15 '18 at 19:05
Visual studio, on the other hand, godbolt.org/z/U95PKq
– user4581301
Nov 15 '18 at 19:07
add a comment |
1
Are you sure about the efficiency claim?
– Shafik Yaghmour
Nov 15 '18 at 18:31
godbolt.org/z/gmj1nn Not really surprising.
– Eugene Sh.
Nov 15 '18 at 18:40
For example here (about the efficiency claim): stackoverflow.com/questions/6321170/…
– mrc ntn
Nov 15 '18 at 19:05
Visual studio, on the other hand, godbolt.org/z/U95PKq
– user4581301
Nov 15 '18 at 19:07
1
1
Are you sure about the efficiency claim?
– Shafik Yaghmour
Nov 15 '18 at 18:31
Are you sure about the efficiency claim?
– Shafik Yaghmour
Nov 15 '18 at 18:31
godbolt.org/z/gmj1nn Not really surprising.
– Eugene Sh.
Nov 15 '18 at 18:40
godbolt.org/z/gmj1nn Not really surprising.
– Eugene Sh.
Nov 15 '18 at 18:40
For example here (about the efficiency claim): stackoverflow.com/questions/6321170/…
– mrc ntn
Nov 15 '18 at 19:05
For example here (about the efficiency claim): stackoverflow.com/questions/6321170/…
– mrc ntn
Nov 15 '18 at 19:05
Visual studio, on the other hand, godbolt.org/z/U95PKq
– user4581301
Nov 15 '18 at 19:07
Visual studio, on the other hand, godbolt.org/z/U95PKq
– user4581301
Nov 15 '18 at 19:07
add a comment |
1 Answer
1
active
oldest
votes
"it is more efficient to use
x*x
instead ofpow(x,2)
"
Not certainly more efficient. It might be the same. C allows analyze-ability of such functions like pow()
and both may emit the same code.
A compiler may not analyze your pow2()
and create sub-optimal code as compared to pow(a,2)
.
If truly concerned, profile your code. Yet what is best on one platform may differ on others.
How to implement a genuine unary implementation of pow2?
inline double pow2(double a){return a*a;}
is OK.
Would it be the most efficient way to obtain the second power of a double?
"most efficient" --> I suggest no function, just x*x
.
Also note that C allows FP to evaluate at higher precision that required. Research FLT_EVL_METHOD
. The goal for most efficient way to obtain the second power of a double with a function may defeat overall performance.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325817%2funary-operator-in-c-or-c-for-the-second-power-of-a-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
"it is more efficient to use
x*x
instead ofpow(x,2)
"
Not certainly more efficient. It might be the same. C allows analyze-ability of such functions like pow()
and both may emit the same code.
A compiler may not analyze your pow2()
and create sub-optimal code as compared to pow(a,2)
.
If truly concerned, profile your code. Yet what is best on one platform may differ on others.
How to implement a genuine unary implementation of pow2?
inline double pow2(double a){return a*a;}
is OK.
Would it be the most efficient way to obtain the second power of a double?
"most efficient" --> I suggest no function, just x*x
.
Also note that C allows FP to evaluate at higher precision that required. Research FLT_EVL_METHOD
. The goal for most efficient way to obtain the second power of a double with a function may defeat overall performance.
add a comment |
"it is more efficient to use
x*x
instead ofpow(x,2)
"
Not certainly more efficient. It might be the same. C allows analyze-ability of such functions like pow()
and both may emit the same code.
A compiler may not analyze your pow2()
and create sub-optimal code as compared to pow(a,2)
.
If truly concerned, profile your code. Yet what is best on one platform may differ on others.
How to implement a genuine unary implementation of pow2?
inline double pow2(double a){return a*a;}
is OK.
Would it be the most efficient way to obtain the second power of a double?
"most efficient" --> I suggest no function, just x*x
.
Also note that C allows FP to evaluate at higher precision that required. Research FLT_EVL_METHOD
. The goal for most efficient way to obtain the second power of a double with a function may defeat overall performance.
add a comment |
"it is more efficient to use
x*x
instead ofpow(x,2)
"
Not certainly more efficient. It might be the same. C allows analyze-ability of such functions like pow()
and both may emit the same code.
A compiler may not analyze your pow2()
and create sub-optimal code as compared to pow(a,2)
.
If truly concerned, profile your code. Yet what is best on one platform may differ on others.
How to implement a genuine unary implementation of pow2?
inline double pow2(double a){return a*a;}
is OK.
Would it be the most efficient way to obtain the second power of a double?
"most efficient" --> I suggest no function, just x*x
.
Also note that C allows FP to evaluate at higher precision that required. Research FLT_EVL_METHOD
. The goal for most efficient way to obtain the second power of a double with a function may defeat overall performance.
"it is more efficient to use
x*x
instead ofpow(x,2)
"
Not certainly more efficient. It might be the same. C allows analyze-ability of such functions like pow()
and both may emit the same code.
A compiler may not analyze your pow2()
and create sub-optimal code as compared to pow(a,2)
.
If truly concerned, profile your code. Yet what is best on one platform may differ on others.
How to implement a genuine unary implementation of pow2?
inline double pow2(double a){return a*a;}
is OK.
Would it be the most efficient way to obtain the second power of a double?
"most efficient" --> I suggest no function, just x*x
.
Also note that C allows FP to evaluate at higher precision that required. Research FLT_EVL_METHOD
. The goal for most efficient way to obtain the second power of a double with a function may defeat overall performance.
edited Nov 15 '18 at 19:02
answered Nov 15 '18 at 18:54
chuxchux
84.3k874154
84.3k874154
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325817%2funary-operator-in-c-or-c-for-the-second-power-of-a-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Are you sure about the efficiency claim?
– Shafik Yaghmour
Nov 15 '18 at 18:31
godbolt.org/z/gmj1nn Not really surprising.
– Eugene Sh.
Nov 15 '18 at 18:40
For example here (about the efficiency claim): stackoverflow.com/questions/6321170/…
– mrc ntn
Nov 15 '18 at 19:05
Visual studio, on the other hand, godbolt.org/z/U95PKq
– user4581301
Nov 15 '18 at 19:07