我正在尝试做如下的事情
#include <stdio.h>
int main() {
float a = 0b00000100;
float b = 0b0.0110; // this line is not working
printf("The value of fist float is = %f\n", a*15);
printf("The value of second float is = %f\n", b);
return 0;
}
我在这里唯一的目标是为 b 分配小数二进制数,即 0.0110,结果应该是 0.375。有人有什么想法吗?
我快速编写了这段代码:
#include<stdio.h>
#include<string.h>
float binary_f(char fraction[]){
char *pointp; //pointer to the point
long integer=strtol(fraction, &pointp, 2); //Gives the integer and the pointer to the point, at the same time!
int point=(int)(pointp-fraction);
long div = 2;
int lenght=strlen(fraction);
float result=0;
for (int i=point+1;i<lenght;i++){
if (fraction[i]=='1'){
result+=(float)1/div;
}
div <<= 1; //Multiply the divisor by 2
}
return result+(float)integer;
}
int main() {
char fractionary[]="10.001";
float a=binary_f(fractionary);
printf("%f",a);
return 0;
}
正如您可能注意到的,它并不是真正强大,但它可以完成以下工作 这是预期的参数。