0

我正在做定点实现,我正在运行测试,我试图用 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

   }

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

谢谢

4

1 回答 1

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
于 2013-04-16T10:30:24.670 回答