Quadruple Precision in C++ (GCC)
Asked Answered
L

2

14

Just recently, the GCC 4.6.0 came out along with libquadmath. Unfortunately, GNU has supported Fortran, but not C or C++ (all that is included is a .so). I have not found a way to use these new features in C++, however, GNU C does support the __float128 type for guaranteed quadruple-precision floats. GNU C does not seem to support the math functions in libquadmath, such fabsq (absolute value, q being the suffix for quad).

Is there any way to get these functions working in C++, or is there some alternative library that I could use for math functions with __float128? What is the best method for getting quadruple-precision floats working in the GCC? Right now, I can add, subtract, and multiply them, but this is useless to me, considering how I have no way to convert them to strings or use functions such as truncq and fabsq to create my own string function.

Leff answered 27/3, 2011 at 18:44 Comment(8)
The math functions themselves. I can add and subtract the floats, but I can't use math functions such as sinq, cosq, absq, etc. Q being the suffix for quad.Gilbertgilberta
How so? undeclared function? linker error? garbage return value? Can you use them in C?Grammar
The GCC supposedly supports them in Fortran, but the library is still there. I was asking if there was some kind of way to use them in C.Gilbertgilberta
@RetroX: So are you missing header files? Getting linker errors? What exactly goes wrong when you try to use these functions? (Basically what Polybos and David X said)Credendum
No, there aren't any missing headers. I'll edit my question to generalise it a bit more, then, hopefully, it'll be easier to understand.Gilbertgilberta
@RetroX: You still haven't explained what you mean by "does not seem to support the math functions". What happens when you try to call fabsq(qx), where qx obviously is a quad-precision float?Credendum
And by explain we mean please post sample code you've tried to compile and any/all errors you receive. There is a quadmath.h header you can include (and it is referenced in the documentation). Here is some sample source you can try to compile for us: gcc.gnu.org/onlinedocs/libquadmath/quadmath_005fsnprintf.htmlHandicraft
The problem this user is encountering is very common. The quadlib libraries do not extend the math.h and cmath library headers in the expected way as of end of 2017. I've written special headers with all the C++ overloads, so that normal programs with math.h and <cmath> can be compiled against quadlib with minimal changes and am willing to trade code for them. See:: #48592096Embowel
L
10

Apparently, this seems to have been an installation error on my part.

While the core C/C++ portion of the GCC includes libquadmath.so, the Fortran version supplies libquadmath.a and quadmath.h, which can be included to access the functions.

#include <quadmath.h>
#include <iostream>
int main()
{
  char* y = new char[1000];
  quadmath_snprintf(y, 1000, "%Qf", 1.0q);
  std::cout << y << std::endl;
  return 0;
}
Leff answered 29/3, 2011 at 2:4 Comment(0)
R
4

nm the .so file, and see what function names really are. IIRC, fortran routines have an _ at end of name. In C++, you'll need to extern "C" {} prototypes. If this is a fortran interface, then all args are passed by reference, so proto might be something like

extern "C" { long double fabsq_(long double* x); }
Reorganization answered 29/3, 2011 at 1:18 Comment(1)
exactly. That's why I wrote a header file to encapsulate all the extern definitions, and defined the class for floating type limits, exceptions, etc. It's a pain ... especially getting cin << cout >> to work naturally. Hopefully, if someone helps me with a minor problem ... I'll release the headers for everyone to share. :: #48592096Embowel

© 2022 - 2024 — McMap. All rights reserved.