我正在学习如何使用 VC++ 2008 中内置的多精度 mpir 库。下面的代码是整数计算的阶乘:
#include "stdafx.h"
#include <afxwin.h>//for clipboard operation
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <windows.h>
#include <ctype.h>
using namespace std;
#pragma warning(disable: 4800)
#include <mpir.h>
#include <mpirxx.h>
#pragma warning(default: 4800)
HANDLE hCon;
enum Color { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };
void SetColor(Color c){
if(hCon == NULL)
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon, c);
}
int _tmain(int argc, _TCHAR* argv[])
{
mpf_class a(1,19449),b(1,19449),c(1,19449);//
SetColor(GREEN);
cout<< "Factorial Calculator v0.0, Shuai Xiao Dai, 2013" <<endl<<endl;
SetColor(RED);
cout<<"Input an integer to calculate its factorial."<<endl;
SetColor(PINK);
while(cin>>a){
c=a;
if (a<=0){
if (a<0)
{SetColor(PINK);
cout<<"Sorry, we don't handle negative numbers ... "<<endl;}
a = 1;
}
else{
if(a>=1){
for(;a>=1;a--)
{
b *=a;
}
}
else{
SetColor(PINK);
cout<<"Sorry, we don't what happens ... "<<endl;
a = 1;}
SetColor(YELLOW);
cout<<setprecision(50)<<c<<"! = ";
SetColor(WHITE);
cout << setprecision (10500) <<b << endl<<endl;
SetColor(RED);
cout<<"Input a new integer to calculate its factorial:" <<endl;
SetColor(PINK);
b=1;
}
}
system("pause");
return 0;
}
我的问题是:
1)如何将相同的阶乘计算器转换为 Matlab mex 函数,这样我就不必使用相对较慢的符号工具箱?
2)特别是如何将“多精度”结果传递给符号matlab变量会出现问题;