Thread: Data Types Dammit!

  1. #1
    Seba
    Guest

    Data Types Dammit!

    Yeah someone tell me how to define a new data type!!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The only new types you can create are structures (and less frequently unions).

    Like so...
    Code:
    struct foo {
      char name[30];
      int age;
    };
    And you would create a variable of that type by
    Code:
      struct foo student;
      struct foo all_students[100];
    Now you can hide some of the detail by using a typedef, but that doesn't create anything new, it just creates a new way to refer to something which already exists.

    Code:
    typedef struct foo foo_st;
    The two variables above can be expressed in exactly the same way by doing this...
    Code:
      foo_st student;
      foo_st all_students[100];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >€&<>&2Minimization of boolean functions, PROM,PLA design >‚&0>ƒ&WA, USA guitar, dogsCommercial Aviation >„&>>…&USAProgramming
    Posts
    742
    A user defined data type must first be declared in a structure.
    Code:
    struct label
    {
      int iNum;
      doulbe dNum;
      char cName[20];
    };
    This is a global declaration. It goes after your #include headers. In main() you can define an instance of the user defined type.

    Code:
    int main()
    {
       struct label sRecord;
       ...
       sRecord.iNum = 100;
       printf("iNum member is %d", sRecord.iNum);
       _getch();
       return 0;
    }
    Last edited by Witch_King; 08-12-2001 at 03:35 PM.
    I compile code with:
    Visual Studio.NET beta2

  4. #4
    Registered User Fool's Avatar
    Join Date
    Aug 2001
    Posts
    335

    ...

    That's not exactly what I mean by "new data type".

    What I would like to do is to define a data type that is essentially much bigger than a float, double, etc.

    So instead of :

    float myFloat = 1.0825;

    I could do :

    greatbig myGreatBigNum = 1.0825857346906234562134567879548743234;

    ...and so on defining new variables of type "greatbig" as needed.

    I could then perform more precise calculations than are possible with C's standard numerical datatypes.

  5. #5
    Registered User Fool's Avatar
    Join Date
    Aug 2001
    Posts
    335
    Um?

    I didn't type that, I know nothing about C++ programming. Must be an error or something.

    -Fool

  6. #6
    Registered User Fool's Avatar
    Join Date
    Aug 2001
    Posts
    335

    I AM SEBASTIAN!!!

    For some reason, the message board is taking me for someone else!!! I have unwillingly stolen someone elses Identity!!!

  7. #7
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >€&<>&2Minimization of boolean functions, PROM,PLA design >‚&0>ƒ&WA, USA guitar, dogsCommercial Aviation >„&>>…&USAProgramming
    Posts
    742
    This new board has some bugs that are being worked out.

    My suggestion to the problem would be to wait for I64 technology which will facilitate new compilers with larger 'built-in' types or 'primitives'. This will happen quite soon.
    I compile code with:
    Visual Studio.NET beta2

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    **TEST**

    ...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Are you asking how to making a primitive that is larger than a double? If so..you can't. I have never personally tried this but maybe you can make something like fixed point numbers but use a double for extra bits. But that may be more work than it is worth.

  10. #10
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You could create your own data type. If memory isn't a concern you could use a byte sized type (char)for each decimal place, or pack the decimal places within a byte.

    You would then have to create your own functions to input/output these values and for adding, subtracting, multiplying etc.

    Using this method would be considerably slower than using a primitive so unless you've got a very good reason I wouldn't bother.

  11. #11
    aurë entuluva! mithrandir's Avatar
    Join Date
    Aug 2001
    Posts
    1,209
    I could then perform more precise calculations than are possible with C's standard numerical datatypes.

    That's not really possible, as numerical values are never 100% accurate on computers anyway. Using double or long is your best bet for very large values. You won't ever get an exact number even then. I don't see how you could create a new data type that is not specified in C already.

    My suggestion to the problem would be to wait for I64 technology which will facilitate new compilers with larger 'built-in' types or 'primitives'. This will happen quite soon.
    64-bit processing will certainly allow twice the current memory allocation allowed by CPUs, but floating point numbers would still be imprecise (as you have to round of some time or it will continue forever).

  12. #12
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435

    take a look at...

    the large int case study for AP cs.

    you can, but it'll have to be through classes & c++.
    .sect signature

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > greatbig myGreatBigNum = 1.0825857346906234562134567879548743234;
    As I seem to have said this already several times in reply to other posters, I'll just say it again.

    No there are no extended data types for numeric data, but there's nothing stopping you implementing a library.

    Here's one example
    http://www.swox.com/gmp/
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >€&<>&2Minimization of boolean functions, PROM,PLA design >‚&0>ƒ&WA, USA guitar, dogsCommercial Aviation >„&>>…&USAProgramming
    Posts
    742
    Geez, I think I might have to give this URL out to my teachers at DeVRY so that they can reccomend students to post their quesitons here.
    I compile code with:
    Visual Studio.NET beta2

  15. #15
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Geez, I think I might have to give this URL out to my teachers at DeVRY so that they can reccomend students to post their quesitons here.
    You evil, evil little man.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extending basic data types.
    By nempo in forum C++ Programming
    Replies: 23
    Last Post: 09-25-2007, 03:28 PM
  2. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. Using enumerated data types
    By SXO in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2001, 06:26 PM