473,587 Members | 2,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why do I need ::sin, not std::sin?

Hi,

simple question. Why does this not work:

#include <algorithm>
#include <cmath>
#include <iostream>

int
main()
{
double a[2];
a[0] = 0;a[1] = M_PI/2.;
std::transform( a, a+2, a, std::sin);
std::cout << a[0] << ", " << a[1] << std::endl;
return 0;
}

for gcc and icc the type of the third argument of transform is unknown. If I
use the global ::sin it works fine. Why? I include cmath and there sin is
defined in namespace std, so why is it unknown?

regards,
alex

Jul 22 '05 #1
14 3144
Alexander Stippler wrote:
Hi,

simple question. Why does this not work:

#include <algorithm>
#include <cmath>
#include <iostream>

int
main()
{
double a[2];
a[0] = 0;a[1] = M_PI/2.;
std::transform( a, a+2, a, std::sin);
std::cout << a[0] << ", " << a[1] << std::endl;
return 0;
}

for gcc and icc the type of the third argument of transform is unknown. If I
use the global ::sin it works fine. Why? I include cmath and there sin is
defined in namespace std, so why is it unknown?

regards,
alex


IANAG (I am not a guru), and am probably way off base (please correct me
guys -- please no flames).

Isn't std::sin a template? Would you need to specifically instantiate
std::sin, i.e.

std::transform( a, a+2, a, std::sin<double >)

You might also want to use std::ptr_fun as a wrapper to std::sin (or ::sin).

Jul 22 '05 #2
Alexander Stippler wrote in news:40******@n ews.uni-ulm.de in
comp.lang.c++:
Hi,

simple question. Why does this not work:

#include <algorithm>
#include <cmath>
#include <iostream>

int
main()
{
double a[2];
a[0] = 0;a[1] = M_PI/2.;
std::transform( a, a+2, a, std::sin);
std::cout << a[0] << ", " << a[1] << std::endl;
return 0;
}

for gcc and icc the type of the third argument of transform is
unknown. If I use the global ::sin it works fine. Why? I include cmath
and there sin is defined in namespace std, so why is it unknown?

regards,
alex


There are several overloads of std::sin, you need to pick one:

std::transform( a, a+2, a, (double (*)(double)std: :sin);

Note M_PI isn't AFAICT Standard C++, you can use:

double const M_PI = (2.0 * std::acos( 0.0 ));

Though maybe:

double const double_pi = (2.0 * std::acos( 0.0 ));

Whould be better.

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
red floyd wrote:

Isn't std::sin a template?


No, just an ordinary function, with overloads for arguments of type
float, double, and long double. (There's also a sin template for
arguments of type complex<T>, but that's not involved here)

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #4
red floyd wrote:
Alexander Stippler wrote:
Hi,

simple question. Why does this not work:

#include <algorithm>
#include <cmath>
#include <iostream>

int
main()
{
double a[2];
a[0] = 0;a[1] = M_PI/2.;
std::transform( a, a+2, a, std::sin);
std::cout << a[0] << ", " << a[1] << std::endl;
return 0;
}

for gcc and icc the type of the third argument of transform is unknown.
If I use the global ::sin it works fine. Why? I include cmath and there
sin is defined in namespace std, so why is it unknown?

regards,
alex


IANAG (I am not a guru), and am probably way off base (please correct me
guys -- please no flames).

Isn't std::sin a template? Would you need to specifically instantiate
std::sin, i.e.

std::transform( a, a+2, a, std::sin<double >)

You might also want to use std::ptr_fun as a wrapper to std::sin (or

std::transform( a, a+2, a, std::sin<double >)

What about this line if you have <complex> included? I took a look at the
<cmath> source code. It defines (in namespace std!) sin for float argument
as non template function, but the example above results in the same error
for float. I'm confused.

regards,
alex


Jul 22 '05 #5
Rob Williscroft wrote:
Alexander Stippler wrote in news:40******@n ews.uni-ulm.de in
comp.lang.c++:

Hi,

simple question. Why does this not work:

#include <algorithm>
#include <cmath>
#include <iostream>

int
main()
{
double a[2];
a[0] = 0;a[1] = M_PI/2.;
std::transform( a, a+2, a, std::sin);
std::cout << a[0] << ", " << a[1] << std::endl;
return 0;
}

for gcc and icc the type of the third argument of transform is
unknown. If I use the global ::sin it works fine. Why? I include cmath
and there sin is defined in namespace std, so why is it unknown?

regards,
alex

There are several overloads of std::sin, you need to pick one:

std::transform( a, a+2, a, (double (*)(double)std: :sin);


Unmatched parentheses. Did you mean to write

std::transform( a, a+2, a, (double (*)(double))std ::sin);

(i.e. use the C-style cast)?

BTW, why can't it figure out the type of 'std::sin'? Is that because
it's overloaded?
[...]


V
Jul 22 '05 #6
Rob Williscroft wrote:

Note M_PI isn't AFAICT Standard C++, you can use:

double const M_PI = (2.0 * std::acos( 0.0 ));

Though maybe:

double const double_pi = (2.0 * std::acos( 0.0 ));

Whould be better.


The latter, definitely. Otherwise you end up calling acos every time you
use the value of M_PI.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #7
Victor Bazarov wrote in news:Is******** ********@dfw-read.news.verio .net
in comp.lang.c++:
There are several overloads of std::sin, you need to pick one:

std::transform( a, a+2, a, (double (*)(double)std: :sin);
Unmatched parentheses. Did you mean to write

std::transform( a, a+2, a, (double (*)(double))std ::sin);


Thanks, I should have "cut-n-paste" from my editor.
(i.e. use the C-style cast)?

BTW, why can't it figure out the type of 'std::sin'? Is that because
it's overloaded?


Yes, but its also because std::transform' s signature doesn't do
anything to help:

template<class InputIterator, class OutputIterator, class UnaryOperation>
OutputIterator
transform(
InputIterator first, InputIterator last,
OutputIterator result, UnaryOperation op
)
;

I.e. UnaryOperation is independant of InputIterator and OutputIterator.

Deducable example:

#include <cmath>

template < typename T >
T f( T arg, T (*func)( T ) )
{
return func( arg );
}

int main()
{
f( 0.0, std::sin );
}
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #8
Pete Becker wrote:
Rob Williscroft wrote:
Note M_PI isn't AFAICT Standard C++, you can use:

double const M_PI = (2.0 * std::acos( 0.0 ));

Though maybe:

double const double_pi = (2.0 * std::acos( 0.0 ));

Whould be better.

The latter, definitely. Otherwise you end up calling acos every time you
use the value of M_PI.


Huh? I must be missing something. Did you mean to support the usual
const versus macro argument but instead implied that if the name of the
constant is M_PI, then it's somehow a function call instead of a const,
and if the name is 'double_pi', then it's reverse?

Rob didn't try to suggest

#define M_PI (2.0 * std::acos(0))

, did he? So, the latter or the former should be basically the same.

Victor
Jul 22 '05 #9
red floyd wrote:
Alexander Stippler wrote:
Hi,

simple question. Why does this not work:

#include <algorithm>
#include <cmath>
#include <iostream>

int
main()
{
double a[2];
a[0] = 0;a[1] = M_PI/2.;
No such thing as 'M_PI' in the standard C++, BTW.
std::transform( a, a+2, a, std::sin);
std::cout << a[0] << ", " << a[1] << std::endl;
return 0;
}

for gcc and icc the type of the third argument of transform is
unknown. If I
use the global ::sin it works fine. Why? I include cmath and there sin is
defined in namespace std, so why is it unknown?

regards,
alex

IANAG (I am not a guru), and am probably way off base (please correct me
guys -- please no flames).

Isn't std::sin a template?


Not for doubles. For complex<> and for valarray<>, yes. For
double, float, long double, it's just an overloaded function.
Would you need to specifically instantiate
std::sin, i.e.

std::transform( a, a+2, a, std::sin<double >)
No.

You might also want to use std::ptr_fun as a wrapper to std::sin (or
::sin).


That might help. I am not sure I understand why, though.

For all I can see in the Standard, the 'std::transform ' template has
to be able to figure out the type of 'std::sin'. It's an overloaded
function and the lookup should be limited to the type of the first
argument.

I would like to really hear from Greg Comeau on this, too. His online
try-it-out thing refuses to compile and I cannot figure out how to make
it.

Victor
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
4877
by: franky.backeljauw | last post by:
Hello, I have the following code: <code> class X { ___ sin(); } X sin( X ) { return sin( y, x ); }
10
2345
by: Gregc. | last post by:
G'day I am trying to work out the length of the hypotenuse. Attached is the code that I am using: #include <stdio.h> #include <math.h> double hypUsingTrig(double e) {
0
2056
by: Cause | last post by:
In the following code to call my website for serial registration reasons, I want to test on localhost. The code works on the internet, but this code sends me a bunch of 400 bad request errors when I use appache on port 80 localhost. Here's the code: ////////////////////////////////////////////////////////////////////////////////////// ...
15
10346
by: Morgan Cheng | last post by:
Hi, I am writing a program that will take a lot of Math.Cos & Math.Sin operation. I am afraid this will be source of performance impact. Anybody knows how Math.cos & Math.Sin is implemented? I suppose it just retrieving a huge pre-computed table, it might be quick. I tried to cache all possible angle cos/sin in my own array , it turns to...
23
8967
by: spidey4549 | last post by:
I am trying to get the value of sin based on an angle input from the user. The program changes the angle from degrees to radians. Then uses the formula: sin(x) = x − x^3/3! + x^5/5! - x^7/7! + x^9/9!... It does this all the way to 15. When I try to compile my program it says it doesn't evaluate to a function. Here is what I have so far:...
0
4107
by: outofstep | last post by:
Hit me up on AIM/YIM (oipaloi). I'm not a code monkey and would like to get some advice. Or hit up this thread right here. I'm using DEV C++ as my compiler. I'm trying to calculate SIN/COS/TAN, etc without the use of CMATH. Using two recursive functions for the power and factorial. Gotta do some other things as well, but right now I'm just...
8
3003
by: asd | last post by:
Hello all. I was doing some kind of development and in the test file I have something like this: ///// Test File ///// double x = sin(45); The output for that statement is:
70
7322
by: Adam | last post by:
Hi, I am using the following code: printf("%g", sin(M_PI)) and getting 1.22461e-16 instead of zero. Does anyone have any idea why, and what I can do about it? Thanks, Adam
318
10910
by: King Raz | last post by:
The shootout site has benchmarks comparing different languages. It includes C# Mono vs Java but not C# .NET vs Java. So I went through all the benchmark on the site ... http://kingrazi.blogspot.com/2008/05/shootout-c-net-vs-java-benchmarks.html Just to keep the post on topic for my friends at comp.lang.c++, how do I play default windows...
0
8205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.