Data Type Conversion

This AeroScript program shows examples of data type conversions.

// Data Type Conversion Example:
// Demonstrates the built-in data types (integer, real, string, axis)
// and how to perform various data type conversions.

program

	// Disable all axes.
	Disable([X, Y, Z])

	// Declare variables of different types and initialize the values.
	var $myAxis as axis = X
	var $myInteger as integer = 5
	var $myReal as real = 72.50
	var $myString as string = "This is a string"

	// Convert an axis variable to a string and display it.
	AppMessageDisplay(AxisToString($myAxis))

	// Convert an integer variable to a string and display it.
	AppMessageDisplay(IntegerToString($myInteger))

	// Convert a real (floating point) variable to a string and display it.
	AppMessageDisplay(RealToString($myReal))

	// Display a string variable directly.
	AppMessageDisplay($myString)


	// Convert a string to a real (floating point) variable.
	$myReal = StringToReal("123.4")
	if ($myReal == 123.4)
		AppMessageDisplay("Successful string to real conversion.")
	end

	// Convert a string to an integer variable.
	$myInteger = StringToInteger("995")
	if ($myInteger == 995)
		AppMessageDisplay("Successful string to integer conversion.")
	end

	// Convert a string that represents an axis name to an axis variable.
	// In this case, the Y axis will be enabled and then disabled.
	$myString = "Y"
	$myAxis = @$myString
	Enable($myAxis)
	Dwell(2.0)
	Disable($myAxis)
	Dwell(1.0)

	// Convert an integer that represents an axis index to an axis variable.
	// In this case, axis index 2 (3rd axis) will be enabled and then disabled.
	$myInteger = 2
	$myAxis = @$myInteger
	Enable($myAxis)
	Dwell(2.0)
	Disable($myAxis)
	Dwell(1.0)

	// Assign an integer variable to a real (floating point) variable directly.
	// Because there is no loss of precision, this assignment will work without error.
	$myInteger = 890
	$myReal = $myInteger
	if ($myReal == 890.0)
		AppMessageDisplay("Successful integer to real assignment.")
	end

	// Assign a real (floating point) variable to an integer variable directly.
	// Because there is no fractional component, this assignment will work without error.
	// If there was a fractional component, the assignment below would cause a task error.
	$myReal = 741.0
	$myInteger = $myReal
	if ($myInteger == 741)
		AppMessageDisplay("Successful real to integer assignment.")
	end

	// Safely truncate a real (floating point) variable into its integer form
	// and its fractional form.
	// Then assign to an integer variable directly.
	$myInteger = Trunc(567.8)
	$myReal = Frac(567.8)
	AppMessageDisplay("Truncated value = " + IntegerToString($myInteger))
	AppMessageDisplay("Fractional value = " + RealToString($myReal))

	// Round real (floating point) value - positive value.
	// Rounding rounds to the nearest integer, away from zero.
	$myInteger = Round(234.6)
	if ($myInteger == 235)
		AppMessageDisplay("Successful real fractional value (positive value).")
	end

	// Round real (floating point) value - negative value.
	// Rounding rounds to the nearest integer, away from zero.
	$myInteger = Round(-234.6)
	if ($myInteger == -235)
		AppMessageDisplay("Successful real fractional value (negative value).")
	end


	var $ceilValue as integer
	var $floorValue as integer

	// Take the ceiling and floor of a real (floating point) value - positive value.
	// Ceil rounds up in the positive direction to the next integer.
	// Floor rounds down in the negative direction to the next integer.
	$ceilValue = Ceil(630.3)
	$floorValue = Floor(630.3)
	if (($ceilValue == 631) && ($floorValue == 630))
		AppMessageDisplay("Successful ceiling/floor conversion (positive value).")
	end

	// Take the ceiling of a real (floating point) value - negative value.
	// Ceil rounds up in the positive direction to the next integer.
	// Floor rounds down in the negative direction to the next integer.
	$ceilValue = Ceil(-630.3)
	$floorValue = Floor(-630.3)
	if (($ceilValue == -630) && ($floorValue == -631))
		AppMessageDisplay("Successful ceiling/floor conversion (negative value).")
	end

	// Convert a real (floating point) variable to a positive value by taking the
	// absolute value.
	$myReal = Abs(-555.5)
	AppMessageDisplay("Absolute value is " + RealToString($myReal))

end