在 C++ 中使用对数实现运算符重载

发布于 2024-08-31 11:45:54 字数 760 浏览 4 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清引 2024-09-07 11:45:54

我的第二个目标是能够执行左操作数为双精度、右操作数为对数对象的运算。

为此,您需要将运算符声明为命名空间范围内的非成员函数(即不在 _log 的定义中),例如,

_log operator+(const double a, const _log& b);

如果您需要访问 的私有成员>_log,您可以在 _log 的定义中将其声明为友元:

friend _log operator+(const double a, const _log& b);

请注意,以下划线开头的名称(例如,_log)保留给在全局命名空间中实现;如果下划线后面跟着一个大写字母或另一个下划线,则它在任何地方都被保留。选择不同的类名称是个好主意。

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.

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.,

_log operator+(const double a, const _log& b);

If you need access to the private members of _log, you can declare it as a friend inside the definition of _log:

friend _log operator+(const double a, const _log& b);

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.

迷离° 2024-09-07 11:45:54

一些事情

  1. 在你的类前面使用 _ 是一个非常糟糕的主意(tm)。来自c++标准:

17.4.3.2.1 全局名称 [lib.global.names]
某些名称和函数签名集始终保留给
实施:

  • 每个包含双下划线 (_ _) 或以下划线开头的名称
    下划线后跟大写字母
    字母(2.11)保留给
    实现任何用途。
  • 每个以下划线开头的名称都保留给
    用作名称的实现
    全局命名空间。165

165) 这些名称也在命名空间 ::std (17.4.3.1) 中保留。

  1. 由于与 cmath 中的 log() 冲突,我猜测您使用了 _log 而不是 log。正是由于这个原因,将您自己的类保留在标准名称空间中是一个非常糟糕的主意。也许下一版本的标准将提供 _log 或 Logarithm 类?
    将您自己的类包装在 namespace somename {} 中,并使用 somename::Logarithm()

    引用它,

  2. 正如其他人已经提到的,您需要将运算符重载声明为朋友。而不是你拥有的东西

    log 运算符+ ( const double b ) const;

    更改为

    好友日志运算符+(const double d, const log&l);
    

    并在命名空间范围中定义函数。

  3. 这是基本公式变化的数学

    基本公式的更改

  4. 数学中的系数是指乘以日志。所以如果你有
    log_b(x) = y

    A 是系数,B 是基数,Y 是结果(或其他名称)

A few things

  1. Using an _ in the front of your class is a Very Bad Idea (tm). From the c++ standard:

17.4.3.2.1 Global names [lib.global.names]
Certain sets of names and function signatures are always reserved to the
implementation:

  • Each name that contains a double underscore (_ _) or begins with
    an underscore followed by an uppercase
    letter (2.11) is reserved to the
    implementation for any use.
  • Each name that begins with an underscore is reserved to the
    implementation for use as a name in
    the global namespace.165

165) Such names are also reserved in namespace ::std (17.4.3.1).

  1. 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 using somename::Logarithm()

  2. 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

    friend log operator+(const double d, const log& l);
    

    and define the function in the namespace scope.

  3. Here is the math for the change of base formula

    Change of base formula

  4. 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)

‖放下 2024-09-07 11:45:54

一些想法:

  1. 不要用前导下划线命名。此类标识符在 C 和 C++ 中具有放射性。
  2. 在使用浮点数进行运算之前定义对数之间的运算。
  3. 将 #2 与 James 的建议结合起来:

    友对数运算符+( const logarithm &l, const logarithm &r );
    
  4. 定义一个转换构造函数以从浮点数生成对数

    对数::对数( double f );
    

    现在,C++ 会将 double 转换为 1.0 + my_logmy_log + 1.0 中的 对数

  5. 在课堂上实现自然对数。不用担心base
  6. 根据函数定义基数转换:

    doublealternate_base( 双基 ) const;
    

    基数转换只是将对数除以替代底数的自然对数。将整数和小数部分一起返回到一个 double 中可能是最方便的。

A few ideas:

  1. Don't name with a leading underscore. Such identifiers are radioactive in C and C++.
  2. Define operations between logarithms before operations with floats.
  3. Combine #2 with James' suggestion:

    friend logarithm operator+( const logarithm &l, const logarithm &r );
    
  4. Define a conversion constructor to generate a logarithm from a float:

    logarithm::logarithm( double f );
    

    Now C++ will convert the double to a logarithm in either 1.0 + my_log or my_log + 1.0.

  5. Implement natural logarithms in your class. Don't bother with base.
  6. Define base conversion in terms of a function:

    double alternate_base( double base ) const;
    

    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 single double.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文