对数计算器

Logarithm calculator

我正在尝试制作一个对数计算器,但卡住了——它没有打印出一个值。问题可能出在第 15 行或第 24 行或两者。我怎样才能让它打印出值(都是用C写的)。

完整代码如下:

#include <stdio.h>
#include <stdlib.h>

// Finds base 10 logarithms

int main()
{
    float result;
    float base = 10.0;
    float multiplier = 1.0;
    // float counter1 = 0.0;
    // float counter2 = 0;

    printf("Input result: ");
    scanf("%l", result);

    // Solves for all results above the base
    if(result > base) {
        while(result > multiplier) {
            multiplier =  multiplier * multiplier; // the multiplier has to check non-whole numbers
            multiplier += 0.001;
        } // division
    }
    printf("Your exponent is: %l \n", &multiplier);
    printf("Hello mathematics!");
    return 0;
}

感谢所有帮助, Xebiq

你应该在printf中删除&,并在scanf中添加&

printf("Your exponent is: %f \n", multiplier);
scanf("%f", &result);

并在其中使用 %f

并以 10 为基数,我建议使用此函数来计算对数:

unsigned int Log2n(unsigned int n)
{
    return (n > 1) ? 1 + Log2n(n / 10) : 0;
}

你也应该在这里了解浮点数: multiplier += 0.001;

可能恰好 0.001 不会被添加到 multiplier 当我调试这个 0.00100005 在我的编译器中被添加到 multiplier。(这会影响乘法)

在 printf 中删除“&”,在 scanf 中在变量前添加“&”。