小编Ved*_*yak的帖子

pow功能出错

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

void main() {
    int decimal, count, binary, digit;
    printf("Enter the number : ");
    scanf("%d", &decimal);
    count = 0; binary = 0;
    while (decimal > 0) {
        digit = decimal % 2;
        binary = binary + digit * pow(10, count);
        decimal = decimal / 2;
        ++count;
    }
    printf("Binary form : %d", binary);
}
Run Code Online (Sandbox Code Playgroud)

我使用上面的代码将Decimal转换为二进制.但问题是输出.

Input           : 12
Expected Output : 1100
Actual Output   : 1099
Run Code Online (Sandbox Code Playgroud)

[img] https://i.imgur.com/1mZlMQN.png [/ img ]

其他输入也存在此问题.只有8给出正确的输出.

有人可以解释为什么会这样吗?当我将它移植到那里时,这个错误也出现在C++中.

PS:pow在检查数字是否是阿姆斯特朗以及是否为回文时,也会弹出此错误.

c

0
推荐指数
1
解决办法
344
查看次数

标签 统计

c ×1