自制pow()c ++ [英] self made pow() c++

查看:146
本文介绍了自制pow()c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读我如何写一篇功能函数自己?和dan04给出的答案吸引了我的注意,主要是因为我不知道fortran给出的答案,但我采取了并实现这个:

  #include< iostream> 
using namespace std;
float pow(float base,float ex){
// power of 0
if(ex == 0){
return 1;
// negative exponenet
} else if(ex< 0){
return 1 / pow(base,
// even exponenet
} else if((int)ex%2 == 0){
float half_pow = pow(base,ex / 2)
return half_pow * half_pow;
//整数exponenet
} else {
return base * pow(base,ex - 1);
}
}
int main(){
for(int ii = 0; ii <10; ii ++){\
cout< pow(< ii<<.5)=< pow(ii,.5)<< endl;
cout<< pow(< ii<<,2)=< pow(ii,2)<< endl;
cout<< pow(< ii<<,3)=< pow(ii,3)< endl;
}
}

虽然我不确定是否翻译所有调用给出.5作为指数返回0.在答案中,它表示它可能需要一个log2(x)基于 a ^ b = 2 ^(b * log2(a)),但是我不确定把它放在哪里,或者如果我甚至考虑这个问题。



注意:我知道这可能在数学库中定义,但是我不需要为一些函数增加整个数学库的所有费用。



编辑:有没有人知道分数指数的浮点实现? (我已经看到一个双重实现,但是这是使用一个技巧与寄存器,我需要浮点,并添加一个库只是为了做一个戏法我会更好,只是包括数学库)

$ b



我认为更简单的解决方案会缺乏准确性在结果中或不处理InF和NaN参数。



http://opensource.apple.com/source/Libm/Libm-2026/Source/Intel/expf_logf_powf.c



http://opensource.apple.com/source/Libm/Libm- 315 / Source / ARM / powf.c


I was reading through How can I write a power function myself? and the answer given by dan04 caught my attention mainly because I am not sure about the answer given by fortran, but I took that and implemented this:

#include <iostream>
using namespace std;
float pow(float base, float ex){
    // power of 0
    if (ex == 0){
        return 1;
    // negative exponenet
    }else if( ex < 0){
        return 1 / pow(base, -ex);
    // even exponenet
    }else if ((int)ex % 2 == 0){
        float half_pow = pow(base, ex/2);
        return half_pow * half_pow;
    //integer exponenet
    }else{
        return base * pow(base, ex - 1);
    }
}
int main(){
    for (int ii = 0; ii< 10; ii++){\
        cout << "pow(" << ii << ".5) = " << pow(ii, .5) << endl;
        cout << "pow(" << ii << ",2) = " << pow(ii,  2) << endl;
        cout << "pow(" << ii << ",3) = " << pow(ii,  3) << endl;
    }
}

though I am not sure if I translated this right because all of the calls giving .5 as the exponent return 0. In the answer it states that it might need a log2(x) based on a^b = 2^(b * log2(a)), but I am unsure about putting that in as I am unsure where to put it, or if I am even thinking about this right.

NOTE: I know that this might be defined in a math library, but I don't need all the added expense of an entire math library for a few functions.

EDIT: does anyone know a floating-point implementation for fractional exponents? (I have seen a double implementation, but that was using a trick with registers, and I need floating-point, and adding a library just to do a trick I would be better off just including the math library)

解决方案

Below are links to real implementations of powf.

I expect simpler solutions would lack accuracy in the result or not handle InF and NaN parameters.

http://opensource.apple.com/source/Libm/Libm-2026/Source/Intel/expf_logf_powf.c

http://opensource.apple.com/source/Libm/Libm-315/Source/ARM/powf.c

这篇关于自制pow()c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆