CAST: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Initial version)
 
No edit summary
 
Line 1: Line 1:
{{DISPLAYTITLE:_CAST}}
{{DISPLAYTITLE:_CAST}}
The '''_CAST''' pseudo-function performs a C-like cast of a numerical value to a specified numerical type.
The '''_CAST''' function performs a C-like cast of a numerical value to a specified numerical type.




{{PageSyntax}}
{{PageSyntax}}
: {{Parameter|result}} = '''_CAST'''({{Parameter|numericalType}}, {{Parameter|numericalValue}})
: {{Parameter|result}} = [[_CAST]]({{Parameter|numericalType}}, {{Parameter|numericalValue}})




{{PageParameters}}
{{PageParameters}}
* {{Parameter|numericalType}} specifies the target type for the conversion.
* {{Parameter|numericalType}} specifies the target type for the conversion. Accepted types are:
** Accepted types are:
** [[SINGLE]]
*** [[SINGLE]]
** [[DOUBLE]]
*** [[DOUBLE]]
** [[_FLOAT]]
*** [[_FLOAT]]
** [[_BYTE]]
*** [[_BYTE]]
** [[INTEGER]]
*** [[INTEGER]]
** [[LONG]]
*** [[LONG]]
** [[_INTEGER64]]
*** [[_INTEGER64]]
** [[_OFFSET]]
*** [[_OFFSET]]
* The [[_UNSIGNED]] modifier is also allowed and can be combined with the integer types.
** The [[_UNSIGNED]] modifier is also allowed.
* {{Parameter|numericalValue}} is the value to be cast to the specified type.
* {{Parameter|numericalValue}} is the value to be cast to the specified type.


Line 25: Line 24:
* '''_CAST''' allows explicit type conversion, similar to C-style casting.
* '''_CAST''' allows explicit type conversion, similar to C-style casting.
* '''_CAST''' does not round the value like [[INT]], [[_ROUND]], [[CINT]], or [[CLNG]].
* '''_CAST''' does not round the value like [[INT]], [[_ROUND]], [[CINT]], or [[CLNG]].
* The return type is determined by {{Parameter|numericalType}}.
* The {{Parameter|result}} type is determined by {{Parameter|numericalType}}.
* A compiler error is thrown if {{Parameter|numericalType}} is invalid or {{Parameter|numericalValue}} is a [[STRING]].
* A compiler error is thrown if {{Parameter|numericalType}} is invalid or {{Parameter|numericalValue}} is a [[STRING]].
* No runtime errors are thrown if {{Parameter|numericalType}} and {{Parameter|numericalValue}} are valid.
* No runtime errors are thrown if {{Parameter|numericalType}} and {{Parameter|numericalValue}} are valid.
Line 83: Line 82:
Casted: -456
Casted: -456
{{OutputEnd}}
{{OutputEnd}}
----


; Example 2: Type conversion function comparison.
; Example 2: Type conversion function comparison.
Line 107: Line 108:
* [[_ROUND]]
* [[_ROUND]]
* [[Variable Types]]
* [[Variable Types]]
* [[Keyword Reference - Alphabetical]]
* [[Keyword Reference - By usage]]




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 20:20, 8 December 2024

The _CAST function performs a C-like cast of a numerical value to a specified numerical type.


Syntax

result = _CAST(numericalType, numericalValue)


Parameters


Description

  • _CAST allows explicit type conversion, similar to C-style casting.
  • _CAST does not round the value like INT, _ROUND, CINT, or CLNG.
  • The result type is determined by numericalType.
  • A compiler error is thrown if numericalType is invalid or numericalValue is a STRING.
  • No runtime errors are thrown if numericalType and numericalValue are valid.


Availability


Examples

Example 1
Basic type conversion.
DIM a AS DOUBLE
DIM b AS _INTEGER64

a = 123.456
b = _CAST(_INTEGER64, a)

PRINT "Original: "; a
PRINT "Casted: "; b

a = 456.789
b = _CAST(_INTEGER64, a)

PRINT "Original: "; a
PRINT "Casted: "; b

a = -123.456
b = _CAST(_INTEGER64, a)

PRINT "Original: "; a
PRINT "Casted: "; b

a = -456.789
b = _CAST(_INTEGER64, a)

PRINT "Original: "; a
PRINT "Casted: "; b
Original:  123.456
Casted:  123
Original:  456.789
Casted:  456
Original: -123.456
Casted: -123
Original: -456.789
Casted: -456

Example 2
Type conversion function comparison.
PRINT 4.6!; INT(4.6!); FIX(4.6!); CLNG(4.6!); _ROUND(4.6!); _CAST(LONG, 4.6!)
PRINT -4.6!; INT(-4.6!); FIX(-4.6!); CLNG(-4.6!); _ROUND(-4.6!); _CAST(LONG, -4.6!)

PRINT 1%%; _CAST(_UNSIGNED _BYTE, 1%%)
PRINT -1%%; _CAST(_UNSIGNED _BYTE, -1%%)
 4.6  4  4  5  5  4
-4.6 -5 -4 -5 -5 -4
 1  1
-1  255


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link