gpt4 book ai didi

连接二进制数

转载 作者:太空宇宙 更新时间:2023-11-04 04:16:40 26 4
gpt4 key购买 nike

我正在尝试编写一个程序,该程序将采用以 10 为底的 float 并将其小数部分转换为以 2 为底的小数部分。在下面的代码中,我打算将我的转换函数调用到 printf 中,并格式化输出;我遇到的问题出在我的 fra_binary() 中,我无法找出返回整数的最佳方法,该整数分别由每个回合的转换结果组成(串联)。这是我现在所做的(代码未优化,因为我仍在努力):

#include <stdio.h>
#include <math.h>

int fra_binary(double fract) ;

int main()
{
long double n ;
double fract, deci ;
printf("base 10 :\n") ;
scanf("%Lf", &n) ;
fract = modf(n, &deci) ;
int d = deci ;
printf("base 2: %d.%d\n", d, fra_binary(fract)) ;

return(0) ;
}

int fra_binary(double F)
{
double fl ;
double decimal ;
int array[30] ;

for (int i = 0 ; i < 30 ; i++) {
fl = F * 2 ;
F = modf(fl, &decimal) ;
array[i] = decimal ;
if (F == 0) break ;
}

return array[0] ;
}

显然这会部分返回所需的输出,因为我需要将整个数组连接为一个 int 或 char 来显示我需要的 1 和 0 系列。所以每次,我都想使用我处理的数字的小数部分作为二进制数来连接(1 + 0 = 10 而不是 1)。我该怎么办?希望这是有道理的!

最佳答案

return array[0] ;只是 int array[30] 的第一个值设置在 fra_binary() .代码丢弃循环的第一个计算以外的所有 for (int i = 0 ; i < 30 ; i++) .

convert its fractional part in base 2

OP 的循环思想是一个很好的起点。然而int array[30]不足以对所有 double 的小数部分进行编码变成“二进制文件”。

can't figure out the best way to return an integer

返回 int将是不够的。而是考虑使用字符串 - 或者以类似的方式管理整数数组。

使用来自 <float.h> 的定义插入缓冲区需求。

#include <stdio.h>
#include <math.h>
#include <float.h>

char *fra_binary(char *dest, double x) {
_Static_assert(FLT_RADIX == 2, "Unexpected FP base");
double deci;
double fract = modf(x, &deci);
fract = fabs(fract);
char *s = dest;
do {
double d;
fract = modf(fract * 2.0, &d);
*s++ = "01"[(int) d];
} while (fract);
*s = '\0';

// For debug
printf("%*.*g --> %.0f and .", DBL_DECIMAL_DIG + 8, DBL_DECIMAL_DIG, x,
deci);

return dest;
}

int main(void) {
// Perhaps 53 - -1021 + 1
char fraction_string[DBL_MANT_DIG - DBL_MIN_EXP + 1];
puts(fra_binary(fraction_string, -0.0));
puts(fra_binary(fraction_string, 1.0));
puts(fra_binary(fraction_string, asin(-1))); // machine pi
puts(fra_binary(fraction_string, -0.1));
puts(fra_binary(fraction_string, DBL_MAX));
puts(fra_binary(fraction_string, DBL_MIN));
puts(fra_binary(fraction_string, DBL_TRUE_MIN));
}

输出

                       -0 --> -0 and .0
1 --> 1 and .0
3.1415926535897931 --> 3 and .001001000011111101101010100010001000010110100011
-0.10000000000000001 --> -0 and .0001100110011001100110011001100110011001100110011001101
1.7976931348623157e+308 --> 179769313486231570814527423731704356798070600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 and .0
2.2250738585072014e-308 --> 0 and .00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
4.9406564584124654e-324 --> 0 and .000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001

也不清楚为什么输入是 long double , 但处理是用 double .建议仅使用一种 FP 类型。

关于连接二进制数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51367314/

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