FB's numeric data-type(s)

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
MrSwiss
Posts: 3796
Joined: Jun 02, 2013 9:27
Location: Switzerland

FB's numeric data-type(s)

Postby MrSwiss » Jan 26, 2021 0:05

This is related to all none-string related variables, available in FreeBASIC.

Code to show things, rather than hundreds or thousands of words:

Code: Select all

' FB_num_data-types.bas -- (c) 2021-01-26, MrSwiss

' demo code start
Dim As String   t = "Numeric data-types and their sizes, in Byte(s) & bits", _
                cmsg(1) = { "FBC x64 used!", "FBC x32 used!" }

Print t : Print String(Len(t), "~") : Print         ' title | underline | LF

Print "whole numbers (fixed size)"
Print SizeOf(UByte);    " Byte",  SizeOf(Byte)    * 8; "  bits (U/Byte)"
Print SizeOf(UShort);   " Bytes", SizeOf(Short)   * 8; " bits (U/Short)"
Print SizeOf(ULong);    " Bytes", SizeOf(Long)    * 8; " bits (U/Long)"
Print SizeOf(ULongInt); " Bytes", SizeOf(LongInt) * 8; " bits (U/LongInt)"
Print

Print "floating point numbers (fixed size)"
Print SizeOf(Single); " Bytes",   SizeOf(Single)  * 8; " bits (Single)"
Print SizeOf(Double); " Bytes",   SizeOf(Double)  * 8; " bits (Double)"
Print

Print "whole numbers (variable size / compiler bitness / pointer size)"
Print SizeOf(UInteger); " Bytes", SizeOf(Integer) * 8; " bits (U/Integer)"
Print

Print IIf(SizeOf(Any Ptr) = 8, cmsg(0), cmsg(1))    ' instant If instead of: _
Print                                               ' If ... Then ,,, Else ...

Sleep
' ----- EOF -----
Run the code in 32 and 64 bit compilers of FB.
You'll see the difference yourself.
Comments in code may clarify issues ...
MrSwiss
Posts: 3796
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FB's numeric data-type(s)

Postby MrSwiss » Apr 08, 2021 15:31

Here is a addendum to above which explains how U/Integer data-types can be defined.
In order to do so they must be #undef (undefined) first.
The whole exercise is pretty usless except for DEMO-PURPOSE (educational info).

Code: Select all

' (c) 2021-04-08, MrSwiss

' delete Integer and UInteger data-types
#Undef Integer
#Undef UInteger
' they are now undefined (not any longer available)
#Ifndef Integer                         ' prove undefined
#Ifndef UInteger                        ' prove undefined
' redefine them depending on compiler bitness
#Ifdef __FB_64BIT__                    ' FBC x64 (detected)
   #Define Integer       LongInt
   #Define UInteger    ULongInt
#Else                                   ' FBC x32
   #Define Integer       Long
   #Define UInteger    ULong
#EndIf  ' __FB_64BIT__ (innermost condition terminates first)
' prove that a var-size type can be made with fix-size type's
#EndIf  ' #Ifndef UInteger
#EndIf  ' #Ifndef Integer
' now: everything is back to square A1 (as at start)


Print "FBC x"; Str(SizeOf(Any Ptr) * 8) + _
      "  ver. " + __FB_VERSION__
Print
Print "Integer  size:"; SizeOf(Integer);  " byte(s)"
Print "UInteger size:"; SizeOf(UInteger); " byte(s)"

Sleep
As above: read comments in code for more detail ...
dodicat
Posts: 6914
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FB's numeric data-type(s)

Postby dodicat » Apr 08, 2021 17:57

Hi MrSwiss
You missed out boolean.
A bit of the help file description:
...
A more realistic definition is that the boolean data type is a 1-bit integer, having the value 0 to indicate False and 1 to indicate True.
...

So it could probably be considered numeric, without recourse to the numeric operators.
MrSwiss
Posts: 3796
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FB's numeric data-type(s)

Postby MrSwiss » Apr 09, 2021 1:45

Hi dodicat
You missed out boolean.
Yes, correct but I consider it a 'hybrid data-type' (not strictly numeric only).
Since, if we simply state Print flag when flag dim(med) as Boolean the return is:
either true or false which is the respective Const 'identifier' (aka: name).

What i mostly dislike in the purely *theoretical* description is: "... the boolean data type is a 1-bit integer ..."
Reasons:
  • there isn't something like a single bit of anything (in any programming language I know of)
  • this gives an erroneous assumption to the user, that single bit's exist and can be used
  • minimal size of memory allocation is: 1 byte (8 bits) = SizeOf(Boolean)
I prefer a more practice oriented approach to things.


The code tries to explain the intricacies of Boolean variable implementation (FB specific).
Aka: the way FB 'sees it' vs. the way C 'sees it' (for C-libraries compatibilty).

Code: Select all

' Boolean-test1.bas -- (c) 2021-04-09, MrSwiss

#Undef FALSE                            ' get rid of it (to define it again)
#Undef TRUE                             ' get rid of it (dito)

#Ifndef FALSE                           ' if not defined (only)
#Ifndef TRUE                            ' if not defined (only)

Const As Byte   FALSE = 0               ' aka:  0, &b00000000 (8 bits)
Const As Byte   TRUE  = Not FALSE       ' aka: -1, &b11111111 (8 bits)

#EndIf  ' TRUE
#EndIf  ' FALSE
' now: back to as initially defined (the BOOLEAN)


Dim As Boolean  flag                    ' same size as a signed byte
Dim As Byte Ptr pflg = @flag            ' proves the byte argumentation

Print "Boolean FALSE: '"; flag; Tab(22); _
      "' FB's print output (FALSE Const)"
Print "Boolean FALSE: "; CByte(flag); Tab(22); _    ' proves the byte argument too
      "  FB's numeric output (CByte(Boolean))"
Print "Boolean FALSE: "; *pflg; Tab(22); _          ' proves the *(Byte Ptr) argument too
      "  C's numeric 'behind the scenes' value *(Byte Ptr)"
flag = Not flag : Print                 ' only works with 0/-1 values (in FB only)
Print "Boolean TRUE:  '"; flag; Tab(22); _
      "' FB's print output (TRUE const)"
Print "Boolean TRUE:  "; CByte(flag); Tab(22); _    ' proves the byte argument too
      "  FB's numeric output (CByte(Boolean))"
Print "Boolean TRUE:  "; *pflg; Tab(22); _          ' proves the *(Byte Ptr) argument too
      "  C's numeric 'behind the scenes' value *(Byte Ptr)"
Print : Print
Print "... done ... ";

Sleep
Again, more details in code's comments.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 5 guests