473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

template alike approach for floating point

Hello,

I am trying to work around the issue with floating point as template
parameter. Basically all I want is be able to take advantage of the
compile time optimization. For instance how would one write something
like this with a single class declaration:

struct s_05
{
static const double TF = 0.5;
bool f(double v) { return v TF; }
};

struct s_06
{
static const double TF = 0.6;
bool f(double v) { return v TF; }
};
.... and so on and so forth

thanks !
-Mathieu

Aug 29 '07 #1
2 1301
mathieu wrote:
Hello,

I am trying to work around the issue with floating point as template
parameter. Basically all I want is be able to take advantage of the
compile time optimization. For instance how would one write something
like this with a single class declaration:

struct s_05
{
static const double TF = 0.5;
bool f(double v) { return v TF; }
};

struct s_06
{
static const double TF = 0.6;
bool f(double v) { return v TF; }
};
... and so on and so forth
You need to find a way (or ways) to express your constants without
simply specifying them. For example, if your constants can be
expressed as rationals, then you could define your 'TF' in terms
of integrals:

template<int over, unsigned under>
struct s_
{
static const double TF;
bool f(double v) { return v TF; }
};

template<int over, unsigned under>
const double s_<over,under>::TF = double(over)/double(under);

typedef s_<5,10s_05;
typedef s_<6,10s_06;

int main() {
s_05 s1;
s_06 s2;

return s1.f(0.55) && s2.f(0.75);
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 29 '07 #2
On Aug 29, 3:58 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
mathieu wrote:
Hello,
I am trying to work around the issue with floating point as template
parameter. Basically all I want is be able to take advantage of the
compile time optimization. For instance how would one write something
like this with a single class declaration:
struct s_05
{
static const double TF = 0.5;
bool f(double v) { return v TF; }
};
struct s_06
{
static const double TF = 0.6;
bool f(double v) { return v TF; }
};
... and so on and so forth

You need to find a way (or ways) to express your constants without
simply specifying them. For example, if your constants can be
expressed as rationals, then you could define your 'TF' in terms
of integrals:

template<int over, unsigned under>
struct s_
{
static const double TF;
bool f(double v) { return v TF; }
};

template<int over, unsigned under>
const double s_<over,under>::TF = double(over)/double(under);

typedef s_<5,10s_05;
typedef s_<6,10s_06;

int main() {
s_05 s1;
s_06 s2;

return s1.f(0.55) && s2.f(0.75);
}
Pretty cool ! This should work for my limited usage.

Thanks !
-Mathieu

Aug 29 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Alexander Block | last post by:
Hello newsgroup, let's say I have a function like template<class Type> inline bool areEqual(const Type &a, const Type &b) { return ( a == b ); }
31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
3
by: Jason S | last post by:
is there any way to use templates to bind integer/floating point constants to a template for compile-time use? e.g. template <double conversion> class meters { const factor = conversion;
7
by: mathieu | last post by:
Hello, I did read the FAQ on template(*), since I could not find an answer to my current issue I am posting here. I have tried to summarize my issue in the following code (**). Basically I am...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
1
by: ALiX | last post by:
I'm writing a class where one template parameter must be a type behaving like a floating-point type. Is there a standard way of checking for this? I can come up with two ways of doing this. One...
20
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in...
10
by: Matthias | last post by:
Dear newsgroup. I want to write a template function which accepts either integer or floating point numbers. If a certain result is not a whole number and if the template parameter is an...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.