检测嵌入式系统上的长双溢出

发布于 2024-10-21 09:49:42 字数 120 浏览 5 评论 0原文

我将在嵌入式系统上的 C++ 代码中使用大数字。幸运的是,编译器可以识别长双精度。
我无法使用标准库、boost 库、gnu 数学库等。并且系统没有内置 float math cpu。
现在我如何检测长双溢出?

I'm going to use big numbers in C++ code on an embedded system. Luckily the compiler recognizes long doubles.
I can not use standard libraries, boost libraries, gnu math libraries, etc. And the system has not got built-in float math cpu.
Now how can I detect long double overflows?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

〃安静 2024-10-28 09:49:42

您声明您需要“大数字”,但这并不一定意味着指示使用 long double 。据我所知,在大多数嵌入式应用程序中,选择 long double 是因为它具有更高的精度,即小数的分辨率更多位,而不是因为它增加了范围。

您还声明您的实现提供了很少的常用浮点库和/或功能。基于这些陈述,我会质疑您是否需要功能齐全的浮点功能。如果您的担忧仅限于“大数字”,请检查您的编译器是否提供 long long 数据类型,这是一个 64 位整数。

如果您确实需要一些浮点功能,您可以考虑定点实现。假设很长,您可以选择以 48.16 格式表示数字,这将允许 ~2.8x10^14 的数字,小数点右侧有 16 位。 (如果您需要定点计算的介绍,请从此处开始。

)说完了背景问题,我们再看一下原来的问题。如果您希望检测 unsigned int 中的溢出(我通常在嵌入式工作中这样做),将最新结果与前一个结果进行比较就足够了。例如,我的应用程序要求我定期检查由外部时钟驱动的 16 位计数器。如果我当前的观察值小于上次的观察值,那么我可以假设 16 位计数器溢出,并且我可以采取相应的措施。如果您使用 long long 整数数据类型实现大数字,则可以应用类似的策略来检测溢出。

Your state that you need "big numbers", but this does not necessarily mean that the use of long double is indicated. In most embedded applications that I know of, long double is chosen for its enhanced precision, i.e. more bits of resolution for fractional numbers, than for its increased range.

You also state your implementation offers little of the usual floating point libraries and/or functionality. Based on these statements, I would question whether your need fully functional floating-point capabilities. If your concerns are limited to "big numbers", check to see if your compiler offers a long long datatype, which is a 64-bit integer.

If you do need some floating-point capability, you might consider a fixed-point implementation. Assuming a long long, you might choose to represent numbers in a 48.16 format, which will permit numbers of ~2.8x10^14 with 16 bits to the right of decimal. (If you need an introduction to fixed-point computation, start here.)

Having addressed some of the background issues, let's look at the original question. If you wish to detect overflow in an unsigned int (which I commonly do in my embedded work), it's sufficient to compare your latest result with the previous one. For example, my application requires me to periodically inspect a 16-bit counter that is driven by an external clock. If my current observation is less than the last observation, then I can assume that the 16-bit counter overflowed, and I can take action accordingly. If you implement your big numbers using a long long integer datatype, you can apply a similar strategy to detect overflow.

别在捏我脸啦 2024-10-28 09:49:42

由于它不是标准 C++,因此您必须依赖特定环境提供的方法。嵌入式系统的制造商应该记录如何做到这一点。问他。

As it's not standard C++, you will have to rely on methods provided by your specific environment. The manufacturer of the embedded system should have documented how it can be done. Ask him.

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