| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 7517 人关注过本帖
标题:cmath中的pow函数
只看楼主 加入收藏
kisscjy
Rank: 1
等 级:新手上路
帖 子:217
专家分:0
注 册:2007-4-9
收藏
 问题点数:0 回复次数:10 
cmath中的pow函数
当我使用G++编译器的时候
用到了cmath中的pow函数~~

x=pow(2,3);

结果就出了一大堆错误,郁闷啊

把错误信息给大家看一下,希望大家给个意见~

79006\Main.cc:14: error: call of overloaded `pow(int, int&)' is ambiguous
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/math.h:150: note: candidates are: double pow(double, double)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:361: note: long double std::pow(long double, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:357: note: float std::pow(float, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:353: note: double std::pow(double, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:349: note: long double std::pow(long double, long double)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:345: note: float std::pow(float, float)
79006\Main.cc:17: error: call of overloaded `pow(int, int&)' is ambiguous
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/math.h:150: note: candidates are: double pow(double, double)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:361: note: long double std::pow(long double, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:357: note: float std::pow(float, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:353: note: double std::pow(double, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:349: note: long double std::pow(long double, long double)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:345: note: float std::pow(float, float)
79006\Main.cc:24: error: call of overloaded `pow(int, int&)' is ambiguous
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/math.h:150: note: candidates are: double pow(double, double)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:361: note: long double std::pow(long double, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:357: note: float std::pow(float, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:353: note: double std::pow(double, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:349: note: long double std::pow(long double, long double)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:345: note: float std::pow(float, float)

但是在VC6.0中就完全正确~~
不解啊~~~
搜索更多相关主题的帖子: pow cmath 函数 
2007-10-25 01:03
永夜的极光
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2721
专家分:1
注 册:2007-10-9
收藏
得分:0 
据说pow()换成pow<double>()就行

G++对类型的要求比较严格,你的调用产生了二义性,可以使用强制转换


int a,b;
pow(a,b) 可改成 pow(long double(a),b)

[此贴子已经被作者于2007-10-25 8:20:15编辑过]


从BFS(Breadth First Study)到DFS(Depth First Study)
2007-10-25 08:16
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 
x=pow(2.0,3.0);

应该是这个编译器的类型检查更严格.

倚天照海花无数,流水高山心自知。
2007-10-25 08:18
kisscjy
Rank: 1
等 级:新手上路
帖 子:217
专家分:0
注 册:2007-4-9
收藏
得分:0 

好像不行~~改了之后依然会有错无~~

79034\Main.cc: In function `int main()':
79034\Main.cc:17: error: no matching function for call to `pow(int, int&)'
79034\Main.cc:20: error: no matching function for call to `pow(int, int&)'
79034\Main.cc:27: error: call of overloaded `pow(int, int&)' is ambiguous
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/math.h:150: note: candidates are: double pow(double, double)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:361: note: long double std::pow(long double, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:357: note: float std::pow(float, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:353: note: double std::pow(double, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:349: note: long double std::pow(long double, long double)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:345: note: float std::pow(float, float)


每当我一晚写下70,80个程序时,你还真以为,这都是我一个人干的.....不过说真的,其实都是抄书的~~ ^@^
2007-10-25 08:19
永夜的极光
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2721
专家分:1
注 册:2007-10-9
收藏
得分:0 

G++对类型的要求比较严格,你的调用产生了二义性,可以使用强制转换


int a,b;
pow(a,b) 可改成 pow(long double(a),b)


从BFS(Breadth First Study)到DFS(Depth First Study)
2007-10-25 08:21
kisscjy
Rank: 1
等 级:新手上路
帖 子:217
专家分:0
注 册:2007-4-9
收藏
得分:0 

我现在的问题是这样~

int x=2, y=3;

pow( x,y )

这样的话该怎样改呢??


每当我一晚写下70,80个程序时,你还真以为,这都是我一个人干的.....不过说真的,其实都是抄书的~~ ^@^
2007-10-25 08:24
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 

强制类型转换


倚天照海花无数,流水高山心自知。
2007-10-25 08:32
永夜的极光
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2721
专家分:1
注 册:2007-10-9
收藏
得分:0 
pow(long double(x),y)

从BFS(Breadth First Study)到DFS(Depth First Study)
2007-10-25 08:39
herolzx
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2007-9-25
收藏
得分:0 

我也遇到过,干脆自己写一个int pow(int,int)函数算了,也不难。
int pow(int a,int b){
int s=1;
for(int i=0,i<b,i++)s= s* a;
return s;
}

2007-10-25 08:42
kisscjy
Rank: 1
等 级:新手上路
帖 子:217
专家分:0
注 册:2007-4-9
收藏
得分:0 

我晕了~~还不行.....................
我把我的代码帖上来吧~~~
[CODE]
/*---------------------------------------------------------------


Description

  集合的前N个元素:编一个程序,按递增次序生成集合M的最小的N个数,M的定义如下:
(1)数1属于M;
(2)如果X属于M,则Y=2*X+1和Z=3*x+1也属于M;
(3)此外再没有别的数属于M。


Input

n(b.in) (1≤n≤100)

Output

生成集合M的最小的N个数(b.out)

Sample Input


4

Sample Output


1 3 4 7

-----------------------------------------------------------------*/


#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;


int main()
{
int num, i, j, k;
cin>>num;

int sum=0;
for( i=0; num > sum; i++ )
sum = sum + pow(2.0,double(i));

k=i-1;
int *p=new int [pow(2.0,double(k))];
vector< int > vec;

p[0]=1, vec.push_back(1);

for( i=1; i<=k; i++ )
{
for( j=pow(2,i)-1; j>=0; j--)
{
if( j % 2 !=0 )
p[j] = 3 * p[j/2] + 1;

else
p[j] = 2 * p[j/2] + 1;

vec.push_back( p[j] );
}
}

sort( vec.begin(), vec.end() );

for( i=0; i<num; i++ )
cout<<vec[i]<<" ";

cout<<endl;

return 0;
}



[/CODE]


每当我一晚写下70,80个程序时,你还真以为,这都是我一个人干的.....不过说真的,其实都是抄书的~~ ^@^
2007-10-25 08:43
快速回复:cmath中的pow函数
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033545 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved