User defined data type.

Discussion in 'C++' started by zacariaz@gmail.com, Jan 19, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. Andre Kostur Guest

    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;
     
    Andre Kostur, Jan 19, 2007
    #2
    1. Advertising

  3. Guest

    Andre Kostur skrev:
    > 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, 2007
    #3
  4. Guest

    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, 2007
    #4
  5. Kai-Uwe Bux Guest

    wrote:

    >
    > Andre Kostur skrev:
    >> 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
     
    Kai-Uwe Bux, Jan 19, 2007
    #5
  6. Jim Langston Guest

    <> wrote in message
    news:...
    > 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...
     
    Jim Langston, Jan 19, 2007
    #6
  7. "Kai-Uwe Bux" <> wrote in message
    news:eopdpo$2v0$...
    > 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
     
    Sylvester Hesp, Jan 19, 2007
    #7
  8. Kai-Uwe Bux Guest

    Sylvester Hesp wrote:

    >
    > "Kai-Uwe Bux" <> wrote in message
    > news:eopdpo$2v0$...
    >> 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
     
    Kai-Uwe Bux, Jan 19, 2007
    #8
  9. On Jan 19, 1:57 am, 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
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Jan 19, 2007
    #9
  10. kwikius Guest

    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::eek: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';
    }
    }
     
    kwikius, Jan 19, 2007
    #10
  11. Guest

    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, 2007
    #11
  12. On 2007-01-19 16:48, 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
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jan 19, 2007
    #12
  13. Guest

    Erik Wikström skrev:
    > On 2007-01-19 16:48, 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, 2007
    #13
  14. On 2007-01-20 17:54, wrote:
    > Erik Wikström skrev:
    >> On 2007-01-19 16:48, 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
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jan 20, 2007
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oodini
    Replies:
    1
    Views:
    1,779
    Keith Thompson
    Sep 27, 2005
  2. Replies:
    2
    Views:
    428
  3. Replies:
    1
    Views:
    454
    Sylvester Hesp
    May 16, 2007
  4. Replies:
    1
    Views:
    506
    Sion Arrowsmith
    Jul 10, 2008
  5. Lyes Amazouz
    Replies:
    2
    Views:
    287
    Lyes Amazouz
    Aug 20, 2008
Loading...

Share This Page