How to get sqrtl in g++?

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
User avatar
Cho
A great helper
Posts: 274
Joined: Wed Oct 20, 2004 11:51 pm
Location: Hong Kong

How to get sqrtl in g++?

Post by Cho » Sun Aug 28, 2005 5:23 am

I've tried to compile a program, which involves sqrtl, with g++. I included <math.h> and compile with the command line "g++ -lm source.cpp". But it said the function sqrtl not found. I've tried to compile in both unix and cygwin. What should I do to make the compiler recognize sqrtl?
I code, therefore I am.

misof
A great helper
Posts: 430
Joined: Wed Jun 09, 2004 1:31 pm

Post by misof » Sun Aug 28, 2005 9:09 am

test.cc:

Code: Select all

#include <iostream>
#include <cmath>
using namespace std;
int main() {
  long double x = 3.14;
  cout << sqrt(x) << endl;
  return 0;
}
and just use g++ test.cc, you don't need the "-lm" flag.

User avatar
Cho
A great helper
Posts: 274
Joined: Wed Oct 20, 2004 11:51 pm
Location: Hong Kong

Post by Cho » Sun Aug 28, 2005 10:16 am

But what I want is sqrtl. Yours is sqrt.
To be specific, how could I compile this code?

Code: Select all

#include <stdio.h>
#include <math.h>
int main()
{
  long double x = 3.14;
  printf("%Lf\n", sqrtl(x));
  return 0;
}
I code, therefore I am.

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf » Sun Aug 28, 2005 12:18 pm

Under cygwin, sqrtl() simply isn't defined in math.h. Add this line after #include's:

Code: Select all

long double sqrtl(long double);
And compile with g++. (or with "gcc -lstdc++", if you prefer C)

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Sun Aug 28, 2005 1:07 pm

This is C++, so no suffixes are necessary and precision is determined by the argument type. For instance:

Code: Select all

long double x;
sqrt(x);
or

Code: Select all

sqrt((long double)1.0);
will work on long doubles. Note that sqrt(1.0) works on doubles.

User avatar
Cho
A great helper
Posts: 274
Joined: Wed Oct 20, 2004 11:51 pm
Location: Hong Kong

Post by Cho » Sun Aug 28, 2005 4:23 pm

Thanks for the replies. I ask this question because I wonder how the online judge compile my program. I personally haven't used long double or sqrtl until 10886.

At the beginning I thought it could be done using double and sqrt only. After WA, I switch everything to long double and sqrtl, and submit it without compiling. So, just want to know any proper way to compile it in case I need sqrt of long double again in other problems.
I code, therefore I am.

User avatar
shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post by shamim » Sun Aug 28, 2005 5:32 pm

If i remember correctly, sqrtl() is defined in <math.h>, if you use C as the compiler option and not C++.
This is how the problem 10023 was solved by many before the judge data was increased.

Post Reply

Return to “C++”