Trunc c что это
Перейти к содержимому

# Trunc c что это

## Trunc c что это

All three functions are used to remove digits after decimal point and return the modified decimal number. trunc() : Truncates a double value after the decimal point and gives the integer part as the result. The return value and the arguments are of the type double.

Syntax : double trunc(double x); Parameters: x :It takes a double value as an input and then truncates the values after the decimal point. Return Value : It returns a double value whose values after the decimal point is 0 only.

Time Complexity: O(1)

Space Complexity: O(1)

## Rounding and Truncating numbers using math.h in C

In this article, we have explored how we can round and truncate numbers in C using the math.h library.

math.h is a header file in the list of standard C libraries which enables the coder to perform some basic mathematical operations and transformations. The header file contains many functions among which some commonly used functions are pow(), ceil(), floor(), sqrt(), trunc(), round() and abs(). Most of the functions in math.h uses floating point numbers.

In the following article we will be discussing about the two familiar functions:

1. round(): roundl(), lround(), lroundl(), lroundf()
2. trunc(): truncl(), truncf()

To use round() and trunc(), you need to import the header file:

## round()

round() function in c is used to return the nearest integer value of the float/double/long double argument passed to this function.

If the decimal value is from ".1 to .5", it returns an integer value which is less than the argument we passed and if the decimal value is from ".6 to .9", the function returns an integer value greater than the argument we passed. In short the round() function returns an integer value nearest to the argument we passed.

There are two types of rounding functions in math.h library.

### 1. round()

It takes a floating value as argument and rounds the decimal part of it. It returns value in floating type format.

If we pass float number as an argument, the syntax is:

If we pass double as an argument, the syntax is:

• If we pass long double as an argument, the syntax is:

### 2. lround()

• It also takes a floating value as argument and rounds the decimal part of it, similar to round(). The only difference is lround() returns a value in integer type format.
• If we pass float number as an argument, the syntax is:
• If we pass double as an argument, the syntax is:
• If we pass long double as an argument, the syntax is:
• If arg is ±∞, it is returned, unmodified
If arg is ±0, it is returned, unmodified
• If arg has type long double, roundl, lroundl, llroundl is called. Otherwise, if arg has integer type or the type double, round, lround, llround is called. Otherwise, roundf, lroundf, llroundf is called, respectively.

## trunc()

• The trunc() function allows to truncate(remove) the decimal value from a floating number and return an inetger value.
• There are three types of truncate functions in math.h library

### 1. trunc()

It takes a double value as argument and truncates the decimal part of it. It returns a double value whose decimal value is zero(0).

Syntax:

### 2. truncf()

• It works same as trunc() function but with the difference that it is used for float values instead of double. It takes a float value as argument, removes digits after decimal point and return modified float value.
• Syntax:

### 3. truncl()

• It works same as trunc() and truncf function but with the difference that it is used for long double values instead of double and float. It truncates the values after decimal and returns the modified value.
• Syntax:

#### Remember

round() is used to round a floating number to its nearest integer value whereas trunc() is used to remove the decimal values from a number.

With this, you have the complete knowledge of rounding and truncating a number in C.

## std:: trunc, std:: truncf, std:: truncl

If no errors occur, the nearest integer value not greater in magnitude than arg (in other words, arg rounded towards zero) is returned.

####  Error handling

Errors are reported as specified in math_errhandling .

If the implementation supports IEEE floating-point arithmetic (IEC 60559),

• The current rounding mode has no effect.
• If arg is ±∞, it is returned, unmodified
• If arg is ±0, it is returned, unmodified
• If arg is NaN, NaN is returned

####  Notes

FE_INEXACT may be (but isn’t required to be) raised when truncating a non-integer finite value.

The largest representable floating-point values are exact integers in all standard floating-point formats, so this function never overflows on its own; however the result may overflow any integer type (including std::intmax_t ), when stored in an integer variable.

The implicit conversion from floating-point to integral types also rounds towards zero, but is limited to the values that can be represented by the target type.

## Функция trunc() в C++

Функция trunc() в C ++ округляет аргумент до нуля и возвращает ближайшее целое значение, которое по величине не превышает аргумент.

### прототип trunc() [в соответствии со стандартом C ++ 11]

Функция trunc() принимает единственный аргумент и возвращает значение типа double, float или long double. Эта функция определена в заголовочном файле <cmath>.

### Параметры

Принимает единственный аргумент, значение trunc которого должно быть вычислено.

### Возвращаемое значение

Округляет x до нуля и возвращает ближайшее целое значение, которое по величине не превышает x.

Просто функция trunc() обрезает значение после десятичной дроби и возвращает только целую часть.

### Пример для целочисленных типов

Для целых значений применение функции trunc возвращает то же значение, что и результат. Поэтому на практике он обычно не используется для целочисленных значений.