0

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

Rd7
  • 45
  • 2
  • 9
  • You cannot use namespaces like that. There can only be one main function. I'm not sure what you are trying to do, what's wrong with just writing the same code twice but with different *explicit* namespaces? Don't use `using namespace ...;` in other words. I think you may have the wrong idea about what namespaces are for. – john Apr 16 '13 at 09:47
  • *"now i need to calculate the same operations and see the results from cmath header"* - Can you explain what you mean by this? – JBentley Apr 16 '13 at 09:50
  • @john`what's wrong with just writing the same code twice but with different explicit namespaces?` so do i need to write the same code twice but with different explicit namespaces in two test files ? – Rd7 Apr 16 '13 at 09:53
  • @JBentleyi need the do the operations(like addition, subtractions) from my fixed point header and also from cmath header and compare the results of precision. – Rd7 Apr 16 '13 at 09:57
  • @JBentleyYes sorry, i thought addition, subtraction is also done by cmath header. so if i need to do the functions (sin,pow, sqrt) of the fixedpoint from my header, get the results and compare it with the results of the functions (sin,pow, sqrt) from cmath header in the same .cpp file. how do i need to do? – Rd7 Apr 16 '13 at 10:07
  • What do you mean "do the operations from ...."? Operators don't come from headers, they're part of the language, and you can't overload them for fundamental data types such as `float`. is for mathematical functions like `sin`, `pow`, `sqrt`, etc. In the code you've shown, you aren't using anything from either header. – JBentley Apr 16 '13 at 10:08
  • @Rd7 That sounds reasonable. – john Apr 16 '13 at 10:12
  • Sorry, I tried to change my comment, so it's not in the right order now. I will answer in a minute, – JBentley Apr 16 '13 at 10:14
  • @JBentleyYes sure take your time – Rd7 Apr 16 '13 at 10:29

1 Answers1

1

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
JBentley
  • 6,099
  • 5
  • 37
  • 72
  • Thank you, so the difference can be seen with the `::scope` from the header file and without the scope. – Rd7 Apr 16 '13 at 10:36
  • Yes, by prefixing the function name with `fp::` (known as fully qualifying it), you tell the compiler that you mean the `pow` function inside the `fp` namespace, instead of the `pow` function in the global namespace that was imported from . – JBentley Apr 16 '13 at 10:37
  • You can also force a header into a namespace by surrounding the include with `namespace X { #include
    }`. Note that this is not nice to do and may/will break some code, but in some cases it's the only way to do so.
    – dascandy Apr 16 '13 at 12:13