.cpp文件中的两个名称空间用于比较

时间:2013-04-16 09:43:53

标签: c++ namespaces fixed-point

我正在进行定点实现,我正在运行测试,我正在尝试使用cmath头的操作精度来检查我的定点操作的精度。

所以这是我在test.cpp中的代码

#include <fixed_point_header.h>
#include <stdio.h>


int main()
{
    float fp1 = 3.14159;
    float fp2 = 4.1723;
    float fp3,fp4,fp5,fp6;
    fp3 = fp1+fp2;
    fp4 = fp1-fp2;
    fp5 = fp1*fp2;
    fp6 = fp1/fp2;

    printf("float fixed point summation data ==%f\n",fp3);
    printf("float fixed point difference data ==%f\n",fp4);
    printf("float fixed point multiplied data ==%f\n",fp5);
    printf("float fixed point divided data ==%f\n",fp6);
}

上面的代码测试正常,但现在我需要计算相同的操作,并在同一test.cpp文件中查看cmath头的结果。那么我该如何继续,是否可以使用两个命名空间(我的头文件的一个命名空间,一个命名空间std)?

喜欢

#include <fixed_point_header.h>
#include <stdio.h>
#include <math.h>

using namespace fp;


    int main() {

   ...// do the fixedpoint operations here

   }
   using namespace std;

   int main() {

  ...// do the cmath operations here

   }

是否有可能像上面的代码一样,有人可以帮助如何继续它。

由于

1 个答案:

答案 0 :(得分:1)

根据您的意见回答:

fixed_point_header.h的内容:

namespace fp // This places your function inside the fp namespace
{
   float pow(float base, float exp)
   {
      return 0; // Replace with your algorithm
   }
}

源文件:

#include <iostream>
#include <cmath>
#include "fixed_point_header.h"

int main()
{
   float f1 = 2.0;
   float f2 = 3.0;
   std::cout << pow(f1, f2) << std::endl;     // from cmath
   std::cout << fp::pow(f1, f2) << std::endl; // from your header
   return 0;
}

输出:

8
0