Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do gcc's __float128 floating point numbers take the current rounding mode into account?

Do the arithmetic operations on gcc's __float128 floating point numbers take the current rounding mode into account?

For instance, if using the C++11 function std::fesetenv, I change the rounding mode to FE_DOWNWARD, will results of arithmetic operations on __float128 be rounded down?

Is this guaranteed by the __float128 specification?

like image 222
Walter Mascarenhas Avatar asked Sep 23 '14 06:09

Walter Mascarenhas


1 Answers

I believe that it is guaranteed that operations on __float128 take the rounding mode into account.

According to the GNU C Library documentation floating-point calculations respect the rounding mode.

According to the GCC manual __float128 is a floating type which supports arithmetic operations as division etc.

From that I deduce, that for operations on __float128, the rounding mode has to be taken into account.

The GNU C Library documentation states:

20.6 Rounding Modes

Floating-point calculations are carried out internally with extra precision, and then rounded to fit into the destination type. This ensures that results are as precise as the input data. IEEE 754 defines four possible rounding modes: [...]

Reference: http://www.gnu.org/software/libc/manual/html_node/Rounding.html

The GCC manual states:

6.11 Additional Floating Types

As an extension, GNU C supports additional floating types, __float80 and __float128 to support 80-bit (XFmode) and 128-bit (TFmode) floating types. Support for additional types includes the arithmetic operators: add, subtract, multiply, divide; unary arithmetic operators; relational operators; equality operators; [...]

Reference: https://gcc.gnu.org/onlinedocs/gcc/Floating-Types.html

Working Example

Example code and output

__float128 f1=1.0;
__float128 f2=3.0;

char buf[256];

fesetround(FE_DOWNWARD);

quadmath_snprintf (buf, sizeof buf, "%*.34Qf",10, (f1/f2));
printf ("%s\n", buf);

fesetround(FE_UPWARD);

quadmath_snprintf (buf, sizeof buf, "%*.34Qf",10, (f1/f2));
printf ("%s\n", buf);

outputs:

0.3333333333333333333333333333333333
0.3333333333333333333333333333333334
like image 179
oo_miguel Avatar answered Sep 21 '22 17:09

oo_miguel