如何在C99中使数学对数函数? [英] How to make math logarithm functions in C99?
问题描述
我终于回到构建函数的过程,这是在制作实际的BPML语言之前要做的事情.在第3部分-数学中,我想做一些对数函数.
I'm finally back at building my functions, which is what I'm doing before making the actual BPML language. In Part 3 - Math, I want to make some logarithm functions.
我从一开始就不知道对数函数,但是随着我的深入,我学会了并做到这一点:
I never knew what logarithm functions were at the very beginning, but as I went deeper, I learned it and made this:
float log_num(int num) {
int mult;
float result = 0;
for (int i = 0; ; i++) {
mult = 10 ^ i;
if (mult >= num) {
result = i;
break;
}
}
return result;
}
log_num
仅支持 int
,并且 float
和 double
将具有各自的分隔符.
log_num
only supports int
and float
and double
will have their separate ones.
现在我在使用此功能时遇到两个问题:
Now I got 2 problems with this function:
- 当我尝试运行它并使用
100
作为函数中的数字时,结果应该是2.00
,但它给了我1.00
. - 因为要返回的值是
float
,所以我希望该函数给我不同的值(如果它不是10的幂).例如:2 = 0.30102999566398119521521373889472449
.
- When I tried to run it and use
100
as the number in the function, the result should've been2.00
, but it gave me1.00
. - Since the value to be returned is a
float
, I want the function to actually give me different values if it is not a power of 10. An example of it is2 = 0.30102999566398119521373889472449
.
问:如何解决问题1,如何使功能按问题2中的解释工作?
我想从头开始制作该功能,而不要依赖其他功能.
I want to make the function from scratch and not relying on other functions.
推荐答案
问:如何解决问题1 ...?
Q: How do I fix problem 1 ... ?
10 ^ i
是10 独占或和 i
.不是10 i .
10 ^ i
is 10 exclusive-or'ed with i
. It is not 10i.
查找log 10 (num)
To find the integer portion of log10(num)
int i_portion = 0;
if (num <= 0) Handle_elsewhere();
else {
int i_portion = 0;
int n = num;
while (n >= 10) {
n /= 10;
i_portion++;
}
printf("%d\n", i_portion);
}
问:...以及如何使该功能按问题2中的解释工作?
Q: ... and how do I make the function work as how I explained in problem 2?
以下是自C99以来的快速解决方案:
Below is a quick solution since C99:
#include <math.h>
float log_num(int num) {
return log10f(num);
}
要编写没有< math.h>
的代码是相当广泛的.为了高效施工,我们需要设计参数.
To code without <math.h>
is fairly broad. For efficient construction, we need design parameters.
首先,要弄清楚基础.标准的 log()
是 e ,而不是以10为底的问题暗示.
First, be clear about base. The standard log()
is base e, not base 10 as the question
implies.
转角/错误处理:您如何处理负输入?Log 任何正基数(0)通常以-∞形式返回.对于有限的正值,没有范围问题.+∞,也许是+∞应该返回什么?对于 NaN ,也许是NaN,应该返回什么?
Corner/Error handling: How do you want to handle negative inputs? Logany positive base(0) is usually returned as -∞. For finite positive values, no range issues. What should be returned for +∞, perhaps +∞? What should be returned for NaN, perhaps NaN?
准确性/精确度:您是否希望获得最佳结果或愿意放弃速度或小代码占用量的准确性?为什么要返回 float
而不是更常见的 double
?
Accuracy/precision: Do you want the best result or willing to given up accuracy for speed or small code foot-print? Why return float
versus the more common double
?
性能:仅运行时性能差的代码好吗?只需按照对数计算进行编码.由于目标包括 my_log_i(int),my_log_f(float),my_log_d(double)
,因此,现在只需编写代码 my_log_d(double)
,然后让其他人调用它即可.
Performance: simply code that is of poor run-time performance OK? Just code per Logarithm Calculation. Since the goal includes my_log_i(int), my_log_f(float), my_log_d(double)
, for now, just code my_log_d(double)
and have the the others call it.
可移植性-便携性如何?
Portability - how portable?
当然,我们可以编写一个简单的 float my_log_10(int)
,但是如果没有设计细节,结果将在很多方面都缺乏.
Sure we can code up a simply float my_log_10(int)
, but without the design details, the result would be lacking in many ways.
这篇关于如何在C99中使数学对数函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!