centos7 Python安装bigfloat库以处理高精度浮点数运算

记录下个小问题

这里使用的系统是centos7,python版本为python3.6

以下的python3,pip3命令应根据自己的设置调整

前提当然是已经安装配置好python和pip

直接尝试使用pip安装bigfloat库:

pip3 install bigfloat

这里产生报错:

building mpfr extension creating build/temp.linux-x86_64-3.6 gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/python3/include/python3.6m -c mpfr.c -o build/temp.linux-x86_64-3.6/mpfr.o mpfr.c:343:17: fatal error: gmp.h: No such file or directory #include "gmp.h" ^ compilation terminated. error: command gcc failed with exit status 1

---------------------------------------- Command "/usr/local/python3/bin/python3.6 -u -c "import setuptools, tokenize;__file__=/tmp/pip-install-cxc7p2hl/bigfloat/setup.py;f=getattr(tokenize, open, open)(__file__);code=f.read().replace( , );f.close();exec(compile(code, __file__, exec))" install --record /tmp/pip-record-oigx0po0/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-cxc7p2hl/bigfloat/

我们看到原因是找不到gmp.h,搜索后发现可以使用yum下载对应的头文件

输入命令:

yum install gmp-devel

确认安装,再次输入:

pip3 install bigfloat

又得到报错:

mpfr.c:344:18: fatal error: mpfr.h: No such file or directory #include "mpfr.h" ^ compilation terminated. error: command gcc failed with exit status 1

这回看起来和上一次类似,缺少mpfr.h文件

尝试使用同样的解决方法:

yum install mpfr-devel

确认安装成功

再再输入命令:

pip3 install bigfloat

似乎安装成功!

输入python3进入交互模式测试

>>> import bigfloat >>> bigfloat.BigFloat(1) BigFloat.exact(1.0000000000000000, precision=53)

ok,看来安装成功

bigfloat库的具体用法可以查看官方网站

经验分享 程序员 微信小程序 职场和发展