473,395 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

"power of" method ??

Hi,

Pretty simple one I think...Is there a "power of" or squared function in
C++. This is what i'm trying to achieve.

(array[1][0]-array[0][0])*2

this doesnt work with minus numbers so i need a square or power function.

also.. Is there a "sum of" function. Ultimately i'm trying to do a euclidean
distance equation and need to find the "sum of" some numbers.

Thanks
Aaron
Jul 22 '05 #1
9 2188
Aaron Gallimore wrote:

Hi,

Pretty simple one I think...Is there a "power of" or squared function in
C++. This is what i'm trying to achieve.

(array[1][0]-array[0][0])*2

this doesnt work with minus numbers so i need a square or power function.

power of: pow()

but if all you want is a square, I would write one myself, since
pow is a littel bit of overkill for this specific task:

double square( double x )
{
return x * x;
}
also.. Is there a "sum of" function.
No. But it's easy to write one, once you know how you represent
the set of numbers you want to sum over.

BTW: What text books are you using?
Ultimately i'm trying to do a euclidean
distance equation and need to find the "sum of" some numbers.


???
What for? To calculate the distance between 2 points (x1,y1) and
(x2, y2), all you need to do is (using the square function from above):

double Dist = sqrt( square( x1 - x2 ) + square( y1 - y2 ) );

Don't tell me you want to replace the simple addition with a function
call :-)

But ok, here you go:

double sum( double Arg1, double Arg2 )
{
return Arg1 + Arg2;
}

double Dist = sqrt( sum( square( x1 - x2 ), square( y1 - y2 ) ) )

But then why not replace the subtraction with a function call of it's own :-)

double dif( double Arg1, double Arg2 )
{
return Arg1 - Arg2;
}

double Dist = sqrt( sum( square( dif( x1, x2 ) ), square( dif( y1, y2 ) ) ) );

Hmm. Remindes me somehow to pure Lisp :-)

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
"Karl Heinz Buchegger" <kb******@gascad.at> a écrit ...
[...]
double square( double x )
{
return x * x;
}


inline double square( double x )
{
return x * x;
}

And so on. No?

Pierre
Jul 22 '05 #3

"Pierre Maurette" <mmaauurreettttttee.ppiieerrrree@@ffrreeee.ffrr>
wrote
"Karl Heinz Buchegger" <kb******@gascad.at> a écrit ...
[...]
double square( double x )
{
return x * x;
}


inline double square( double x )
{
return x * x;
}

And so on. No?


It's probably easier to compile with optimizations. A good compiler
will inline this function without being told to. Generally "inline"
is only essential as a means of allowing a function definition to
occur more than once in a program, for example, when a function is
defined at namespace scope in a header.

Regards,
Buster.
Jul 22 '05 #4
Pierre Maurette wrote:
"Karl Heinz Buchegger" <kb******@gascad.at> a écrit ...
[...]
double square( double x )
{
return x * x;
}


inline double square( double x )
{
return x * x;
}

And so on. No?


Why would we want to convert integers or floats into doubles and later
back?

template<typename T>
inline T square(T x)
{
return x * x;
}
Jul 22 '05 #5
Buster wrote:

"Pierre Maurette" <mmaauurreettttttee.ppiieerrrree@@ffrreeee.ffrr>
wrote
"Karl Heinz Buchegger" <kb******@gascad.at> a écrit ...
[...]
> double square( double x )
> {
> return x * x;
> }
inline double square( double x )
{
return x * x;
}

And so on. No?


It's probably easier to compile with optimizations. A good compiler
will inline this function without being told to.


It will, but only if the function is defined in the same translation
unit as it is used. If not, the compiler simply has no chance to inline
it at all. If you make the funciton static and define it in the header,
you can actually go without inline.
Generally "inline" is only essential as a means of allowing a function
definition to occur more than once in a program, for example, when a
function is defined at namespace scope in a header.


Right, and that's exactly what you need to do if you want your function
to be inlined in every translation unit where it's called.

Jul 22 '05 #6

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c1*************@news.t-online.com...
Buster wrote:

"Pierre Maurette" <mmaauurreettttttee.ppiieerrrree@@ffrreeee.ffrr> wrote
"Karl Heinz Buchegger" <kb******@gascad.at> a écrit ...
[...]
> double square( double x )
> {
> return x * x;
> }

inline double square( double x )
{
return x * x;
}

And so on. No?
It's probably easier to compile with optimizations. A good compiler will inline this function without being told to.


It will, but only if the function is defined in the same

translation unit as it is used.
Yes. It is.
If not, the compiler simply has no chance to inline
it at all. If you make the funciton static and define it in the header, you can actually go without inline.
True enough. I assume by "make the function static", you mean
putting it in an anonymous namespace. I knew this but I hadn't made
the connection with avoiding inline. So, can we conclude (on the
assumption that our compiler is 'good') that inline is never
essential?
Generally "inline" is only essential as a means of allowing a function definition to occur more than once in a program, for example, when a function is defined at namespace scope in a header.


Right, and that's exactly what you need to do if you want your

function to be inlined in every translation unit where it's called.


Cheers,
Buster.
Jul 22 '05 #7
Buster wrote:
If not, the compiler simply has no chance to inline
it at all. If you make the funciton static and define it in the
header, you can actually go without inline.
True enough. I assume by "make the function static", you mean
putting it in an anonymous namespace.


I actually meant declaring it as 'static', but an anonymous namespace
will be ok, too. The point is that you need to make sure the linker
won't get into trouble if the function is defined multiple times.
I knew this but I hadn't made the connection with avoiding inline. So,
can we conclude (on the assumption that our compiler is 'good') that
inline is never essential?


Inline is only a hint anyway. The compiler is free to not inline
functions that are declared 'inline' or to inline functions you didn't
delcare so. But on some compilers the potential for the function to be
inlined is higher if you declare them as 'inline'. So if you want them
inlined, you should still say so.
Also, inline will make sure that multiple definitions of the function
don't make trouble. You can also do that with static or an anonymous
namespace, but it is still a different. In places where your function
is not inlineed, a non-inline version of that function is needed. When
declared 'inline', only one non-inline version exists for the whole
program (dunno if the standard says that, but on typical
implementations, it's the case), while static or anonymous namespace
will be decided on a per translation unit basis, and so the program
might contain the non-inline version several times.
Jul 22 '05 #8
Rolf Magnus wrote:

Pierre Maurette wrote:
"Karl Heinz Buchegger" <kb******@gascad.at> a écrit ...
[...]
double square( double x )
{
return x * x;
}


inline double square( double x )
{
return x * x;
}

And so on. No?


Why would we want to convert integers or floats into doubles and later
back?

template<typename T>
inline T square(T x)
{
return x * x;
}


Because if the OP asks a question like this you can bet your
ass that he has not heared of templates up to now. Why confuse
him with concepts that are way beyond his head? Let him manage
functions first and until he gets more fluent in C++ introduce him
to templates.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #9
Pierre Maurette wrote:

"Karl Heinz Buchegger" <kb******@gascad.at> a écrit ...
[...]
double square( double x )
{
return x * x;
}


inline double square( double x )
{
return x * x;
}

And so on. No?


Yes, sure.
But given the OP's current level of knowledge I didn't want
to introduce things that could confuse him :-)

Let him learn how to write functions, let him write some
programs wich use functions and then show him how he could
squeeze out a few nano seconds of his code.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

49
by: Ville Vainio | last post by:
I don't know if you have seen this before, but here goes: http://text.userlinux.com/white_paper.html There is a jab at Python, though, mentioning that Ruby is more "refined". -- Ville...
11
by: Joseph Turian | last post by:
Fellow hackers, I have a class BuildNode that inherits from class Node. Similarly, I have a class BuildTree that inherits from class Tree. Tree includes a member variable: vector<Node>...
11
by: Russ | last post by:
I have a couple of questions for the number crunchers out there: Does "pow(x,2)" simply square x, or does it first compute logarithms (as would be necessary if the exponent were not an integer)?...
4
by: =?utf-8?B?Qm9yaXMgRHXFoWVr?= | last post by:
Hello, what is the use-case of parameter "start" in string's "endswith" method? Consider the following minimal example: a = "testing" suffix="ing" a.endswith(suffix, 2) Significance of...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
15
by: John A Grandy | last post by:
Does .NET provide a "factors of" method ? I need to determine the factors of an integer.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.