ginac库定义数字

2020-10-11  本文已影响0人  一路向后

1.程序源码

#include <iostream>
#include <ginac/ginac.h>

using namespace std;
using namespace GiNaC;

int main()
{
        //定义整数
        numeric two = 2;

        //定义分数
        numeric r(2,3);

        //定义浮点数
        numeric e(2.71828);

        //从字符串定义数字
        numeric p = "3.14159265358979323846";

        //定义复数
        numeric z1 = 2-3*I;
        numeric z2 = 5.9+1.6*I;
        numeric z3 = "1/4+2/3*I";
        numeric z4 = z1 + z3;

        //打印输出结果
        cout << two << endl;
        cout << r << endl;
        cout << e << endl;
        cout << p << endl;
        cout << z1 << endl;
        cout << z2 << endl;
        cout << z3 << endl;
        cout << z4 << endl;
        //转小数输出
        cout << z4.evalf() << endl;
        //转分数/整数输出
        cout << z4.eval_integ() << endl;

        return 0;
}

2.编译源码

$ g++ -o example example.c -lginac -lcln -I/usr/local/include -L/usr/local/lib64

3.运行结果

./example
2
2/3
2.7182800000000000296
3.1415926535897932385
2-3*I
5.9000000000000003553+1.6000000000000000888*I
1/4+2/3*I
9/4-7/3*I
2.25-2.3333333333333333333*I
9/4-7/3*I
上一篇下一篇

猜你喜欢

热点阅读