1 Star 0 Fork 0

zhouxs1023/eigenmath_pratt

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mstr.cpp 1.47 KB
一键复制 编辑 原始数据 按行查看 历史
Calin Barbat 提交于 2018-02-24 10:52 . Initial commit.
// Convert bignum to string
#include "stdafx.h"
#include "defs.h"
static int divby1billion(unsigned int *);
static char *str;
static int len;
char *
mstr(unsigned int *a)
{
int k, n, r, sign;
char c;
if (str == NULL) {
str = (char *) malloc(1000);
len = 1000;
}
// estimate string size
n = 10 * MLENGTH(a) + 2;
if (n > len) {
free(str);
str = (char *) malloc(n);
len = n;
}
sign = MSIGN(a);
a = mcopy(a);
k = len - 1;
str[k] = 0;
for (;;) {
k -= 9;
r = divby1billion(a);
c = str[k + 9];
sprintf(str + k, "%09d", r);
str[k + 9] = c;
if (MZERO(a))
break;
}
// remove leading zeroes
while (str[k] == '0')
k++;
if (str[k] == 0)
k--;
// sign
if (sign == -1) {
k--;
str[k] = '-';
}
mfree(a);
return str + k;
}
// Returns remainder as function value, quotient returned in a.
static int
divby1billion(unsigned int *a)
{
int i;
unsigned long long kk;
kk = 0;
for (i = MLENGTH(a) - 1; i >= 0; i--) {
if (little_endian()) {
((unsigned int *) &kk)[1] = ((unsigned int *) &kk)[0];
((unsigned int *) &kk)[0] = a[i];
} else {
((unsigned int *) &kk)[0] = ((unsigned int *) &kk)[1];
((unsigned int *) &kk)[1] = a[i];
}
a[i] = (int) (kk / 1000000000);
kk -= (unsigned long long) 1000000000 * a[i];
}
// length of quotient
for (i = MLENGTH(a) - 1; i > 0; i--)
if (a[i])
break;
MLENGTH(a) = i + 1;
if (little_endian())
return ((unsigned int *) &kk)[0];
else
return ((unsigned int *) &kk)[1];
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/zhouxs1023/eigenmath_pratt.git
git@gitee.com:zhouxs1023/eigenmath_pratt.git
zhouxs1023
eigenmath_pratt
eigenmath_pratt
master

搜索帮助