所以我正在努力创建一个精确的 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,有谁知道为什么会这样?如果您需要更多信息,请告诉我,我会尽力更新问题。