处理 GMP 中的表达式
我最近向自己介绍了用于高精度算术的 GMP 库。它看起来很容易使用,但在我的第一个程序中我遇到了实际问题。如何评估表达式。例如,如果我有“1+8*z^2”并且 z 是 mpz_t“大整数”变量,我如何快速评估它? (我正在编写的程序中有更大的表达式。)目前,我正在手动执行每一个操作,并将结果存储在临时变量中,就像“1+8*z^2”表达式一样:
1)首先执行 mpt_mul (z,z,z) 到 z 的平方
2) 然后定义一个名为“eight”的 mpz_t 变量,其值为 8。
3) 将步骤一的结果乘以这个 8 并存储在 temp 变量中。
4) 定义名为“one”的 mpz_t 变量,值为 1。
5) 将其添加到步骤 3 中的结果中以找到最终答案。
这是我应该做的吗?或者有更好的方法吗?如果有一本 GMP 用户手册来帮助人们入门,那确实会有帮助,但只有参考手册。
I introduced myself to the GMP library for high precision arithmetic recently. It seems easy enough to use but in my first program I am running into practical problems. How are expressions to be evaluated. For instance, if I have "1+8*z^2" and z is a mpz_t "large integer" variable, how am I to quickly evaluate this? (I have larger expressions in the program that I am writing.) Currently, I am doing every single operation manually and storing the results in temporary variables like this for the "1+8*z^2" expression:
1) first do mpt_mul(z,z,z) to square z
2) then define an mpz_t variable called "eight" with the value 8.
3) multiply the result from step one by this 8 and store in temp variable.
4) define mpz_t variable called "one" with value 1.
5) add this to the result in step 3 to find final answer.
Is this what I am supposed to be doing? Or is there a better way? It would really help if there was a user's manual for GMP to get people started but there's only the reference manual.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
GMP 带有 C++ 类接口提供了一种更直接的方式来表达算术表达式。该接口使用 C++ 运算符重载来允许您编写:
当然,这是假设您使用的是 C++。如果您仅使用 C,则可能需要使用 GMP 的 C 接口,该接口不提供操作员过载。
GMP comes with a C++ class interface which provides a more straightforward way of expressing arithmetic expressions. This interface uses C++ operator overloading to allow you to write:
This is, of course, assuming you're using C++. If you are using C only, you may need to use the C interface to GMP which does not provide operator overloading.
事实证明,在“expr”子目录中存在与 GMP 一起分发的不受支持的表达式解析器。它不是 GMP 本身的一部分,可能会发生更改,但在该目录中的自述文件中对此进行了讨论。它不能保证以最快的方式进行计算,因此买家要小心。
因此,用户在使用 GMP 时必须手动计算所有表达式,除非他们希望使用此库或制作自己的表达式解析器。
Turns out that there's an unsupported expression parser distributed with GMP in a "expr" subdirectory. It's not part of GMP proper and is subject to change but it is discussed in a README file in that directory. It isn't guaranteed to do the calculation in the fastest way possible, so buyer beware.
So the user must manually evaluate all expressions when using GMP unless they wish to use this library or make their own expression parser.