math library

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

Moderator: Board moderators

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

Post by Krzysztof Duleba »

Ups, sorry for the mistake in your name.
Julien Cornebise
Experienced poster
Posts: 145
Joined: Sat Feb 23, 2002 2:00 am
Location: Paris, France
Contact:

Post by Julien Cornebise »

Hi

Did you use either
[cpp]#include <math.h>[/cpp] or
[cpp]#include <cmath>[/cpp]
?
I guess the easiest thing would be that you mail me your code (on the forum here, or by mail : cornebis at esiea.fr), I'll check it out. I think you might forget to include a header definition.

PS : don't worry about any mistake in my name : :wink:
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

I always use cmath.

It's not possible for me to submit that version of code. I was angry when I got 0.004s and I changed the code deeply. For instance I used buffered input/output routines from STL that are defined in some files that have #include <algorithm> in, and in algorithm header file abs() is defined, so the problem disappeared (before that I had to include algorithm or cmath manually to get AC). Actually I don't remember how the old version worked, but I'll try to find it out, then I'll place the code somewhere.
Julien Cornebise
Experienced poster
Posts: 145
Joined: Sat Feb 23, 2002 2:00 am
Location: Paris, France
Contact:

Post by Julien Cornebise »

Okay.
Given that I'm quite new to C++ after having been a C programmer for years, if the trick is too linked with the C++ specific headers, I'll be of little help - though I'll try anyway of course :)
But it doesn't seem so weird to me that you have to manually include cmath (if I understood your post, that was what surprized you). Some compiler have a set of implicitely included files, tolering such oblivions, but that's not always the case, so that might be why you didn't got AC first.
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

The following code gets CE in problem 155 even though it compiles well on my PC (win2k + cygwin) in ansi mode and on some other Linux machine (which was used during ACM regionals in 2002, possibly with not exactly the same set of software):

[cpp]#include <iostream>
using namespace std;

int main()
{
abs(-1);
}[/cpp]

It can be fixed easily, but the question arises: do my compilers (GNU C++ 2.95 and GNU C++ 3.2) obey some other ANSI standard than the OJ compiler?
Ashique Mahmood Rupam
New poster
Posts: 8
Joined: Sun Sep 14, 2003 11:38 pm
Location: Dhaka
Contact:

math funcitons

Post by Ashique Mahmood Rupam »

I see that math functions of judge's compiler are overloaded ( when I use <cmath> ). Should they be ? As C math functions are inherited as they were, they should not have been overloaded as they were not. I do not also see any specification by TC++PL stroustrup. Then why are they overloaded ?
There is Nothing, Nothing At All !
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

How do you know that OJ is overloading those functions?
Ashique Mahmood Rupam
New poster
Posts: 8
Joined: Sun Sep 14, 2003 11:38 pm
Location: Dhaka
Contact:

cmath of OJ

Post by Ashique Mahmood Rupam »

I got similar CE at past. OJ gives details of compilers error messages; I checked and found that their compiler found amguity while passing the value to abs( ) or log( ), as these are overloaded for several data types except the one I passed. I can't remember, exactly what was written there.

And you should read OJ's compiler error messages. I think, thus you will also learn that they overload.

And any expert please reply my past posting.
There is Nothing, Nothing At All !
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

I don't have to read OJ compiler output. I usually don't get CE, and if I do then it's the fault of either OJ not obeying the standard or me being to lazy to compile before submitting.

Was the message you received similar to
*.cpp: In function `int main()':
*.cpp:*: error: call of overloaded `sqrt(int&)' is ambiguous
/usr/include/math.h:61: error: candidates are: double sqrt(double)
/usr/include/c++/3.3.1/cmath:550: error: long double
std::sqrt(long double)
/usr/include/c++/3.3.1/cmath:546: error: float std::sqrt(float)
make: *** [t] Error 1
If so, then everything is ok and that's the only message you should get. I already wrote why a month or two ago - search and you will find.
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

To be more precise: a month ago and even in this thread. You haven't read what you're replying to?
Ashique Mahmood Rupam
New poster
Posts: 8
Joined: Sun Sep 14, 2003 11:38 pm
Location: Dhaka
Contact:

Reply to Duleba

Post by Ashique Mahmood Rupam »

I read that, but it is confusion that whether Duleba a single person or two. That posting contains a compiler error message,

"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) "

where I can clearly see that the error concerns overloading of math functions. That means you are familiar with this problem. But, afterwards you asked me -
"How do you know that OJ is overloading those functions?"
You also knew that they are overloaded by OJ, then why did you ask ?
There is Nothing, Nothing At All !
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

I asked you to give more details. I wanted to see the CE message you received - exactly how do you know that those functions were overloaded. Trying to guess what one meant isn't fun. You should learn how to report a problem. And you should learn how to be a bit more polite too.
Ashique Mahmood Rupam
New poster
Posts: 8
Joined: Sun Sep 14, 2003 11:38 pm
Location: Dhaka
Contact:

Politeness!

Post by Ashique Mahmood Rupam »

Well, not only a bit politeness, but also I know the degree of politeness. The way you said -
"If so, then everything is ok and that's the only message you should get. I already wrote why a month or two ago - search and you will find."
-&-
"You haven't read what you're replying to?"
was polite to a certain limit.

My reply did not exceed the limit shown by you.
And my problem was language specific, not OJ's CE message specific and my report was not incomplete without it, specially for those who know this problem exactly. So, posts should not be such that politeness becomes a topic. I only need expert suggestions about C++, specifically about C++'s math functions at this moment. Suggestions about report giving style and politeness should be posted elsewhere.
There is Nothing, Nothing At All !
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

Question if you'd read what you were replying to was relevant - it seemed that you hadn't. Question whether Dulebas are a single person was not.

What kind of help exactly do you need? Still you haven't specified it. Still you haven't quoted the message you receive (from OJ or your compiler). I already wrote that trying to guess what you meant isn't fun.

If you want to run a math funcion for an integer, you have to convert it to proper floating point type before (that's what your compiler is probably telling you, why haven't you tried?). The following code shall compile:
[cpp]int a=1;
sqrt((double)a);
sqrt((float)a);
sqrt((long double)a);[/cpp]
whereas sqrt(a) will produce an error.
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

I forgot to add that if your compiler has such ability, you might consider using
[cpp]sqrt(static_cast<double>(a));[/cpp]
which is recommended by many authors.
Post Reply

Return to “C++”