简体   繁体   English

C:具有幂函数将二进制转换为十进制

[英]C: with power function converting binary to decimal

I am trying to convert decimal number to binary number using recursion. 我正在尝试使用递归将十进制数转换为二进制数。 But I am getting wrong output...I tried to debug the program and got stuck with "pow()". 但是我得到了错误的输出...我试图调试程序,并卡在“ pow()”上。 I inserted many printf statements to see the status of the variables... Here is my program... 我插入了许多printf语句以查看变量的状态...这是我的程序...

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

void main(){
int n,k;
int z;
long b,j;
long binary(int,int);
printf("Enter a Number : ");
scanf("%d",&n);
printf("Binary Equivalent is : ");
k=log2(n);
b=binary(n,k);
z=pow(10,k); 
printf("\n\nb=%d ,z=%d",b,z); 
}



long binary(int n,int c)
{
    int a,np;
    static long b=0;
    if(n<=1){
        b=(n%2)*(int)pow(10,c)+b;
        printf("\n nmod2=%d\nb=%ld\nc=%d\nn=%d ",(n%2),b,c,n);
        return b;
    }
    else{
        a=n%2;  
        np=pow(10,c);
        b=a*np+b;
        printf("\n a=%d,np=%d \n nmod2=%d \n b=%ld \n c=%d \n n=%d ",a,np,(n%2),b,c,n);
        binary(n/2,--c);
    }
}


Output is:
Enter a Number : 5
Binary Equivalent is :
 a=1,np=99
nmod2=1
b=99    
c=2    
n=5
 a=0,np=10
nmod2=0
b=99
c=1
n=2
 nmod2=1
b=100
c=0
n=1

b=100 ,z=100

why the "pow(10,c)" in binary() when c=2 equals 99 why not 100 as in main()? 为什么当c = 2时,binary()中的“ pow(10,c)”等于99,为什么不像main()中那样为100?

The problem is here: 问题在这里:

(int)pow(10,c)

The result is around 100, but rounding it down with (int) can get you either 99 or 100 depending on the roundoff error of pow . 结果大约是100,但是根据pow的舍入误差,使用(int)对其进行四舍五入可以得到99或100。

This is a handy function lround in math.h that should help you. 这是一个方便的功能lroundmath.h ,应该帮助你。 It rounds a number towards the closest integer, rounding 0.5 towards zero. 将数字四舍五入到最接近的整数,将0.5四舍五入到零。

Alternatively, you could define: 另外,您可以定义:

int my_round(double f) {
    return (int)(f+0.5);
}

Adding 0.5 and rounding down looks like rounding to the closest integer. 加0.5并四舍五入看起来像四舍五入到最接近的整数。

The pow() function is a floating-point function, which tends to screw up such stuff. pow()函数是一个浮点函数,它倾向于弄糟这些东西。 You shouldn't use it for integers without proper rounding to compensate. 如果不进行适当的舍入以进行补偿,则不应将其用于整数。

Multiple problems. 多个问题。

  1. As others have point out that the result of pow(10,c) is the problem. 正如其他人指出的那样, pow(10,c)就是问题所在。 A good pow() would return exactly 100.0 for pow(10,2) . 一个好的 pow()将为pow(10,2) 恰好返回100.0。 It is reasonable to expect that behavior from pow() , but a cautious programmer would do as others suggest and round-to-nearest a floating point number to an int . 可以预期pow()行为是合理的,但是谨慎的程序员会按照他人的建议进行操作,并将浮点数舍入为int

  2. Round to nearest is best handle with a library function. 四舍五入是具有库函数的最佳句柄。 Should one want to roll your own: 如果应该自己动手做:

    int IRound(double f) { return (f > 0.0) ? (int)(f + 0.5) : (int)(f - 0.5); }

  3. long binary(int n,int c) does not return a value when n > 1 . n > 1long binary(int n,int c)不返回值。 Suspect return binary(n/2,--c) is wanted. 需要可疑return binary(n/2,--c)

  4. The static long b=0; static long b=0; is a bad way to deal with recursion. 是处理递归的不好方法。 long binary(int n,int c) would nominally correctly run only once. long binary(int n,int c)名义上只能正确运行一次。 A second call, b would not necessarily start at 0 . 第二个调用b不一定从0开始。

  5. Given pow() is flaky, log2() may need special care too. 由于pow()属于片状,因此log2()可能也需要特别注意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM