C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Templates: Type-dependent constants

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Rune Allnor
Guest





PostPosted: Tue May 08, 2007 9:02 pm    Post subject: Templates: Type-dependent constants Reply with quote



Hi all.

I am working with a class which applies numerical algorithms
to numbers of type either 'float' or 'double'. In such applications
one needs to handle various type-dependent constants, like
minimum/maximum values, numerical accuracy for answers
after computations, etc. For the type 'float', the relative
accuracy is one the order 1e-8, for 'double' it is on the order
1e-15.

How can I design a template class which handles these
sorts of type-dependent constants seamlessly?

The idea is described as:

///////////////////////////////////////////////////////////////////////

template <typedef T>
class Data {
private:
T accoracty;
public:
Data();
};

Data::Data<T>()
{
if (T == float) accuracy = 1e-8;
if (T == double) accuracy = 1e-15;
}

///////////////////////////////////////////////////////////////////

Rune


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Daniel Krügler
Guest





PostPosted: Wed May 09, 2007 2:50 am    Post subject: Re: Templates: Type-dependent constants Reply with quote



On 8 Mai, 23:02, Rune Allnor <all...@tele.ntnu.no> wrote:
Quote:
I am working with a class which applies numerical algorithms
to numbers of type either 'float' or 'double'. In such applications
one needs to handle various type-dependent constants, like
minimum/maximum values, numerical accuracy for answers
after computations, etc. For the type 'float', the relative
accuracy is one the order 1e-8, for 'double' it is on the order
1e-15.

How can I design a template class which handles these
sorts of type-dependent constants seamlessly?

The idea is described as:

///////////////////////////////////////////////////////////////////////

template <typedef T

Should be: <typename T>

Quote:
class Data {
private:
T accoracty;

accuracy?

Quote:
public:
Data();

};

Data::Data<T>()
{
if (T == float) accuracy = 1e-8;
if (T == double) accuracy = 1e-15;

}

///////////////////////////////////////////////////////////////////

Simply use std::numeric_limits by writing

#include <limits>

template <typename T>
Data<T>::Data() : accuracy(std::numeric_limits<T>::epsilon())
{
}

(As you see, I have fixed your c'tor definition and have
additionally moved your initialization into the class
initializer list)

Greetings from Bremen,

Daniel Krügler



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Seungbeom Kim
Guest





PostPosted: Wed May 09, 2007 2:50 am    Post subject: Re: Templates: Type-dependent constants Reply with quote



Rune Allnor wrote:
Quote:
Hi all.

I am working with a class which applies numerical algorithms
to numbers of type either 'float' or 'double'. In such applications
one needs to handle various type-dependent constants, like
minimum/maximum values, numerical accuracy for answers
after computations, etc. For the type 'float', the relative
accuracy is one the order 1e-8, for 'double' it is on the order
1e-15.

How can I design a template class which handles these
sorts of type-dependent constants seamlessly?

Use template specialization:

template <typename T> struct traits;
// you can add other type-specific things to this class as well

template <> struct traits<float>
{ static float accuracy() { return 1e-8f; } };

template <> struct traits<double>
{ static double accuracy() { return 1e-15; } };

// use traits<float>::accuracy() or traits<double>::accuracy()

--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Le Chaud Lapin
Guest





PostPosted: Wed May 09, 2007 2:51 am    Post subject: Re: Templates: Type-dependent constants Reply with quote

On May 8, 4:02 pm, Rune Allnor <all...@tele.ntnu.no> wrote:
Quote:
Hi all.

I am working with a class which applies numerical algorithms
to numbers of type either 'float' or 'double'. In such applications
one needs to handle various type-dependent constants, like
minimum/maximum values, numerical accuracy for answers
after computations, etc. For the type 'float', the relative
accuracy is one the order 1e-8, for 'double' it is on the order
1e-15.

How can I design a template class which handles these
sorts of type-dependent constants seamlessly?

The idea is described as:

///////////////////////////////////////////////////////////////////////

template <typedef T
class Data {
private:
T accoracty;
public:
Data();

};

Data::Data<T>()
{
if (T == float) accuracy = 1e-8;
if (T == double) accuracy = 1e-15;

}

Hi Rune,

#include <iostream>
using namespace std;

template <typename X> inline static const double accuracy ();
template <> inline static const double accuracy<float >() {return
1e-8 ;}
template <> inline static const double accuracy<double>() {return
1e-15 ;}

int main ()
{
cout << "The accuracy for float is " << accuracy<float>() << endl;
cout << "The accuracy for double is " << accuracy<double>() << endl;

return 0;
}

-Le Chaud Lapin-


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Craig Scott
Guest





PostPosted: Wed May 09, 2007 2:52 am    Post subject: Re: Templates: Type-dependent constants Reply with quote

On May 9, 7:02 am, Rune Allnor <all...@tele.ntnu.no> wrote:
Quote:
Hi all.

I am working with a class which applies numerical algorithms
to numbers of type either 'float' or 'double'. In such applications
one needs to handle various type-dependent constants, like
minimum/maximum values, numerical accuracy for answers
after computations, etc. For the type 'float', the relative
accuracy is one the order 1e-8, for 'double' it is on the order
1e-15.

How can I design a template class which handles these
sorts of type-dependent constants seamlessly?

The idea is described as:

///////////////////////////////////////////////////////////////////////

template <typedef T
class Data {
private:
T accoracty;
public:
Data();

};

Data::Data<T>()
{
if (T == float) accuracy = 1e-8;
if (T == double) accuracy = 1e-15;
}

First up, if you have not already, you should take a look at
std::numeric_limits. It might already provide the sort of things you
need. If not, then one way you could implement what you want is given
below.

//////////////////////////////////////
#include <iostream>

template<typename T> struct AccuracyTraits;

template<> struct AccuracyTraits<double>
{
static double accuracy() { return 1e-15; }
};

template<> struct AccuracyTraits<float>
{
static float accuracy() { return 1e-8; }
};

int main(int argc, char* argv[])
{
std::cout << "Accuracy of double = " <<
AccuracyTraits<double>::accuracy() << std::endl;
std::cout << "Accuracy of float = " <<
AccuracyTraits<float>::accuracy() << std::endl;
}

/////////////////////////////////

If all Data did was provide an accuracy measure, it is not needed
because AccuracyTraits does this. If, however, Data does more than
what your original post showed, it would look something like this:

template <typedef T>
class Data {
private:
T accuracy;
public:
Data() : accuracy(AccuracyTraits::accuracy()) {}

// I presume you also have other stuff here in your real
problem....
};

--
Computational Fluid Dynamics, CSIRO (CMIS)
Melbourne, Australia


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Chris Uzdavinis
Guest





PostPosted: Wed May 09, 2007 8:17 pm    Post subject: Re: Templates: Type-dependent constants Reply with quote

On May 8, 5:02 pm, Rune Allnor <all...@tele.ntnu.no> wrote:
Quote:
For the type 'float', the relative
accuracy is one the order 1e-8, for 'double' it is on the order
1e-15.

How can I design a template class which handles these
sorts of type-dependent constants seamlessly?

It sounds like you are wanting a traits class. You might look at
std::numeric_limits<>, as it represents many traits of numbers. For
just the accuracy, you can "hand write" whatever you want, as I show
below. You might also call it something else if you need to have
other attributes about a given type, all bundled into one object. (In
that case, perhaps rename the struct from Accuracy to Traits, and then
rename value() to accuracy(), etc.)

// primary template (unimplemented)
template <typename T> struct Accuracy;

// specialize for float
template <> struct Accuracy<float> {
static float value() const { return 1e-8; }
};

// specialize for double
template <> struct Accuracy<double> {
static double value() const { return 1e-15; }
};

// then use the Accuracy template in your constructor:
Data::Data<T>()
{
accuracy = Accuracy<T>::value();
};

--
Chris


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Rune Allnor
Guest





PostPosted: Fri May 11, 2007 4:08 am    Post subject: Re: Templates: Type-dependent constants Reply with quote

On 9 May, 04:52, Craig Scott <audiofana...@gmail.com> wrote:

Quote:
First up, if you have not already, you should take a look at
std::numeric_limits. It might already provide the sort of things you
need.

The question was more about handling what I presume is known
as specialized templates. The numerics was the case study I
could come up with which most people can relate to.

Quote:
If not, then one way you could implement what you want is given
below.

//////////////////////////////////////
#include <iostream

template<typename T> struct AccuracyTraits;

template<> struct AccuracyTraits<double
{
static double accuracy() { return 1e-15; }

};

template<> struct AccuracyTraits<float
{
static float accuracy() { return 1e-8; }

};

int main(int argc, char* argv[])
{
std::cout << "Accuracy of double = "
AccuracyTraits<double>::accuracy() << std::endl;
std::cout << "Accuracy of float = "
AccuracyTraits<float>::accuracy() << std::endl;

}

/////////////////////////////////

If all Data did was provide an accuracy measure, it is not needed
because AccuracyTraits does this. If, however, Data does more than
what your original post showed, it would look something like this:

template <typedef T
class Data {
private:
T accuracy;
public:
Data() : accuracy(AccuracyTraits::accuracy()) {}

Is this correct? How does the compiler know which function
to use?

It shouldn't be

Data() : accuracy(AccuracyTraits<T>::accuracy()) {}

instead?

Rune


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Craig Scott
Guest





PostPosted: Fri May 11, 2007 9:10 am    Post subject: Re: Templates: Type-dependent constants Reply with quote

Quote:
It shouldn't be

Data() : accuracy(AccuracyTraits<T>::accuracy()) {}

instead?

Yes, sorry, my typo. You need the <T> on AccuracyTraits for the Data()
constructor, as you point out.

--
Computational Fluid Dynamics, CSIRO (CMIS)
Melbourne, Australia


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
 


Powered by phpBB © 2001, 2006 phpBB Group