Results 1 to 14 of 14

Thread: Type Sizes

  1. #1
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default Type Sizes

    I've noticed a lot of our beginner scriptwriters here haven't noticed that types have sizes or how to get these sizes.

    I wrote a small example script to show sizes and what they mean for the people who just wanna get to it and not read, It can be found neer the end.


    You can get the size in bytes for a type doing the following. SizeOf(V);
    Simba Code:
    var
      B: byte;
    begin
      WriteLn(SizeOf(B)); //1
    end.

    You can get the maximum of a unsigned variable doing the following. Pow(2, SizeOf(V) * 8) - 1;
    Simba Code:
    var
      B: byte;
    begin
      WriteLn(Pow(2, SizeOf(B) * 8) - 1); //255
    end.

    You can get the maximum of a signed variable doing the following. (Pow(2, SizeOf(V) * 8) - 2) / 2;
    Note: Add 1 and negate for Minimum.
    Simba Code:
    var
      B: byte;
    begin
      WriteLn((Pow(2, SizeOf(B) * 8) - 2) / 2); //127
    end.


    Byte
    Size: 1 byte
    Range: 0..255

    ShortInt
    Size: 1 byte
    Range: -128..127

    Word
    Size: 2 bytes
    Range: 0..65535

    SmallInt
    Size: 2 bytes
    Range: -32768..32767

    Cardinal (AKA LongWord)
    Size: 4 bytes
    Range: 0..4294967295

    Integer
    Size: 4 bytes
    Range: -2147483648..2147483647


    This next ones are not that important but ill include them =)

    Single
    Size: 4 bytes
    Range: 1.5x10**-45..3.4x10**38

    Double
    Size: 8 bytes
    Range: 5.0x10**-324..1.7x10**308

    Extended
    Size: 10 bytes
    Range: 3.6x10**-4951..1.1x10**4932


    Simba Code:
    program _;

    function PadC(s: string; maxl: integer): string;
    begin
      Result := s;
      while (Length(Result) < maxl) do
      begin
        Result := ' ' + Result + ' ';
        if (Length(Result) = maxl - 1) then
          Result := ' ' + Result;
      end;
    end;

    function DebugType(Name: string; Size: integer; Unsigned: boolean): string;
    var
      MaxS: Extended;
    begin
      Result := '|' + PadC(Name, 12) + '|';

      if (Size > 1) then
        Result := Result + PadC(IntToStr(Size) + ' bytes', 10) + '|'
      else
        Result := Result + PadC(IntToStr(Size) + ' byte', 10) + '|';

      MaxS := Pow(2, Size * 8) - 1;
      if (not (Unsigned)) then
      begin
        MaxS := (MaxS - 1) / 2;
        Result := Result + PadC('-' + FloatToStr(MaxS + 1) + '..' + FloatToStr(MaxS), 27) + '|';
      end else
        Result := Result + PadC('0..' + FloatToStr(MaxS), 27) + '|';
    end;

    var
      B: Byte;
      SI: ShortInt;
      W: Word;
      SmI: SmallInt;
      C: Cardinal;
      I: Integer;

    begin
      WriteLn('|    Type    |   Size   |            Range          |');
      WriteLn(DebugType('Byte', SizeOf(B), True));
      WriteLn(DebugType('ShortInt', SizeOf(SI), False));
      WriteLn(DebugType('Word', SizeOf(W), True));
      WriteLn(DebugType('SmallInt', SizeOf(SmI), False));
      WriteLn(DebugType('Cardinal', SizeOf(C), True));
      WriteLn(DebugType('Integer', SizeOf(I), False));
    end.

    Small Note: The script uses to function that asks for "Unsigned", first if a number is signed, 1 bit is taken and the number can be negative or positive. If its Unsigned the number goes from 0 to its maximum. As you can see above Byte and ShortInt have the same Size but diffrent Ranges, this is because Byte is unsigned.
    Last edited by Dgby714; 05-10-2011 at 03:05 AM.

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  2. #2
    Join Date
    Feb 2007
    Posts
    849
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    This is very helpful actually.
    Would you add in the sizes for integer, string (I'm sure there gonna be a character limit)

    Is char even supported?
    ________________________________________
    14:19 < cycrosism> I wonder what she would have done without it
    14:19 < cycrosism> without me*
    Cycrosism is now an it.
    Quote Originally Posted by Dervish View Post
    /Facedesk.

  3. #3
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Quote Originally Posted by HarryJames View Post
    This is very helpful actually.
    Would you add in the sizes for integer, string (I'm sure there gonna be a character limit)

    Is char even supported?
    Char = ShortInt;
    Integer is already there.
    String is basically an array of char so its only limited by memory.

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  4. #4
    Join Date
    Feb 2007
    Posts
    849
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Thanks for char/string.
    I didn't see integer - I'm on my iPod and very tired. I've got a full day of sixthform to start in an hour
    ________________________________________
    14:19 < cycrosism> I wonder what she would have done without it
    14:19 < cycrosism> without me*
    Cycrosism is now an it.
    Quote Originally Posted by Dervish View Post
    /Facedesk.

  5. #5
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Quote Originally Posted by HarryJames View Post
    Thanks for char/string.
    I didn't see integer - I'm on my iPod and very tired. I've got a full day of sixthform to start in an hour
    It's there, but here,

    Integer
    Size: 4 bytes
    Range: -2147483648..2147483647

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  6. #6
    Join Date
    Jun 2007
    Location
    La Mirada, CA
    Posts
    2,484
    Mentioned
    1 Post(s)
    Quoted
    3 Post(s)

    Default

    I never knew we even had sizeof(), not that I would have ever used it...interesting though.

    "Failure is the opportunity to begin again more intelligently" (Henry Ford)


  7. #7
    Join Date
    Jan 2010
    Posts
    5,227
    Mentioned
    6 Post(s)
    Quoted
    60 Post(s)

    Default

    I assume you were inspired by C?

    Also, I had no idea PascalScript had a SizeOf operator. I assume it's actually a function and not a unary operator like in C though.

    Reminds me that there's not a whole lot of information about why things work, rather than how to make them work. Makes me sad. :/

    Rep++.

  8. #8
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Quote Originally Posted by i luffs yeww View Post
    I assume you were inspired by C?

    Also, I had no idea PascalScript had a SizeOf operator. I assume it's actually a function and not a unary operator like in C though.

    Reminds me that there's not a whole lot of information about why things work, rather than how to make them work. Makes me sad. :/

    Rep++.
    Noticed it when doing some C->Pascal work and playing with the Windows API in Simba =)

    I've known about SizeOf for a long time tho I never knew what unsigned meant until learning C.

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  9. #9
    Join Date
    Jan 2010
    Posts
    5,227
    Mentioned
    6 Post(s)
    Quoted
    60 Post(s)

    Default

    C -> Pascal? Windows API? D: For what/why?

    Anyway, does Pascal have unsigned types? More importantly, does PascalScript?

  10. #10
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Quote Originally Posted by i luffs yeww View Post
    C -> Pascal? Windows API? D: For what/why?

    Anyway, does Pascal have unsigned types? More importantly, does PascalScript?
    Yes but you cant like specify var name: unsigned integer; there named differently like a Cardinal is a unsigned integer.

    BTW been playing with the Windows API can can now move the mouse and get mouse position in C. (Tho only in windows)

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  11. #11
    Join Date
    Jan 2010
    Posts
    5,227
    Mentioned
    6 Post(s)
    Quoted
    60 Post(s)

    Default

    All you have to do is #include <windows.h> isn't it?

    Anyway, not on the right thread for this discussion imo.

  12. #12
    Join Date
    Jan 2007
    Posts
    8,876
    Mentioned
    123 Post(s)
    Quoted
    327 Post(s)

    Default

    SizeOf() Also works on custom types

    Simba Code:
    type
      TItem = record
        Name      : string;
        StatsName : string;
        UpText    : TStringArray;
        InvSlot   : Integer;
        Stackable : Boolean;
        EquipLevel: Integer;
        DTM       : Integer;
      end;

    var
      B : TItem;

    begin
      WriteLn(SizeOf(B));
    end.

  13. #13
    Join Date
    Jan 2010
    Posts
    5,227
    Mentioned
    6 Post(s)
    Quoted
    60 Post(s)

    Default

    Quote Originally Posted by Zyt3x View Post
    SizeOf() Also works on custom types
    Well that makes sense, since custom types are just normal types, really. What would the size of that be, though? Not sure what size uninitialized strings and TStringArrays would be, so I really have no idea. I was guessing 16 (which would make strings and TStringArrays one byte?), but I don't know.

  14. #14
    Join Date
    Jan 2011
    Location
    Denver, CO
    Posts
    1,351
    Mentioned
    2 Post(s)
    Quoted
    72 Post(s)

    Default

    Quite an interesting read, thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •