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.