ComputersSoftware

Type conversion. Functions Round and Trunc in "Pascal"

Working in Pascal with variables of different types, quite often we have to deal with the fact that when compiling a program there are errors indicating incorrect conversion of values. For example, you can not assign an integer variable of 5.9, since this will result in a compilation error. In this case, you have to talk about using the Round and Trunc functions in Pascal, with which you can convert the types of arguments and continue to perform certain tasks with them.

General information about type conversions

Type conversion (reduction of values) is the process of converting values of one type of data to another. There are explicit and implicit casting types. The first is set directly by the developer using either language constructs or using functions, and the second is executed independently by the interpreter or the code compiler according to the rules declared in the standard of a particular programming language.

Type Conversion in Pascal

In the Pascal programming language, you can use both explicit and implicit type conversions.

With explicit casting, Pascal uses calls to special conversion functions whose arguments belong to the same type, and the value to a completely different type of data. These are The Trunc function in Pascal and the Round function, which will be discussed in more detail below.

Implicit casting of types in this language is possible only in cases where in expressions that consist of Integer and real variables, the former are automatically converted to the second type.

Next, we'll talk about how you can implement type casting for numeric data.

Trunc

Built-in mathematical function. Trunc in "Pascal" discards the entire fractional part of the argument, leading it to the integer-type view. For example, by calling a function Trunc with argument (1.73) in the end you can get the result 1.

Syntax : Functions : Trunc (X: real): Longint.

Round

Built-in mathematical function. The Round function rounds the argument according to the rules of mathematics to the nearest integer. For example, calling Round (1.73) will end up with 2, and Round from argument (1.11) will give 1.

функции : Function syntax : Round (X: real): Longint.

It is worth noting that there are restrictions for the result of the Round and Trunc functions in Pascal. The execution will fail if this result Goes beyond the values of type Longint.

Obviously, the syntax of both built-in functions is quite simple and allows you to use Round and Trunc in Pascal for explicit type conversion without unnecessary problems and does not cause compilation errors about type violation.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.unansea.com. Theme powered by WordPress.