By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,795 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,795 IT Pros & Developers. It's quick & easy.

User defined data type.

P: n/a
I need a specific non existing data type, and it seems that i am not
smart enough to write the code my self, so if anyone will take the time
to do it for me, they are more than welcome and i will be forever
gratefull.

Type definition:
floating point value.
max value == +1
min value == -1

As for precission, im afraid i dont really know how it work, but it
would be preferable if the would be 8, 16 or 32 bit.

Regards

Jan 19 '07 #1
Share this Question
Share on Google+
13 Replies


P: n/a
za******@gmail.com wrote in news:1169165028.566907.266540
@v45g2000cwv.googlegroups.com:
I need a specific non existing data type, and it seems that i am not
smart enough to write the code my self, so if anyone will take the time
to do it for me, they are more than welcome and i will be forever
gratefull.

Type definition:
floating point value.
max value == +1
min value == -1

As for precission, im afraid i dont really know how it work, but it
would be preferable if the would be 8, 16 or 32 bit.
Even if someone were to want to do your homework for you, you haven't
specified nearly enough information to actually implement it. Like what is
supposed to happen if someone were to attempt to assign 1.5 to your object?

class NewType
{
public:
NewType() : internalValue(0.0) {};

NewType & operator=(double newVal) { x = newVal; return *this; };

private:
double internalValue;
};
NewType x;

x = 1.5;
Jan 19 '07 #2

P: n/a

Andre Kostur skrev:
za******@gmail.com wrote in news:1169165028.566907.266540
@v45g2000cwv.googlegroups.com:
I need a specific non existing data type, and it seems that i am not
smart enough to write the code my self, so if anyone will take the time
to do it for me, they are more than welcome and i will be forever
gratefull.

Type definition:
floating point value.
max value == +1
min value == -1

As for precission, im afraid i dont really know how it work, but it
would be preferable if the would be 8, 16 or 32 bit.

Even if someone were to want to do your homework for you, you haven't
specified nearly enough information to actually implement it. Like what is
supposed to happen if someone were to attempt to assign 1.5 to your object?
Allways people think it is homework. I would be the happyest man alive
if it were, as that probably would mean that i would eventually have an
education.

"Like what is supposed to happen if someone were to attempt to assign
1.5 to your object?"
the same thing as when you add 2^32 to an unsigned int (i should be
correct in assuming that an unsigned int can hold max 2^32-1)
>
class NewType
{
public:
NewType() : internalValue(0.0) {};

NewType & operator=(double newVal) { x = newVal; return *this; };

private:
double internalValue;
};
NewType x;

x = 1.5;
Jan 19 '07 #3

P: n/a
oops, pressed the button prematurely.

this is basicly what i need;

union Coord_T {
int id;
struct {
double x;
double y;
double z;
};
};

However, it is somewhat difficult cramming 3*8 byte into 4 or 8 and
thats the reason for ,me wanting a userdefined datatype.

Dont know if it makes sence, but thats the way it is.

Jan 19 '07 #4

P: n/a
za******@gmail.com wrote:
>
Andre Kostur skrev:
>za******@gmail.com wrote in news:1169165028.566907.266540
@v45g2000cwv.googlegroups.com:
I need a specific non existing data type, and it seems that i am not
smart enough to write the code my self, so if anyone will take the time
to do it for me, they are more than welcome and i will be forever
gratefull.

Type definition:
floating point value.
max value == +1
min value == -1

As for precission, im afraid i dont really know how it work, but it
would be preferable if the would be 8, 16 or 32 bit.

Even if someone were to want to do your homework for you, you haven't
specified nearly enough information to actually implement it. Like what
is supposed to happen if someone were to attempt to assign 1.5 to your
object?

Allways people think it is homework. I would be the happyest man alive
if it were, as that probably would mean that i would eventually have an
education.

"Like what is supposed to happen if someone were to attempt to assign
1.5 to your object?"
the same thing as when you add 2^32 to an unsigned int (i should be
correct in assuming that an unsigned int can hold max 2^32-1)
What do you think happens when someone assigns 2^32 to an unsigned int
(assuming 32 bits in an unsigned int, which is just the minimum required)?
Should you be expect anything like an error message or an exception, you
ought to revisit your expectations.

If you are happy with undefined behavior to occur when the bounds of your
new arithmetic type are violated, you could just use a double. As this is
probably not what you want, you still need to be more precise as to what
you want to happen. The analogy you gave, does not tell us.

>class NewType
{
public:
NewType() : internalValue(0.0) {};

NewType & operator=(double newVal) { x = newVal; return *this; };

private:
double internalValue;
};
NewType x;

x = 1.5;

Best

Kai-Uwe Bux
Jan 19 '07 #5

P: n/a
<za******@gmail.comwrote in message
news:11*********************@11g2000cwr.googlegrou ps.com...
oops, pressed the button prematurely.

this is basicly what i need;

union Coord_T {
int id;
struct {
double x;
double y;
double z;
};
};

However, it is somewhat difficult cramming 3*8 byte into 4 or 8 and
thats the reason for ,me wanting a userdefined datatype.

Dont know if it makes sence, but thats the way it is.
You are not making sense. What is wrong with your Coord_T for storing the
values? Coord_T.x can store a value from -1 to +1 already. What is it
about using a double that you don't like? Please give code what will happen
with Coord_T, and what you WANT to happen.

Something like:

Coord_T Foo;
Coord_T Bar;

Foo.x = -1.0;
Bar.x = -1.0;

std::cout << Foo.x + Bar.x;

That would output -2.0. What do you want it to output? Etc...

Jan 19 '07 #6

P: n/a

"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:eo**********@murdoch.acc.Virginia.EDU...
za******@gmail.com wrote:

(assuming 32 bits in an unsigned int, which is just the minimum required)?
Actually the minimum is 16, you're talking about longs ;)

- Sylvester
Jan 19 '07 #7

P: n/a
Sylvester Hesp wrote:
>
"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:eo**********@murdoch.acc.Virginia.EDU...
>za******@gmail.com wrote:

(assuming 32 bits in an unsigned int, which is just the minimum
required)?

Actually the minimum is 16, you're talking about longs ;)
Oops, my bad.
Thanks

Kai-Uwe Bux
Jan 19 '07 #8

P: n/a
On Jan 19, 1:57 am, zacar...@gmail.com wrote:
oops, pressed the button prematurely.

this is basicly what i need;

union Coord_T {
int id;
struct {
double x;
double y;
double z;
};

};

However, it is somewhat difficult cramming 3*8 byte into 4 or 8 and
thats the reason for ,me wanting a userdefined datatype.

Dont know if it makes sence, but thats the way it is.
So your problem is that using double or float is taking up to much
memory and you thought that since you know that the values can only
range from -1 to 1 it might be possible to create some kind of new type
that does not require so much storage?

If that is so I'd like to ask why you think that you can't afford the
space required by doubles?

Second I'd like to point out that the "granularity" of IEEE floating
point numbers is much higher for small values, so you can probably use
float instead of double (shrinking the size required to 3*4 bytes),
unless you have a need for really great accuracy.

Third, I'll propose a solution if none of the above will help, but I
wouldn't _ever_ do it myself, and I'm sure that everyone will agree
that it's a _really_ bad idea. Anyway here it is:

If you take some time to study the IEEE floating point specification
you should be able to either construct a new floating-point type even
smaller than float (2 or 3 bytes), but then you'll have to implement
all operations on them by yourself and will not be able to use the
processors floating-point units, meaning that performance will suffer
greatly.

--
Erik Wikström

Jan 19 '07 #9

P: n/a
za******@gmail.com wrote:
I need a specific non existing data type, and it seems that i am not
smart enough to write the code my self, so if anyone will take the time
to do it for me, they are more than welcome and i will be forever
gratefull.

Type definition:
floating point value.
max value == +1
min value == -1

As for precission, im afraid i dont really know how it work, but it
would be preferable if the would be 8, 16 or 32 bit.

Regards
heres something. Maybe it will help for ideas how to proceed. A fully
fledged UDT needs a full set of operations of course and ideally
details about what it does in all situations. Also needs detail
decisions on design, e.g how it interacts with other types etc.

Personally i would just live with float or double for underlying
value_type to start with

regards
Abdy Little
#include <stdexcept>

// first param is some constraint functor (see example at end)
// second is underlying value_type

template<typename Constraint,typename T>
class constrained_float{

T m_value;
public :
// check value and throw exception on out of range
static T do_constraint(T const & in)
{
Constraint f;
if( f(in)){
return in;
}
else{
throw std::out_of_range("constrained_float arg range");
}
}
constrained_float(T const & in)
: m_value(do_constraint(in)){}

T get() const {return m_value;}
//equals, +=, -= functions etc

constrained_float& operator =( constrained_float const & in)
{
m_value = in.get();
return *this;
}

constrained_float& operator +=( constrained_float const & in)
{
m_value = do_constraint(m_value + in.get());
return *this;
}
};

//+ - operations etc
template<typename CF,typename T>
constrained_float<CF,Toperator+(
constrained_float<CF,Tconst & lhs,
constrained_float<CF,Tconst & rhs
){
return constrained_float<CF,T>(
constrained_float<CF,T>::do_constraint(
lhs.get() + rhs.get()
)
);
}

// constraint policy
// restrict values to -1 <--+1
struct constraint{

template <typename T>
bool operator()( T const & in)const
{
return ((in >= T(-1)) && (in <= T(1)));
}

};

#include <iostream>

int main()
{
typedef constrained_float<constraint,doublecfloat;

cfloat f = .999;

try{
f += 1.1; // will throw

f = 1.1; // will throw
}
catch (std::exception & e){
std::cout << e.what() <<'\n';
}
}

Jan 19 '07 #10

P: n/a
Thank you all for all the responses, i actually didnt count on it,
however you say that i am not making sence or being specific enough,
and that does not make sence to me.

union Coord_T {
int id;
struct {
double x;
double y;
double z;
};
};

First of all this is just an example to descibe the problem, the actual
problem is somehwat greater:

union Coord_T {
__int64 id; // long long or whatever. main thing is that its 64 bits
struct {
float xc,yc,zc,tc,xa,ya,za,r; // 8 floats 8 * 32 bit = 256 bits
};
};

Regardles of what types im using float vs. double, int vs. __int64/long
long.
The 8 floating point values just wont fit into ID, just as the ealier
example with 3 doubles and 1 int wont work.
I am still thinking about a way to split things up, but at best i will
be split up into 2, e.g. 128 bit needs to be crammed into the space of
64.
Furthermore im not really that interested in using 64 bit integers,
would prefer 32 bit.

I realize that it might be hard to figure out what i am trying to do
here, but i asure you that it is also very hard to explain.

I dont really count anyone giving a solution. It is one of those
problems that just cant be solved in a proper way i think.

Regards

Jan 19 '07 #11

P: n/a
On 2007-01-19 16:48, za******@gmail.com wrote:
Thank you all for all the responses, i actually didnt count on it,
however you say that i am not making sence or being specific enough,
and that does not make sence to me.

union Coord_T {
int id;
struct {
double x;
double y;
double z;
};
};

First of all this is just an example to descibe the problem, the actual
problem is somehwat greater:

union Coord_T {
__int64 id; // long long or whatever. main thing is that its 64 bits
struct {
float xc,yc,zc,tc,xa,ya,za,r; // 8 floats 8 * 32 bit = 256 bits
};
};

Regardles of what types im using float vs. double, int vs. __int64/long
long.
The 8 floating point values just wont fit into ID, just as the ealier
example with 3 doubles and 1 int wont work.
I am still thinking about a way to split things up, but at best i will
be split up into 2, e.g. 128 bit needs to be crammed into the space of
64.
Furthermore im not really that interested in using 64 bit integers,
would prefer 32 bit.

I realize that it might be hard to figure out what i am trying to do
here, but i asure you that it is also very hard to explain.
If you could tell us why you need to do this one of us might be able to
help. Am I correct in understanding that you have redefined the problem
from wanting to have a small FP type to wanting a union with a struct of
8 floats/doubles and another type? Does this other type have to be an
integer of some sort? Could it not be more than one, just like the
floats? Something like this:

union Test
{
float data1[8];
char data2[sizeof(float) * 8];
};

--
Erik Wikström
Jan 19 '07 #12

P: n/a

Erik Wikström skrev:
On 2007-01-19 16:48, za******@gmail.com wrote:
Thank you all for all the responses, i actually didnt count on it,
however you say that i am not making sence or being specific enough,
and that does not make sence to me.

union Coord_T {
int id;
struct {
double x;
double y;
double z;
};
};

First of all this is just an example to descibe the problem, the actual
problem is somehwat greater:

union Coord_T {
__int64 id; // long long or whatever. main thing is that its 64 bits
struct {
float xc,yc,zc,tc,xa,ya,za,r; // 8 floats 8 * 32 bit = 256 bits
};
};

Regardles of what types im using float vs. double, int vs. __int64/long
long.
The 8 floating point values just wont fit into ID, just as the ealier
example with 3 doubles and 1 int wont work.
I am still thinking about a way to split things up, but at best i will
be split up into 2, e.g. 128 bit needs to be crammed into the space of
64.
Furthermore im not really that interested in using 64 bit integers,
would prefer 32 bit.

I realize that it might be hard to figure out what i am trying to do
here, but i asure you that it is also very hard to explain.

If you could tell us why you need to do this one of us might be able to
help. Am I correct in understanding that you have redefined the problem
from wanting to have a small FP type to wanting a union with a struct of
8 floats/doubles and another type? Does this other type have to be an
integer of some sort? Could it not be more than one, just like the
floats? Something like this:

union Test
{
float data1[8];
char data2[sizeof(float) * 8];
};

--
Erik Wikström
I am sorry, but telling what i am doing isnt an option, not because i
dont want to, but im not able to i think.
As for the example posted, i guess it wasnt as understandable as i
thought.
+-----------------------------------------------+
| space in memory |
+-----------------------------------------------+
| 32 or 64 bit integer (preferable 32 bit) |
+-----------------------------------------------+
|float|float|float|float|float|float|float|float|
+-----------------------------------------------+

e.g.
the integertype occupies the same memory allocation as a string og 8
floats.

I fail to see how the union you suggest can help me. I need to cram the
floats into one single var, to get a unique ID dependend on the values
of the floats.

Jan 20 '07 #13

P: n/a
On 2007-01-20 17:54, za******@gmail.com wrote:
Erik Wikström skrev:
>On 2007-01-19 16:48, za******@gmail.com wrote:
Thank you all for all the responses, i actually didnt count on it,
however you say that i am not making sence or being specific enough,
and that does not make sence to me.

union Coord_T {
int id;
struct {
double x;
double y;
double z;
};
};

First of all this is just an example to descibe the problem, the actual
problem is somehwat greater:

union Coord_T {
__int64 id; // long long or whatever. main thing is that its 64 bits
struct {
float xc,yc,zc,tc,xa,ya,za,r; // 8 floats 8 * 32 bit = 256 bits
};
};

Regardles of what types im using float vs. double, int vs. __int64/long
long.
The 8 floating point values just wont fit into ID, just as the ealier
example with 3 doubles and 1 int wont work.
I am still thinking about a way to split things up, but at best i will
be split up into 2, e.g. 128 bit needs to be crammed into the space of
64.
Furthermore im not really that interested in using 64 bit integers,
would prefer 32 bit.

I realize that it might be hard to figure out what i am trying to do
here, but i asure you that it is also very hard to explain.

If you could tell us why you need to do this one of us might be able to
help. Am I correct in understanding that you have redefined the problem
from wanting to have a small FP type to wanting a union with a struct of
8 floats/doubles and another type? Does this other type have to be an
integer of some sort? Could it not be more than one, just like the
floats? Something like this:

union Test
{
float data1[8];
char data2[sizeof(float) * 8];
};

--
Erik Wikström

I am sorry, but telling what i am doing isnt an option, not because i
dont want to, but im not able to i think.
As for the example posted, i guess it wasnt as understandable as i
thought.
+-----------------------------------------------+
| space in memory |
+-----------------------------------------------+
| 32 or 64 bit integer (preferable 32 bit) |
+-----------------------------------------------+
|float|float|float|float|float|float|float|float|
+-----------------------------------------------+

e.g.
the integertype occupies the same memory allocation as a string og 8
floats.
As we both know, that will never happen.
I fail to see how the union you suggest can help me. I need to cram the
floats into one single var, to get a unique ID dependend on the values
of the floats.
Have you thought about using a hash, with my example you could run some
algorithm over the char-array (or convert it to ints or whatever if that
makes the algorithm easier) so instead of having two fields in the
union, one ID and the floats, you have something like I did and a
function that returns the ID.

--
Erik Wikström
Jan 20 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.