Frage

I am doing fixed point implementation and i am running the test and i am trying to check the precision of my fixedpoint operations with the precision of operations from cmath header.

so here is my is code in 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);
}

the above code is tested fine, but now i need to calculate the same operations and see the results from cmath header in the same test.cpp file. so how do i proceed, is it possible with the two namespaces ( one namespace of my header file, one namespace std)?

like

#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

   }

Is it possible like the above code, can someone help how to proceed with it.

Thanks

War es hilfreich?

Lösung

Answer based on your comments:

Contents of 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
   }
}

Source file:

#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;
}

Output:

8
0
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top