c++ template class casting

hello all, i am implementing a fixedpoint arthimetic and i am doing a test for division in my test.cpp file. the following is the code for the test division,

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <fixed_point_header.h>
int main()
{
fp::fixed_point<int, 15> fp1 = 32767; 
fp::fixed_point<int, 15> fp2 = 3.1415926535; 
fp::fixed_point<int, 15> fp3;
fp3 = fp1/fp2;

std::cout<<"fixed point divided data =="<<fp3<<std::endl;
}


now i would like to add more detail to my results, by comparing my fixedpoint result with c++ double precision results ie.finding the difference between the two results, like below code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
        fp::fixed_point<int, 15> fp1 = 32767; 
	fp::fixed_point<int, 15> fp2 = 3.1415926535; 
	fp::fixed_point<int, 15> fp3;
	double a = 32767;
	double b = 3.1415926535; 
	double c, difference;
        
	fp3 = fp1/fp2;
	c=a/b;
	//difference= cast template class to double and subtract with double c
		
     }

can some one help me to proceed with how to do casting of template class and subtract with c++ double.

thank you
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
        fp::fixed_point<int, 15> fp1 = 32767; 
	    fp::fixed_point<int, 15> fp2 = 3.1415926535; 
		fp::fixed_point<int, 15> fp3;
		fp::fixed_point<int, 15> fp4;
		double a = 32767;
		double b = 3.1415926535; 
		double c;
        
		fp3 = fp1/fp2;
		c=a/b;
		fp4 =reinterpret_cast <double>(fp::fixed_point<int, 15>fp3) ;
		
     }


i tried this one, but i am getting the following errors

1
2
3
error: expected primary-expression before âfp3â
error: expected â)â before âfp3â
error: expected â)â before â;â token


can some one say what is missing and am i proceeding in a right way?
Is my question clear/understandable?
Assuming that there is an operator double() const or equivalent in fp::fixed_point<>,
use a static_cast<double> instead of a reinterpret_cast <double>

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <type_traits>
#include <cmath>

namespace fp
{
    template < typename T, std::size_t N, typename = void >
    struct fixed_point ; // not defined

    template < typename T, std::size_t N >
    struct fixed_point< T, N,
             typename std::enable_if< std::is_integral<T>::value, void >::type >
    {
        // ...

        fixed_point(double) ;

        fixed_point operator/ ( const fixed_point& that ) const ;

        explicit operator double() const ;

        // ...
    };
}

int main()
{
    const fp::fixed_point<int,15> fp1 = 32.767, fp2 = 3.1415926535;
    const double dbl1 = double(fp1), dbl2 = double(fp2) ;

    const auto fp_result = fp1 / fp2 ;
    const double dbl_result = dbl1 / dbl2 ;
    const double delta = std::abs( dbl_result - double(fp_result) ) ;

    std::cout << "fp_result: " << double(fp_result) << '\n'
              << "dbl_result: " << dbl_result << '\n'
              << "delta: " << delta << '\n' ;
}
Last edited on
Topic archived. No new replies allowed.