“权力的”方法 ?? [英] "power of" method ??
问题描述
我觉得很简单......有没有权力或者平方函数在
C ++中。这就是我想要实现的目标。
(数组[1] [0] -array [0] [0])* 2
这不适用于减号,所以我需要一个方形或幂函数。
也..有一个总和功能。最终,我正在尝试做一个欧几里德
距离方程,并需要找到和的总和。一些数字。
谢谢
Aaron
Aaron Gallimore写道:
我认为非常简单......是否存在权力或者在C ++中使用平方函数。这就是我想要实现的目标。
(数组[1] [0] - 阵列[0] [0])* 2
这不起作用减号,所以我需要一个方形或电源功能。
功率:pow()
但是如果你想要的只是一个正方形,我会自己写一个,因为
pow对于这个特定的任务来说有点过分:
双方(双x)
{
返回x * x;
}
也..是否有总和功能。
不可以。但是一旦你知道你如何代表
你想要总结的数字,就很容易写一个。 />
顺便说一句:你用的是什么教科书?
最后我试图做一个欧几里得的距离方程,需要找到总和一些数字。
???
为什么?要计算2点(x1,y1)和
(x2,y2)之间的距离,你需要做的就是(使用上面的方形函数):
double Dist = sqrt(square(x1 - x2)+ square(y1 - y2));
别告诉我你要用简单的加法替换一个函数
电话:-)
但好的,你去吧:
双倍金额(双Arg1) ,双Arg2)
{
返回Arg1 + Arg2;
}
double Dist = sqrt(sum(x1 - x2),square(y1 - y2)))
但是为什么不用它自己的函数调用替换减法: - )
双差(双Arg1,双Arg2)
{
返回Arg1 - Arg2;
}
double Dist = sqrt(sum(square(dif(x1,x2)),square(dif(y1,y2))));
嗯。以某种方式提醒我纯粹的Lisp :-)
-
Karl Heinz Buchegger
kb ****** @ gascad.at
" Karl Heinz Buchegger" < KB ****** @ gascad.at> écrit...
[...]双方(双x)
{
返回x * x;
}
内联双方(双x)
{
返回x * x;
}
等等。不是吗?
Pierre
" Pierre Maurette" < mmaauurreettttttee.ppiieerrrree @@ ffrreeee.ffrr>
写了" Karl Heinz Buchegger" < KB ****** @ gascad.at> écrit...
[...]双方(双x)
{
返回x * x;
}
内联双方(双x)
{
返回x * x;
}
等等。不是吗?
使用优化进行编译可能更容易。一个好的编译器
将内联此函数而不被告知。通常内联
仅作为一种允许函数定义在程序中多次出现的方法,例如,当函数是
在标题的命名空间范围内定义。
问候,
Buster。
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
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
"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
"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.
这篇关于“权力的”方法 ??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!