Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > User defined data type.

Reply
Thread Tools

User defined data type.

 
 
zacariaz@gmail.com
Guest
Posts: n/a
 
      01-19-2007
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

 
Reply With Quote
 
 
 
 
Andre Kostur
Guest
Posts: n/a
 
      01-19-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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;
 
Reply With Quote
 
 
 
 
zacariaz@gmail.com
Guest
Posts: n/a
 
      01-19-2007

Andre Kostur skrev:
> (E-Mail Removed) 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;


 
Reply With Quote
 
zacariaz@gmail.com
Guest
Posts: n/a
 
      01-19-2007
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.

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      01-19-2007
(E-Mail Removed) wrote:

>
> Andre Kostur skrev:
>> (E-Mail Removed) 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
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      01-19-2007
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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...



 
Reply With Quote
 
Sylvester Hesp
Guest
Posts: n/a
 
      01-19-2007

"Kai-Uwe Bux" <(E-Mail Removed)> wrote in message
news:eopdpo$2v0$(E-Mail Removed)...
> (E-Mail Removed) 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


 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      01-19-2007
Sylvester Hesp wrote:

>
> "Kai-Uwe Bux" <(E-Mail Removed)> wrote in message
> news:eopdpo$2v0$(E-Mail Removed)...
>> (E-Mail Removed) 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
 
Reply With Quote
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      01-19-2007
On Jan 19, 1:57 am, (E-Mail Removed) 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

 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      01-19-2007
(E-Mail Removed) 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:ut_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';
}
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: __STDC_IEC_559__ (defined or !defined ?) Keith Thompson C Programming 0 08-17-2010 04:36 PM
User-defined exception: "global name 'TestRunError' is not defined" jmike@alum.mit.edu Python 1 07-10-2008 12:37 PM
defined? for recursive function call v/s defined? for function call stack Alok Ruby 3 04-13-2006 11:53 AM
Using parenthesis with defined (#if defined(...)) Angel Tsankov C++ 1 04-05-2006 10:00 PM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM



Advertisments