2

所以我正在努力创建一个精确的 Decimal 结构,将它的特征和尾数存储在一个 long 和一个 unsigned long 中。因为我正在这样做,所以我不得不想出自己的减法和加法函数。

在测试我的函数时,我遇到了“负零”这个麻烦的问题。本质上,我不能表示 -0.1 到 -0.9,因为我无法在不使用某种标志值的情况下在零上加上负号。这是背景信息,我将发布代码,以便您了解我是如何进行算术运算的。奇怪的行为是我得到一个高于ULONG_MAX 的数字。具体来说,这是我的日志的输出:

diff->right: 18446744073699551616
b->right10000000
MANTISSA_LIMIT: 100000000
ULONG_MAX: 18446744073709551615
Subtracting 10.10000000 from 10.00000000
Test: tests/bin/decimal.out(subtractDecimalsWithCarry+0x79) [0x40109f]  Decimal: 0.10000000

和代码:

助手/十进制.h:

#ifndef __DECIMAL_H__   
#include <limits.h> 
#define MANTISSA_LIMIT 100000000
#define __DECIMAL_H__
typedef struct{          /* Calling them more convenient terms: */
    long left;           /* characteristic */
    unsigned long right; /* mantissa */
}Decimal;

void createDecimal(long left, unsigned long right, Decimal * dec);

/* Perform arithmetic operations on Decimal structures */
void add_decimals(Decimal* a, Decimal* b, Decimal* sum); 
void subtract_decimals(Decimal* a, Decimal* b, Decimal* diff); 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
void createDecimalFromString(Decimal * dec, const char * str);
#endif

然后是decimal.c的相关代码:

/* Subtract two decimals, a - b */
void subtract_decimals(Decimal* a, Decimal* b, Decimal* diff){
    diff->left = a->left - b->left;
    diff->right = a->right - b->right;
    fprintf(stderr, "diff->right: %lu\n", diff->right);
    fprintf(stderr, "b->right%lu\n", b->right);
    fprintf(stderr, "MANTISSA_LIMIT: %d\n", MANTISSA_LIMIT);
    fprintf(stderr, "ULONG_MAX: %lu\n", ULONG_MAX);
    if(diff->right > MANTISSA_LIMIT) { 
    if(diff->right != 18446744073699551616UL)
        diff->left -= 1;            
    else
        diff->left *= -1; /* This is where I might put a flag for -0*/
    diff->right = ULONG_MAX - diff->right + (18446744073699551616UL == diff->right ? 1 : 0);     /* +1 because of the wrap around, we have to 'count' 0. */
    }
}

void createDecimalFromString(Decimal * dec, const char * str){
    long left;
    unsigned long right;
    char * dotLocation;
    char rawLeft[9];
    char rawRight[9];
    int i;
    int dotPos;
    long leadingZeros;
    int numDetected;

    if(str == NULL)
         return;

    bzero(rawLeft,9);
    bzero(rawRight,9);

    dotLocation = strstr(str, ".");
    leadingZeros = numDetected = 0;
        if(dotLocation == NULL){
           left = atol(str);
           right = 0;
        }else{
        /* ghetto strncpy */
        for(i=0; i != 9 && str[i] != *dotLocation; ++i)
            rawLeft[i] = str[i];
        rawLeft[i] = '\0';
        dotPos = i+1;
        left = atol(rawLeft);
        for(i=0; i != 9 && str[dotPos] != '\0'; ++i,++dotPos){
            if(str[dotPos] == '0' && numDetected == 0)
               leadingZeros++;
            else
               numDetected = 1;

            rawRight[i] = str[dotPos];
        }
        rawRight[i] = '\0';
        right = strtoul(rawRight,NULL,10);
        if(leadingZeros > 0)
            /* subtract the leading zeros, then also the powers of ten taken by the number  itself*/
            right = (right*(powlu(10,7-leadingZeros-(i-2))));
        else
            right = right*(powlu(10,(i > 1 ? 8-(i-1) : 7 ))); 
    }

    dec->left = left;
    dec->right = right;

}

最后是调用代码:

#include <stdio.h>
#include <stdlib.h>
#include <execinfo.h>
#include <unistd.h>

#include "helpers/decimal.h"

void traceAndPrintDecimal(Decimal testDec){
    int nptrs;
void *buffer[100];
    char **strings; 
    nptrs = backtrace(buffer, 100);
    strings = backtrace_symbols(buffer, nptrs);
    printf("Test: %s  Decimal: %ld.%08lu\n", strings[1], testDec.left, testDec.right);

    free(strings);
}

void subtractDecimalsWithCarry(){
    Decimal oper1;
    Decimal oper2;
    Decimal result;
    createDecimalFromString(&oper1, "10.0");
    createDecimalFromString(&oper2, "10.1");
    subtract_decimals(&oper1, &oper2, &result);
    printf("Subtracting %ld.%08lu from %ld.%08lu\n",oper2.left,oper2.right,oper1.left,oper1.right);
    traceAndPrintDecimal(result);
}


int main(){

subtractDecimalsWithCarry();
return 0;
}

还有我用于编译的makefile:

decimal.o: src/helpers/decimal.c
    cc -I./headers -std=gnu99 -pedantic -Wall -Wextra -Werror -g -c src/helpers/decimal.c -o obj/decimal.o

test-decimal: tests/decimal-test.c decimal.o
    cc -I./headers -std=gnu99 -pedantic -Wall -Wextra -Werror -g tests/decimal-test.c obj/decimal.o -o tests/bin/decimal.out -lm -rdynamic

奇怪的diff->right是大于 ULONG_MAX,有谁知道为什么会这样?如果您需要更多信息,请告诉我,我会尽力更新问题。

4

1 回答 1

1

错误的“数字高于 ULONG_MAX”。

乍一看diff->right,值“18446744073699551616”似乎大于 ULONG_MAX(“18446744073709551615”)。但是少了9999999。(@UncleO)


OP 在评论中断言“知道为什么这些地方有点自大吗?由于尾数的工作方式,这个数字应该只偏离 1000000。但它反而偏离了 10000001”。建议这是不正确的。

// from createDecimalFromString(&oper1, "10.0");
oper1.right = 0
// from createDecimalFromString(&oper2, "10.1");
oper1.right = 10000000
// from subtract_decimals(&oper1, &oper2, &result)
diff->right = oper1.right - oper2.right --> 18446744073699551616

unsigned减法在 C 中有很好的定义。在这种情况下,差异oper1.right - oper2.right将在数学上导致oper1.right - oper1.right + (ULONG_MAX + 1).

“ ...无法由生成的无符号整数类型表示的结果以比结果类型可以表示的最大值大一的数字为模减少。” C11 6.2.5 8

于 2013-09-30T16:19:30.953 回答