gpt4 book ai didi

c++ - .cpp 文件中的两个 namespace 用于比较

转载 作者:行者123 更新时间:2023-11-28 07:39:59 25 4
gpt4 key购买 nike

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

所以这是我在 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 header 的结果。那么我该如何继续,是否可以使用两个命名空间(我的头文件的一个命名空间,一个命名空间 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

}

是否可以像上面的代码一样,有人可以帮助如何进行。

谢谢

最佳答案

根据您的评论回答:

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

关于c++ - .cpp 文件中的两个 namespace 用于比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16033700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com