473,799 Members | 3,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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 2213
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******@gasca d.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" <mmaauurreetttt ttee.ppiieerrrr ee@@ffrreeee.ff rr>
wrote
"Karl Heinz Buchegger" <kb******@gasca d.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******@gasca d.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<typena me T>
inline T square(T x)
{
return x * x;
}
Jul 22 '05 #5
Buster wrote:

"Pierre Maurette" <mmaauurreetttt ttee.ppiieerrrr ee@@ffrreeee.ff rr>
wrote
"Karl Heinz Buchegger" <kb******@gasca d.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" <mmaauurreetttt ttee.ppiieerrrr ee@@ffrreeee.ff rr> wrote
"Karl Heinz Buchegger" <kb******@gasca d.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******@gasca d.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<typena me 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******@gasca d.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
2879
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 Vainio http://www.students.tut.fi/~vainio24
11
2104
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> nodes; // For clarity, let this be "orig_nodes" BuildTree includes a member variable:
11
4454
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)? Does "x**0.5" use the same algorithm as "sqrt(x)", or does it use some other (perhaps less efficient) algorithm based on logarithms? Thanks, Russ
4
3298
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 "end" is obvious. But not so for "start".
94
30357
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 Statement (C# Reference) statement to serialize access. " But when is it better to use "volatile" instead of "lock" ?
15
3417
by: John A Grandy | last post by:
Does .NET provide a "factors of" method ? I need to determine the factors of an integer.
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10482
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10225
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.