• Home   /  
  • Archive by category "1"

Gcc Round Math Homework

To round a in C, there are 3 functions to meet the need. Recommend .

The functions round their argument to an integer value in floating-point format, using the current rounding direction and without raising the ‘‘inexact’’ floating point exception. C11dr §7.12.9.3 2

or

The functions differ from the functions (7.12.9.3) only in that the functions may raise the ‘‘inexact’’ floating-point exception if the result differs in value from the argument. C11dr §7.12.9.4 2

or

The functions round their argument to the nearest integer value in floating-point format, rounding halfway cases away from zero, regardless of the current rounding direction. C11dr §7.12.9.6 2


Example

Output


What is weak about OP's code?

  1. Should have a value not near the range, the cast results in undefined behavior.

  2. When results in an inexact answer. This is unlikely here as is a causing the addition to occur at a higher precision than . When and have the same precision, adding to a number may result in numerical rounded answer. (This is not the whole number rounding of OP's post.) Example: the number just less than 0.5 should round to 0 per OP's goal, yet results in an exact answer between 1.0 and the smallest just less than 1.0. Since the exact answer is not representable, that sum rounds, typically to 1.0 leading to an incorrect answer. A similar situation occurs with large numbers.


OP's dilemma about "The above line always prints the value as 4 even when ." is not explainable as stated. Additional code/information is needed. I suspect OP may have used .


Tested little - will do so later when I have time.


20.8.3 Rounding Functions

The functions listed here perform operations such as rounding and truncation of floating-point values. Some of these functions convert floating point numbers to integer values. They are all declared in .

You can also convert floating-point numbers to integers simply by casting them to . This discards the fractional part, effectively rounding towards zero. However, this only works if the result can actually be represented as an —for very large numbers, this is impossible. The functions listed here return the result as a instead to get around this problem.

The functions use the following macros, from TS 18661-1:2014, to specify the direction of rounding. These correspond to the rounding directions defined in IEEE 754-2008.

Round toward +∞.

Round toward -∞.

Round toward zero.

Round to nearest, ties round away from zero.

Round to nearest, ties round to even.

Function: doubleceil(double )
Function: floatceilf(float )
Function: long doubleceill(long double )
Function: _FloatNceilfN(_Float)
Function: _FloatNxceilfNx(_Floatx )

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

These functions round upwards to the nearest integer, returning that value as a . Thus, is .

Function: doublefloor(double )
Function: floatfloorf(float )
Function: long doublefloorl(long double )
Function: _FloatNfloorfN(_Float)
Function: _FloatNxfloorfNx(_Floatx )

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

These functions round downwards to the nearest integer, returning that value as a . Thus, is and is .

Function: doubletrunc(double )
Function: floattruncf(float )
Function: long doubletruncl(long double )
Function: _FloatNtruncfN(_Float)
Function: _FloatNxtruncfNx(_Floatx )

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

The functions round towards zero to the nearest integer (returned in floating-point format). Thus, is and is .

Function: doublerint(double )
Function: floatrintf(float )
Function: long doublerintl(long double )
Function: _FloatNrintfN(_Float)
Function: _FloatNxrintfNx(_Floatx )

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

These functions round to an integer value according to the current rounding mode. See Floating Point Parameters, for information about the various rounding modes. The default rounding mode is to round to the nearest integer; some machines support other modes, but round-to-nearest is always used unless you explicitly select another.

If was not initially an integer, these functions raise the inexact exception.

Function: doublenearbyint(double )
Function: floatnearbyintf(float )
Function: long doublenearbyintl(long double )
Function: _FloatNnearbyintfN(_Float)
Function: _FloatNxnearbyintfNx(_Floatx )

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

These functions return the same value as the functions, but do not raise the inexact exception if is not an integer.

Function: doubleround(double )
Function: floatroundf(float )
Function: long doubleroundl(long double )
Function: _FloatNroundfN(_Float)
Function: _FloatNxroundfNx(_Floatx )

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

These functions are similar to , but they round halfway cases away from zero instead of to the nearest integer (or other current rounding mode).

Function: doubleroundeven(double )
Function: floatroundevenf(float )
Function: long doubleroundevenl(long double )
Function: _FloatNroundevenfN(_Float)
Function: _FloatNxroundevenfNx(_Floatx )

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

These functions, from TS 18661-1:2014 and TS 18661-3:2015, are similar to , but they round halfway cases to even instead of away from zero.

Function: long intlrint(double )
Function: long intlrintf(float )
Function: long intlrintl(long double )
Function: long intlrintfN(_Float)
Function: long intlrintfNx(_Floatx )

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

These functions are just like , but they return a instead of a floating-point number.

Function: long long intllrint(double )
Function: long long intllrintf(float )
Function: long long intllrintl(long double )
Function: long long intllrintfN(_Float)
Function: long long intllrintfNx(_Floatx )

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

These functions are just like , but they return a instead of a floating-point number.

Function: long intlround(double )
Function: long intlroundf(float )
Function: long intlroundl(long double )
Function: long intlroundfN(_Float)
Function: long intlroundfNx(_Floatx )

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

These functions are just like , but they return a instead of a floating-point number.

Function: long long intllround(double )
Function: long long intllroundf(float )
Function: long long intllroundl(long double )
Function: long long intllroundfN(_Float)
Function: long long intllroundfNx(_Floatx )

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

These functions are just like , but they return a instead of a floating-point number.

Function: intmax_tfromfp(double , int , unsigned int )
Function: intmax_tfromfpf(float , int , unsigned int )
Function: intmax_tfromfpl(long double , int , unsigned int )
Function: intmax_tfromfpfN(_Float, int , unsigned int )
Function: intmax_tfromfpfNx(_Floatx , int , unsigned int )
Function: uintmax_tufromfp(double , int , unsigned int )
Function: uintmax_tufromfpf(float , int , unsigned int )
Function: uintmax_tufromfpl(long double , int , unsigned int )
Function: uintmax_tufromfpfN(_Float, int , unsigned int )
Function: uintmax_tufromfpfNx(_Floatx , int , unsigned int )
Function: intmax_tfromfpx(double , int , unsigned int )
Function: intmax_tfromfpxf(float , int , unsigned int )
Function: intmax_tfromfpxl(long double , int , unsigned int )
Function: intmax_tfromfpxfN(_Float, int , unsigned int )
Function: intmax_tfromfpxfNx(_Floatx , int , unsigned int )
Function: uintmax_tufromfpx(double , int , unsigned int )
Function: uintmax_tufromfpxf(float , int , unsigned int )
Function: uintmax_tufromfpxl(long double , int , unsigned int )
Function: uintmax_tufromfpxfN(_Float, int , unsigned int )
Function: uintmax_tufromfpxfNx(_Floatx , int , unsigned int )

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

These functions, from TS 18661-1:2014 and TS 18661-3:2015, convert a floating-point number to an integer according to the rounding direction (one of the macros). If the integer is outside the range of a signed or unsigned (depending on the return type of the function) type of width bits (or outside the range of the return type, if is larger), or if is infinite or NaN, or if is zero, a domain error occurs and an unspecified value is returned. The functions with an ‘’ in their names raise the inexact exception when a domain error does not occur and the argument is not an integer; the other functions do not raise the inexact exception.

Function: doublemodf(double , double *)
Function: floatmodff(float , float *)
Function: long doublemodfl(long double , long double *)
Function: _FloatNmodffN(_Float, _Float *)
Function: _FloatNxmodffNx(_Floatx , _Floatx *)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

These functions break the argument into an integer part and a fractional part (between and , exclusive). Their sum equals . Each of the parts has the same sign as , and the integer part is always rounded toward zero.

stores the integer part in , and returns the fractional part. For example, returns and stores into .


One thought on “Gcc Round Math Homework

Leave a comment

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *