473,394 Members | 1,701 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,394 software developers and data experts.

Strongly type doubles?

I want to create several types of double, e.g. D1 and D2.
I want strong typing, such that a function defined for D1 will not
accept a D2 argument.

One solution is:
struct D1 {
double d;
};

However, the usage is cumbersome (I have to access member variable d),
I don't have any operators defined, and object creation is more
expensive than for native types (right?).

Can someone propose an alternate solution that enforces the type safety
I require?

Thanks,

Joseph

Nov 26 '06 #1
6 2412
Joseph Turian wrote:
I want to create several types of double, e.g. D1 and D2.
I want strong typing, such that a function defined for D1 will not
accept a D2 argument.

One solution is:
struct D1 {
double d;
};

However, the usage is cumbersome (I have to access member variable d),
I don't have any operators defined, and object creation is more
expensive than for native types (right?).

Can someone propose an alternate solution that enforces the type safety
I require?

Thanks,

Joseph
If you use the structs, you could define conversion operators for D1
and D2:

struct D1 {
double d;
operator double () { return d; }
};

Then you will be able to use them in mathematical expressions because
the conversion to double will take place automatically:

D1 a, b;
a.d = 15;
b.d = 25;

double c = a * b;

You will also be able to have functions that accept D1 objects, but not
raw doubles or D2 objects.

If I'm not mistaken, the compiler should also inline the conversions,
so you will not have any performance penalty for using them, instead of
regular doubles.

Regards,
Markus.

Nov 26 '06 #2
Joseph Turian wrote:
I want to create several types of double, e.g. D1 and D2.
I want strong typing, such that a function defined for D1 will not
accept a D2 argument.

One solution is:
struct D1 {
double d;
};

However, the usage is cumbersome (I have to access member variable d),
I don't have any operators defined, and object creation is more
expensive than for native types (right?).

Can someone propose an alternate solution that enforces the type safety
I require?
It is perfectly possible to make your own types that act like doubles,
but with whatever semantics you want to design in. Its not trivial
though to get all the details the way you want them to be.

The link here is to my code for a physical quantity type, but the
underlying principle involved is the same for a strongly typed double:

http://quan.sourceforge.net/index.html.

For variables the assembly code is indistinguishable from that using
doubles in most cases (using VC8.0 with a very good optimiser), however
the code for literal constants is better optimised for inbuilt types so
you do seem to lose performance there.

regards
Andy Little

Nov 26 '06 #3
Markus Svilans:
If you use the structs, you could define conversion operators for D1
and D2:

struct D1 {
double d;
operator double () { return d; }
};

Then you will be able to use them in mathematical expressions because
the conversion to double will take place automatically:

D1 a, b;
a.d = 15;
b.d = 25;

double c = a * b;

You will also be able to have functions that accept D1 objects, but not
raw doubles or D2 objects.
Or something like:

template<class T>
struct TypeProcurer {
typedef T Type;
};

#define DEFINE_LOAN_TYPE(name,T) \
struct name { \
typedef TypeProcurer< T >::Type Type; \
Type obj; \
name () {} \
name (Type const &x) : obj(x) {} \
operator Type&() { return obj; } \
operator Type const&() const { return obj; } \
};

DEFINE_LOAN_TYPE(Double1,double)
DEFINE_LOAN_TYPE(Double2,double)

int main()
{
Double1 a = 5, b;

b = a;
a = b;
a = b / 4;
b *= a;

Double2 c, d = 7;

a = d; /* Compiler ERROR */
}

I would have used templates but I think the macro solution is cleaner. The
DEFINE_LOAN_TYPE macro won't work with types which have a comma in them,
but this can be remedied by using Variable Argument List macros (which are
a part of C99).

--

Frederick Gotham
Nov 26 '06 #4

Frederick Gotham wrote:
Markus Svilans:
If you use the structs, you could define conversion operators for D1
and D2:

struct D1 {
double d;
operator double () { return d; }
};

Then you will be able to use them in mathematical expressions because
the conversion to double will take place automatically:

D1 a, b;
a.d = 15;
b.d = 25;

double c = a * b;

You will also be able to have functions that accept D1 objects, but not
raw doubles or D2 objects.

Or something like:

template<class T>
struct TypeProcurer {
typedef T Type;
};

#define DEFINE_LOAN_TYPE(name,T) \
struct name { \
typedef TypeProcurer< T >::Type Type; \
Type obj; \
name () {} \
name (Type const &x) : obj(x) {} \
operator Type&() { return obj; } \
operator Type const&() const { return obj; } \
};

DEFINE_LOAN_TYPE(Double1,double)
DEFINE_LOAN_TYPE(Double2,double)
I would have used templates but I think the macro solution is cleaner. The
DEFINE_LOAN_TYPE macro won't work with types which have a comma in them,
but this can be remedied by using Variable Argument List macros (which are
a part of C99).
The boost "strong typedef" also uses both a macro and templates:

#include <boost/operators.hpp>

#define BOOST_STRONG_TYPEDEF(T, D) \
struct D \
: boost::totally_ordered1< D \
, boost::totally_ordered2< D, T \
\
{ \
T t; \
explicit D(const T t_) : t(t_) {}; \
D(){}; \
D(const D & t_) : t(t_.t){} \
D & operator=(const D & rhs) { t = rhs.t; return *this;} \
D & operator=(const T & rhs) { t = rhs; return *this;} \
operator const T & () const {return t; } \
operator T & () { return t; } \
bool operator==(const D & rhs) const { return t == rhs.t; } \
bool operator<(const D & rhs) const { return t < rhs.t; } \
};

Greg

Nov 27 '06 #5
Joseph Turian wrote:
I want to create several types of double, e.g. D1 and D2.
I want strong typing, such that a function defined for D1 will not
accept a D2 argument.

One solution is:
struct D1 {
double d;
};

However, the usage is cumbersome (I have to access member variable d),
I don't have any operators defined, and object creation is more
expensive than for native types (right?).

Can someone propose an alternate solution that enforces the type safety
I require?
Using double type to represent mass and velocity can be expressed as
Dimensional Analysis.

Using the compiler to enforce strongly typed doubles is discussed by
following the link below.

http://www.nwcpp.org/Meetings/2005/09.html

You will find a link to a pdf with the lecture notes.

--

Nov 30 '06 #6

ar************@gmail.com wrote:
Using double type to represent mass and velocity can be expressed as
Dimensional Analysis.
Yes, much can be done if you know what the problem domain is. The OP
wasn't very specific though so the answers they are going to get are
going to be the best they can be but probably not that useful.

Nov 30 '06 #7

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

Similar topics

4
by: Bill Cohagan | last post by:
I'm trying to figure out the "best" way to implement a strongly typed ArrayList. Using inheritance is one approach. It has the advantage that I only have to write overrides for the Add method and...
2
by: theWizK | last post by:
Hello all. I have noticed that when I generate a strongly-typed dataset from an xml schema that the DataTables that are generated have their constructors marked as internal. What this means is...
5
by: j0mbolar | last post by:
i know what type promotion is but why is it important?
2
by: Mark | last post by:
Just wanted to confirm that my understanding of a strongly typed language is correct: 1. .NET is a strongly typed language because each variable / field must be declared a specific type (String,...
2
by: Ian Gore | last post by:
Hi, I'm relatively new to VB.NET so I'd be grateful if someone could point out what I don't understand here... 1) Creating a strongly typed collection by inheriting CollectionBase. This is...
7
by: Sathyaish | last post by:
I forgot the syntax for strongly typed enums in .NET. I know C# supports them. Not sure if strong-typing of enums is CLS compliant but, can you refresh my memory with the C# syntax and VB syntax,...
20
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the...
5
by: Macca | last post by:
Hi, My application receives data from multiple sources transferred over ethernet. This data is broken into packets of bytes before being transmitted over the network. My application has to...
4
by: Rachana | last post by:
Hi, I have understood Data Sets but what is meant by typed/untyped/ strongly typed datasets. Can any one explain me or suggest any site/ article, to get these concepts (and their ...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.