In Pascal, the float data type is not available by default. Pascal primarily provides real and double data types for representing floating-point numbers.
The Real Data Type
The real data type in Pascal is used to represent single-precision floating-point numbers. It occupies 4 bytes (32 bits) of memory and can store values with a precision of approximately 7 decimal digits. The range of values that can be represented by the real data type depends on the specific implementation of Pascal.
You can declare a variable of the real data type using the following syntax:
var myReal: real;
To assign a value to a real variable, you can use the assignment operator (
:) or initialize it directly during declaration:
myReal := 3.14; myReal: = 1 / 3;
The Double Data Type
The double data type, also known as “extended” or “real” in some Pascal implementations, provides higher precision than the real data type. It occupies 8 bytes (64 bits) of memory and can store values with a precision of approximately 15 decimal digits.
To declare a variable of the double data type, use the following syntax:
var myDouble: double;
You can assign values to double variables in a similar way as real variables:
myDouble := 3.14159265359; myDouble := myReal * 2;
Casting Real Values to Integers
If you need to convert a real or double value to an integer in Pascal, you can use the trunc function. The trunc function truncates the decimal part of a real or double value and returns the corresponding integer value.
var myReal: real; myInt: integer; begin myReal := 3.14; myInt := trunc(myReal); end.
In Pascal, although the float data type is not available by default, you can use the real and double data types to represent floating-point numbers with different levels of precision. The real data type provides single-precision, while the double data type offers higher precision. Remember to use appropriate casting methods when converting between floating-point and integer values.
By understanding these concepts, you can effectively work with floating-point numbers in Pascal and perform various mathematical calculations accurately.