gpt4 book ai didi

c - libdecnumber 中 decNumberCopy() 的不同大小结构?

转载 作者:太空宇宙 更新时间:2023-11-04 04:06:27 25 4
gpt4 key购买 nike

我在项目中使用 libdecnumber,库中有一个特殊的小函数,其文档让我有点困惑。函数是decNumberCopy()

decNumber  * decNumberCopy(decNumber *, const decNumber *);

这是一个decNumber的定义

/* The size (integer data type) of each unit is determined by the   */
/* number of digits it will hold. */
#if DECDPUN<=2
#define decNumberUnit uint8_t
#elif DECDPUN<=4
#define decNumberUnit uint16_t
#else
#define decNumberUnit uint32_t
#endif
/* The number of units needed is ceil(DECNUMDIGITS/DECDPUN) */
#define DECNUMUNITS ((DECNUMDIGITS+DECDPUN-1)/DECDPUN)

/* The data structure... */
typedef struct {
int32_t digits; /* Count of digits in the coefficient; >0 */
int32_t exponent; /* Unadjusted exponent, unbiased, in */
/* range: -1999999997 through 999999999 */
uint8_t bits; /* Indicator bits (see above) */
/* Coefficient, from least significant unit */
decNumberUnit lsu[DECNUMUNITS];
} decNumber;

这是 libdecnumber 文档中关于 decNumberCopy() 的内容

decNumberCopy(number, source)

This function is used to copy the content of one decNumber structure to another. It is used when the structures may be of different sizes and hence a straightforward structure copy by C assignment is inappropriate. It also may have performance benefits when the number is short relative to the size of the structure, as only the units containing the digits in use in the source structure are copied.

因此,撇开简单的结构分配足以复制 decNumber 这一事实,任何对 libdecnumber 如何工作有更深入了解的人都可以告诉我这两个 decNumber 结构何时会尺寸不同?即使假设您将两个具有不同 DECNUMUNITS 值的不同编译模块放在一起(这确实是一件很糟糕的事情),decNumber 中也没有 size 成员,所以该函数无论如何都无法检测到不同大小的结构。

或者,decNumberCopy() 是否被泛化为 future 的库版本,其中各个 decNumber 可以在尺寸?

最佳答案

我猜这条评论来自 decNumber.h应该解释一下:

  /* Notes: */
/* 1. If digits is > DECDPUN then there will be more than one */
/* decNumberUnits immediately following the first element of lsu. */
/* These contain the remaining (more significant) digits of the */
/* number, and may be in the lsu array, or may be guaranteed by */
/* some other mechanism (such as being contained in another */
/* structure, or being overlaid on dynamically allocated storage). */

换句话说,decNumber 结构包含 decNumberUnit lsu[DECNUMUNITS]数组以保存“默认”位数,但如果需要,库会将数组增大到超出该大小。

更新:

快速浏览 decNumber.c (虽然我不知道它与最新的有多接近)看起来好像他们没有实现任何存储数字的支持,超出了 decNumber.lsu[DECNUMUNITS] 中可以容纳的数字。 .看起来像 decNumberCopy()更关心只复制使用过的 lsu[DECNUMUNITS]元素而不是整个事物(作为优化和/或允许测试以检测可能的数据损坏)。

关于c - libdecnumber 中 decNumberCopy() 的不同大小结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5422499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com