Difference between revisions of "Variable Types"

From Flowcode Help
Jump to navigationJump to search
Line 125: Line 125:
 
|-
 
|-
 
| scope="col" width="50%"| Bit Depth:
 
| scope="col" width="50%"| Bit Depth:
| Array of 8-bit integers
+
| 8-bit array of integers. Default size of the array is 20 but can be adjusted as required.
 
|}
 
|}
  
Line 133: Line 133:
  
 
Strings are automatically terminated with a 0 or null byte, this indicates the end of valid data when using string functions.
 
Strings are automatically terminated with a 0 or null byte, this indicates the end of valid data when using string functions.
 +
 +
String is a typecasting keyword in Flowcode and so cannot be used for variable, property or component names.
 +
 +
Numeric values and variables can easily be converted to a string by using the keyword sting before the variable.
 +
 +
e.g.  StringVar1 = "Temperature: " + string IntVar1
 +
 +
Details on the string functions are available here.
  
  
Line 150: Line 158:
 
The range of a single-precision floating-point number is approximately ±1.175494 × 10−38 to ±3.402824 × 1038.  
 
The range of a single-precision floating-point number is approximately ±1.175494 × 10−38 to ±3.402824 × 1038.  
  
Floating point single-precision numbers are 32-bit approximations of real numbers.
+
Floating point single-precision numbers are 32-bit approximations of real numbers which can represent a much wider range of values than an integer can, but at a loss of accuracy over large ranges.
  
 
Floating point calculations require additional maths libraries, by default calculations will only be 16-bit integer. By using a floating point variable the additional libraries will be used.
 
Floating point calculations require additional maths libraries, by default calculations will only be 16-bit integer. By using a floating point variable the additional libraries will be used.
Line 158: Line 166:
 
To tell the C compiler the constant value is floating point simply use for example 1.0 as your constant.
 
To tell the C compiler the constant value is floating point simply use for example 1.0 as your constant.
  
==Floating point==
+
Float is a typecasting keyword in Flowcode and so cannot be used for variable, property or component names.
A floating point value can represent a much wider range of values than an integer can, but at a loss of accuracy over large ranges. Floating point values when downloaded will be 64-bit if the target supports them, or 32-bit if it does not.
 
 
 
===FLOAT===
 
{|
 
|-
 
| scope="col" width="50%"| Icon:
 
| [[File:VarFloatThumb.png|Image]]
 
|-
 
| scope="col" width="50%"| Range:
 
| -Infinite to +Infinite
 
|-
 
| scope="col" width="50%"| Bit Depth:
 
| 32-bit signed
 
|}
 
 
 
  
Float is a keyword in Flowcode and so cannot be used for variable, property or component names.
 
  
 
Mathematical operations such as a = b + c will be processed as an integer calculation if a, b or c are integer values.  
 
Mathematical operations such as a = b + c will be processed as an integer calculation if a, b or c are integer values.  
  
 
for example you would expect 2.5 * 2 to equal 5 but in the world of integers the calculation only sees 2.5 * 2 = 4, the real portion of the number is lost.
 
for example you would expect 2.5 * 2 to equal 5 but in the world of integers the calculation only sees 2.5 * 2 = 4, the real portion of the number is lost.
 +
 +
To fix this you must instead use 2.5 * 2.0 = 5
  
  
Line 185: Line 179:
  
 
e.g.  FloatVar1 = FloatVar2 + float IntVar1
 
e.g.  FloatVar1 = FloatVar2 + float IntVar1
 
 
Did you know numeric constants such as 35 are also considered as integer values.
 
  
 
Using the float keyword or changing the constant value to for example 35.0 would force the value to be recognised as a floating point type variable.
 
Using the float keyword or changing the constant value to for example 35.0 would force the value to be recognised as a floating point type variable.
 
==Special types==
 
Flowcode also supports some structured types to help with programs.
 
 
===STRING===
 
{|
 
|-
 
| scope="col" width="50%"| Icon:
 
| [[File:VarStringThumb.png|Image]]
 
|-
 
| scope="col" width="50%"| Range:
 
| 0 to 255 per character
 
|-
 
| scope="col" width="50%"| Bit Depth:
 
| 8-bit unsigned array. Default size of the array is 20.
 
|}
 
 
 
A string is a byte-array used to store text. Hence in many circumstances a string and a byte array are interchangeable.
 
 
 
One important difference is in Flowcode strings terminate at a null (0x00) character. This means the first zero-value byte that is encountered in the string marks the end of the string. If a string contains the exact number of characters specified in the declaration it will not contain a terminating zero, in all other cases it will. Not leaving room in the string variable for the null character can be dangerous as it may potentially allow unwanted behaviour such as reading/writing RAM address space of other variables or stack.
 
 
 
String is a keyword in Flowcode and so cannot be used for variable, property or component names.
 
 
Numeric values and variables can easily be converted to a string by using the keyword sting before the variable.
 
 
e.g.  StringVar1 = "Temperature: " + string IntVar1
 
  
  
Line 233: Line 195:
 
| 32-bit unsigned value
 
| 32-bit unsigned value
 
|}
 
|}
 +
 
An object handle is used to reference a more complicated piece of data (such as a file, Flowcode component or block of text) whose internal format is not known. Flowcode provides many [[API Overview|simulation macros]] that use handles to easily pass data around.
 
An object handle is used to reference a more complicated piece of data (such as a file, Flowcode component or block of text) whose internal format is not known. Flowcode provides many [[API Overview|simulation macros]] that use handles to easily pass data around.
 
  
 
In an embedded context, a handle can be thought of as a pointer to memory, except Flowcode manages the handles so referencing a non-existent handle will not cause any fatal errors.
 
In an embedded context, a handle can be thought of as a pointer to memory, except Flowcode manages the handles so referencing a non-existent handle will not cause any fatal errors.
 
  
 
A handle value of zero is considered ''null'' and invalid to Flowcode.
 
A handle value of zero is considered ''null'' and invalid to Flowcode.

Revision as of 15:30, 13 November 2024

Gen Create a New Variable Window.png

A variable type defines values what can be represented by that variable, as well as the amount of memory the variable will occupy.


Integer types

BOOL

Icon: Image
Range: 0 or 1
Bit Depth: 1-bit unsigned integer

A Boolean value is either false or true. In Flowcode, this means 0 or 1, respectively. On download to an embedded target, this may take up 1 byte or a single bit of a byte, depending on the compiler and the optimisation level.

Boolean values are used typically as flags to indicate something needs to happen, has happened, or to control a function.


BYTE

Icon: Image
Range: 0 to 255
Bit Depth: 8-bit unsigned integer

A byte is the smallest storage unit most processors are able to read from in a single instruction.

A byte can be used to store a single value or its bits may be used as flags, almost like a compact array of BOOLs.

The value stored in a byte can only be positive.


INT

Icon: Image
Range: -32768 to 32767
Bit Depth: 16-bit signed integer

A signed int uses the top most bit of the binary value to indicate a positive or negative value, 0b0XXXXXXXXXXXXXXX being a positive value and 0b1XXXXXXXXXXXXXXX being a negative value.

The negative values are stored in 2's compliment form with 0b1000000000000000 = -32768 and 0b11111111111111111 = -1


UINT

Icon: Image
Range: 0 to 65535
Bit Depth: 16-bit unsigned integer

An unsigned value allows for the use of all of the bits allowing for a positive value with twice the range as the signed counterpart.


LONG

Icon: Image
Range: -21474836648 to 21474836647
Bit Depth: 32-bit signed integer

A 32-bit integer is useful for processing larger values and most integer-arithmetic uses found in embedded devices.

A signed long uses the top most bit of the binary value to indicate a positive or negative value. See INT for details.

32-bit calculations require additional maths libraries, by default calculations will only be 16-bit. By using a 32-bit variable the additional libraries will be used.


ULONG

Icon: Image
Range: 0 to 4294967295
Bit Depth: 32-bit unsigned integer

An unsigned value allows for the use of all of the bits allowing for a positive value with twice the range as the signed counterpart.

32-bit calculations require additional maths libraries, by default calculations will only be 16-bit. By using a 32-bit variable the additional libraries will be used.


STRING

Icon: Image
Range: 0 to 255
Bit Depth: 8-bit array of integers. Default size of the array is 20 but can be adjusted as required.

A string consists of an array of 8-bit values, usually containing values with ASCII encoding which maps alphanumeric characters to the numeric values.

For example "Hello" is the same as {72,101,108,108,111,0}

Strings are automatically terminated with a 0 or null byte, this indicates the end of valid data when using string functions.

String is a typecasting keyword in Flowcode and so cannot be used for variable, property or component names.

Numeric values and variables can easily be converted to a string by using the keyword sting before the variable.

e.g. StringVar1 = "Temperature: " + string IntVar1

Details on the string functions are available here.


FLOAT

Icon: Image
Range: ±1.175494 × 10−38 to ±3.402824 × 1038
Bit Depth: 32-bit floating point

The range of a single-precision floating-point number is approximately ±1.175494 × 10−38 to ±3.402824 × 1038.

Floating point single-precision numbers are 32-bit approximations of real numbers which can represent a much wider range of values than an integer can, but at a loss of accuracy over large ranges.

Floating point calculations require additional maths libraries, by default calculations will only be 16-bit integer. By using a floating point variable the additional libraries will be used.

When defining floating point constants e.g. 1, it is important to let the C compiler know that the value is intended for use as a floating point value otherwise an integer calculation will take precedent.

To tell the C compiler the constant value is floating point simply use for example 1.0 as your constant.

Float is a typecasting keyword in Flowcode and so cannot be used for variable, property or component names.


Mathematical operations such as a = b + c will be processed as an integer calculation if a, b or c are integer values.

for example you would expect 2.5 * 2 to equal 5 but in the world of integers the calculation only sees 2.5 * 2 = 4, the real portion of the number is lost.

To fix this you must instead use 2.5 * 2.0 = 5


The keyword float can be used to force a calculation to be done using floating point maths instead of integer maths.

e.g. FloatVar1 = FloatVar2 + float IntVar1

Using the float keyword or changing the constant value to for example 35.0 would force the value to be recognised as a floating point type variable.


HANDLE

Icon: Image
Range: Undefined
Bit Depth: 32-bit unsigned value

An object handle is used to reference a more complicated piece of data (such as a file, Flowcode component or block of text) whose internal format is not known. Flowcode provides many simulation macros that use handles to easily pass data around.

In an embedded context, a handle can be thought of as a pointer to memory, except Flowcode manages the handles so referencing a non-existent handle will not cause any fatal errors.

A handle value of zero is considered null and invalid to Flowcode.