在 C++ 中使用对数实现运算符重载
我在 C++ 中实现带有运算符重载的对数类时遇到一些问题。
我的第一个目标是如何实现 changeBase
方法,我一直很难理解它。
我试图理解改变对数底数背后的数学原理,但我没能做到。有人可以向我解释一下吗?
我的第二个目标是能够执行左操作数是 double 且右操作数是对数对象的操作。
这是我的日志类的片段:
// coefficient: double
// base: unsigned int
// result: double
class _log {
double coefficient, result;
unsigned int base;
public:
_log() {
base = 10;
coefficient = 0.0;
result = 0.0;
}
_log operator+ ( const double b ) const;
_log operator* ( const double b ) const;
_log operator- ( const double b ) const;
_log operator/ ( const double b ) const;
_log operator<< ( const _log &b );
double getValue() const;
bool changeBase( unsigned int base );
};
你们太棒了,谢谢您的宝贵时间。
I'm having some issues with implementing a logarithm class with operator overloading in C++.
My first goal is how I would implement the changeBase
method, I've been having a tough time wrapping my head around it.
I have tried to understand the math behind changing the base of a logarithm, but i haven't been able to. Can someone please explain it to me?
My second goal is to be able to perform an operation where the left operand is a double
and the right operand is a logarithm object.
Here's a snippet of my log class:
// coefficient: double
// base: unsigned int
// result: double
class _log {
double coefficient, result;
unsigned int base;
public:
_log() {
base = 10;
coefficient = 0.0;
result = 0.0;
}
_log operator+ ( const double b ) const;
_log operator* ( const double b ) const;
_log operator- ( const double b ) const;
_log operator/ ( const double b ) const;
_log operator<< ( const _log &b );
double getValue() const;
bool changeBase( unsigned int base );
};
You guys are awesome, thank you for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为此,您需要将运算符声明为命名空间范围内的非成员函数(即不在
_log
的定义中),例如,如果您需要访问
的私有成员>_log
,您可以在_log
的定义中将其声明为友元:请注意,以下划线开头的名称(例如,
_log
)保留给在全局命名空间中实现;如果下划线后面跟着一个大写字母或另一个下划线,则它在任何地方都被保留。选择不同的类名称是个好主意。To do this, you need to declare the operator as a non-member function at namespace scope (i.e., not in the definition of
_log
), e.g.,If you need access to the private members of
_log
, you can declare it as a friend inside the definition of_log
:Note that names starting with an underscore (e.g.,
_log
) are reserved to the implementation in the global namespace; if the underscore is followed by a capital letter or another underscore, it is reserved everywhere. It would be a good idea to choose a different class name.一些事情
由于与 cmath 中的 log() 冲突,我猜测您使用了 _log 而不是 log。正是由于这个原因,将您自己的类保留在标准名称空间中是一个非常糟糕的主意。也许下一版本的标准将提供 _log 或 Logarithm 类?
将您自己的类包装在
namespace somename {}
中,并使用somename::Logarithm()
引用它,
正如其他人已经提到的,您需要将运算符重载声明为朋友。而不是你拥有的东西
log 运算符+ ( const double b ) const;
更改为
并在命名空间范围中定义函数。
这是基本公式变化的数学
数学中的系数是指乘以日志。所以如果你有
log_b(x) = y
A 是系数,B 是基数,Y 是结果(或其他名称)
A few things
I'm guessing that you used _log instead of log due to the clash with log() in cmath. It is a Very Bad Idea to keep your own classes in the standard namespace for this very reason. Maybe the next version of the standard will provide a _log or Logarithm class?
Wrap your own class in
namespace somename {}
and reference it by usingsomename::Logarithm()
As others have mentioned already You need to declare your operator overloading as friend. Instead of what you have
log operator+ ( const double b ) const;
change it to
and define the function in the namespace scope.
Here is the math for the change of base formula
Coefficient in math means the part that is being multiplied by the log. So if you had
A log_b(x) = y
A is the coefficient, B is the base, and Y is the result (or some other names)
一些想法:
将 #2 与 James 的建议结合起来:
定义一个转换构造函数以从浮点数生成
对数
:现在,C++ 会将
double
转换为1.0 + my_log
或my_log + 1.0
中的对数
。base
。根据函数定义基数转换:
基数转换只是将
对数
除以替代底数的自然对数。将整数和小数部分一起返回到一个double
中可能是最方便的。A few ideas:
Combine #2 with James' suggestion:
Define a conversion constructor to generate a
logarithm
from a float:Now C++ will convert the
double
to alogarithm
in either1.0 + my_log
ormy_log + 1.0
.base
.Define base conversion in terms of a function:
Base conversion is simply dividing the
logarithm
by the natural log of the alternate base. It is probably most convenient to return the integer and fractional parts together in a singledouble
.