math library
Moderator: Board moderators
math library
Does C++ have its own math library or do I have to use ANSI C's math.h library for math operations?
-
- New poster
- Posts: 22
- Joined: Sun Oct 20, 2002 6:41 pm
- Location: Lithuania
- Contact:
C++ just wraps old math.h in cmath (as usual). You can see in http://www.boost.org that a lot of additional functionality is proposed, however it has yet to come to standard libs to be used in contest environment (of course, for 'normal' projects you can find a lot more than math.h)
-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact:
Actually the judge is using his own version of math library.
I've just send my code for 149 - Forests problem and got compile error:
`M_PI' was not declared in this scope
`M_3PI_4' was not declared in this scope
M_PI is a definition from math.h header file present in two different g++ 2.95 compilers that I have and in g++ 3.2 as well. I am wondering what else was removed from math.h.
I've just send my code for 149 - Forests problem and got compile error:
`M_PI' was not declared in this scope
`M_3PI_4' was not declared in this scope
M_PI is a definition from math.h header file present in two different g++ 2.95 compilers that I have and in g++ 3.2 as well. I am wondering what else was removed from math.h.
-
- Experienced poster
- Posts: 145
- Joined: Sat Feb 23, 2002 2:00 am
- Location: Paris, France
- Contact:
-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact:
-
- Experienced poster
- Posts: 145
- Joined: Sat Feb 23, 2002 2:00 am
- Location: Paris, France
- Contact:
M_PI hasn't been removed, if you look in math.h you'll see that it's definition is encapsuled in a [c]#if defined __USE_BSD || defined __USE_XOPEN[/c], meangin that it is only avalaible in sources using BSD or XOPEN extensions (wich is the default for gcc). It also implies that it is not part of the ANSI C specification. When you compile for ACM, you must use the [c] -ansi [/c] flag to ensure that your code is 100% Ansi compatible. This reduce the set of functions or constant you can use, but allows everybody to code on the same basis : no one will be more advantaged than another because the judge uses the same version of the compiler as him : everybody will have to use the same set of functions. That's more fair (and more fun), isn't it ? 

-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact:
That's the worst solution. For every reference to PI, acos(0) is evaluated again and again, which takes a lot of time - define macro simply replaces PI by 2*acos(0).
If you want to do define pi this way, the only way is the following:
[cpp]const SUITABLE_FLOAT_TYPE PI 2*acos(0)[/cpp]
Here, acos(0) is evaluated only once and then stored in variable PI.
If you want to do define pi this way, the only way is the following:
[cpp]const SUITABLE_FLOAT_TYPE PI 2*acos(0)[/cpp]
Here, acos(0) is evaluated only once and then stored in variable PI.
-
- Experienced poster
- Posts: 145
- Joined: Sat Feb 23, 2002 2:00 am
- Location: Paris, France
- Contact:
-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact:
-
- Experienced poster
- Posts: 202
- Joined: Fri Mar 22, 2002 2:00 am
- Location: Chittagong. CSE - CUET
- Contact:
Oh! Yeah!Krzysztof Duleba wrote:That's the worst solution. For every reference to PI, acos(0) is evaluated again and again, which takes a lot of time - define macro simply replaces PI by 2*acos(0).
If you want to do define pi this way, the only way is the following:
[cpp]const SUITABLE_FLOAT_TYPE PI 2*acos(0)[/cpp]
Here, acos(0) is evaluated only once and then stored in variable PI.
I once used it in C...........but for C++
[cpp]
#include<cmath>
const double PI = 2.0 * acos(0.0);
[/cpp]
Is always I use!
Thanks for reminding!

-
- Experienced poster
- Posts: 145
- Joined: Sat Feb 23, 2002 2:00 am
- Location: Paris, France
- Contact:
HiKrzysztof Duleba wrote:I know - my remark was adressed to Moni. He, not you, used acos(0). BTW - I think it should be here acos(0.0), otherwise the code may not compile (I don't know, I haven't checked, but that's a common problem with funtions from math library).
Under gcc, it does compile, it implicitely converts from int to double. But it is compiler dependant, of course.
-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact:
We're talking about C++, not C, so you should use g++ instead of gcc. And g++ gave me this output:
That's because almost all the funcions are written in two (for doubles and long doubles) or even three (floats, doubles and long doubles) types of arguments and int can be converted to all of them.t.cpp:7: call of overloaded `acos(int)' is ambiguous
/usr/include/math.h:51: candidates are: double acos(double)
/usr/include/c++/3.2/cmath:112: long double std::acos(long double)
/usr/include/c++/3.2/cmath:99: float std::acos(float)
-
- Experienced poster
- Posts: 145
- Joined: Sat Feb 23, 2002 2:00 am
- Location: Paris, France
- Contact:
-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact: