Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   User defined data type. (http://www.velocityreviews.com/forums/t459911-user-defined-data-type.html)

zacariaz@gmail.com 01-19-2007 12:03 AM

User defined data type.
 
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


Andre Kostur 01-19-2007 12:29 AM

Re: User defined data type.
 
zacariaz@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;

zacariaz@gmail.com 01-19-2007 12:49 AM

Re: User defined data type.
 

Andre Kostur skrev:
> zacariaz@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;



zacariaz@gmail.com 01-19-2007 12:57 AM

Re: User defined data type.
 
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.


Kai-Uwe Bux 01-19-2007 03:26 AM

Re: User defined data type.
 
zacariaz@gmail.com wrote:

>
> Andre Kostur skrev:
>> zacariaz@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

Jim Langston 01-19-2007 03:54 AM

Re: User defined data type.
 
<zacariaz@gmail.com> wrote in message
news:1169168225.744308.81920@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...




Sylvester Hesp 01-19-2007 11:12 AM

Re: User defined data type.
 

"Kai-Uwe Bux" <jkherciueh@gmx.net> wrote in message
news:eopdpo$2v0$3@murdoch.acc.Virginia.EDU...
> zacariaz@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



Kai-Uwe Bux 01-19-2007 11:21 AM

Re: User defined data type.
 
Sylvester Hesp wrote:

>
> "Kai-Uwe Bux" <jkherciueh@gmx.net> wrote in message
> news:eopdpo$2v0$3@murdoch.acc.Virginia.EDU...
>> zacariaz@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

=?iso-8859-1?q?Erik_Wikstr=F6m?= 01-19-2007 11:49 AM

Re: User defined data type.
 
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


kwikius 01-19-2007 02:46 PM

Re: User defined data type.
 
zacariaz@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,T> operator+(
constrained_float<CF,T> const & lhs,
constrained_float<CF,T> const & 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,double> cfloat;

cfloat f = .999;

try{
f += 1.1; // will throw

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



All times are GMT. The time now is 11:51 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.