C++ 定点库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2945747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ fixed point library?
提问by uj2
I am looking for a free C++ fixed point library (Mainly for use with embedded devices, not for arbitrary precision math). Basically, the requirements are:
我正在寻找一个免费的 C++ 定点库(主要用于嵌入式设备,而不是用于任意精度的数学)。基本上,要求是:
- No unnecessary runtime overhead: whatever can be done at compile time, should be done at compile time.
- Ability to transparently switch code between fixed and floating point, with no inherent overhead.
- Fixed point math functions. There's no much point using fixed point if you need to cast back and forth in order to take a square root.
- Small footprint.
- 没有不必要的运行时开销:任何可以在编译时完成的工作,都应该在编译时完成。
- 能够在定点和浮点之间透明地切换代码,没有固有的开销。
- 定点数学函数。如果您需要来回转换以求平方根,那么使用定点没有多大意义。
- 占地面积小。
Any suggestions?
有什么建议?
回答by flatmush
There is an open-source fixed point math library project which can be found by following the links below:
可以通过以下链接找到一个开源定点数学库项目:
It is a C static library with a C++ class interface for C++ users, it implements the following functionality: Trig. Functions: sin, cos, tan, asin, acos, atan, atan2 Saturated Arithmetic: sadd, ssub, smul, sdiv Other Functions: sqrt, exp
它是一个带有 C++ 类接口的 C 静态库,供 C++ 用户使用,它实现了以下功能:Trig。函数:sin、cos、tan、asin、acos、atan、atan2 饱和算法:sadd、ssub、smul、sdiv 其他函数:sqrt、exp
It only supports 16.16 fixed-point datatype.
它只支持16.16 定点数据类型。
It is an actively developed open-source project (looking for interested developers).
它是一个积极开发的开源项目(寻找感兴趣的开发人员)。
回答by herohuyongtao
Check out the following two good implementations about handling fixed point representation in C++ (no external libs are needed).
查看以下关于在 C++ 中处理定点表示的两个很好的实现(不需要外部库)。
Fixed-Point-Classby Peter Schregle. It also efficiently implements the basic operations like addition, multiplication, and division.
Code example:
#include <fixed_point.h> using namespace fpml; main() { fixed_point<int, 16> a = 256; fixed_point<int, 16> b = sqrt(a); }
Implementing Fixed-Point Numbers in C++by Khuram Ali.
Peter Schregle 的Fixed-Point-Class。它还有效地实现了加法、乘法和除法等基本运算。
代码示例:
#include <fixed_point.h> using namespace fpml; main() { fixed_point<int, 16> a = 256; fixed_point<int, 16> b = sqrt(a); }
Khuram Ali在 C++ 中实现定点数。
回答by gbmhunter
Here is an open source fixed-point library on GitHub:
这是GitHub上的一个开源定点库:
https://github.com/mbedded-ninja/MFixedPoint
https://github.com/mbedded-ninja/MFixedPoint
It supports 32-bit and 64-bit fixed-point numbers (with a arbitrary quotient) and both fast (everything is templated, but a little more manual) and slow fixed-point numbers (more automatic, but slower).
它支持 32 位和 64 位定点数(具有任意商),并且支持快速(一切都是模板化的,但需要更多手动)和慢速定点数(更自动,但更慢)。
It is geared towards embedded platforms, however I have used it on both microcontrollers and Linux without any issues.
它面向嵌入式平台,但我在微控制器和 Linux 上都使用过它,没有任何问题。
回答by burner
I got a nice little c++ header. You can find it under sweet::Fixed. Simply define typedef sweet::Fixed MyFloat; and use it like any other float value. Or exchange it whatever float type you want later. The class has two 64 bit values. One for the integer part and on for the fraction.
我有一个漂亮的小 C++ 头文件。您可以在sweet::Fixed下找到它。只需定义 typedef sweet::Fixed MyFloat; 并像任何其他浮点值一样使用它。或者稍后交换任何您想要的浮点类型。该类有两个 64 位值。整数部分为 1,分数为 1。
I have a small fixed point c++11 class header impl in sweet.hppcalled fixed.hpp. It uses 32bit for both parts.
我在sweet.hpp 中有一个小的定点 c++11 类头实现,名为fixed.hpp。它对两个部分都使用 32 位。
typedef float MyFloat; // This will feel the same
typedef sweet::Fixed MyFloat; // like this
回答by Stef
I will try http://www.efgh.com/software/fixed.htmtiny lib...
我会尝试http://www.efgh.com/software/fixed.htmtiny lib ...
回答by PeterK
Maybe you could try the GMP or MPFR libraries. I'm quite sure they will satisfy your performance needs, but maybe they are too much for your needs and you want something more lightweight. Anyway, look here:
也许您可以尝试 GMP 或 MPFR 库。我很确定它们会满足您的性能需求,但也许它们对于您的需求来说太多了,而您想要更轻巧的东西。不管怎样,看这里:
or here:
或在这里: