473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

x.sin() versus sin(x)

Hello,

I have the following code:

<code>
class X {
___ sin();
}

X sin( X ) { return sin( y, x ); }
</code>

Then what should X::sin() do and return? Should it do the same, that is
sin( y, x ) and return y, or should it do sin( x, x ), that is compute the
sine of itself and store it in itself?

I am stuck in choosing either of these, so I would like to know your
opinion on this, and/or some examples of classes which have both these
functionalities.

Thanks in advance for any reply.

Regards,

Franky B.
Jul 19 '05 #1
7 4862
fr***************@ua.ac.be wrote in message news:<Pi*******************************@leibniz.ru ca.ua.ac.be>...
Hello,

I have the following code:

<code>
class X {
___ sin();
}

X sin( X ) { return sin( y, x ); }
</code>

Then what should X::sin() do and return? Should it do the same, that is
sin( y, x ) and return y, or should it do sin( x, x ), that is compute the
sine of itself and store it in itself?

I am stuck in choosing either of these, so I would like to know your
opinion on this, and/or some examples of classes which have both these
functionalities.

Thanks in advance for any reply.

Regards,

Franky B.

Your code isn't compilable. I cannot even understand what you ask.
What is sin(y, x)? Never heared of a sine that has two arguments.
Moreover, why reinventing the wheel? The standard libraries come with
a sin function. Please clarify.
Jul 19 '05 #2

The sin function should *return* its result, not store it in an output
parameter. It should be defined something like this:

float sin( float x );

or

double sin( double x );
I see no reason it should be a member of a class, either. The sin function
operates on a single value and returns a single value, so there's no reason
to have it a class member. But if, for some reason, you needed such a
function, then it should still return the result, not store it in a
parameter.

-Howard

P.S., what's with the three leading underscores before the function name?
And where's the return type?

Jul 19 '05 #3
On Mon, 8 Sep 2003, Dan Cernat wrote:
I have the following code:

<code>
class X {
___ sin();
}

X sin( X ) { X y; sin( y, x ); return y; }
</code>


Your code isn't compilable. I cannot even understand what you ask.
What is sin(y, x)? Never heared of a sine that has two arguments.
Moreover, why reinventing the wheel? The standard libraries come with
a sin function. Please clarify.


The function sin( y, x ) computes the sine of x and stores the result in
y. Indeed, the code seems to be wrong; I corrected it above. That is,
the function outside the class is to be used as in y = sin(x).

The question is what the function X::sin() should do when it occurs in the
class as given in the example. More precisely, should x.sin() compute the
sine of x and store the result back into x, or should it behave as y =
sin(x), that is x.sin() computes the sine of x and returns the result as a
new object of class X?

My apologies for the apparant mistake.

Thanks for any reply.

Regards,

Franky B.
Jul 19 '05 #4
On Mon, 8 Sep 2003, Howard wrote:
The sin function should *return* its result, not store it in an output
parameter. It should be defined something like this:

float sin( float x ); or double sin( double x );

I see no reason it should be a member of a class, either. The sin function
operates on a single value and returns a single value, so there's no reason
to have it a class member. But if, for some reason, you needed such a
function, then it should still return the result, not store it in a
parameter.
It feels strange, I know, but now I have a class which provides these
functions as member functions, and also as non-member functions. While
the non-member function behaves as in y = sin(x), that is it computes the
sine of x and returns the result (so it can be assigned to y), I thought
the member function would behave as in x = x.sin(), that is computing the
sine of the object and storing the result back in x. Otherwise, I would
see no reason to have these member functions at all.
P.S., what's with the three leading underscores before the function
name? And where's the return type?


That depends on what it should do. It is either X& or void, depending on
whether it should return the result or not (I believe it should not return
the result if the behavior is as in x = sin(x)).

Regards,

Franky B.
Jul 19 '05 #5
>
It feels strange, I know, but now I have a class which provides these
functions as member functions, and also as non-member functions. While
the non-member function behaves as in y = sin(x), that is it computes the
sine of x and returns the result (so it can be assigned to y), I thought
the member function would behave as in x = x.sin(), that is computing the
sine of the object and storing the result back in x. Otherwise, I would
see no reason to have these member functions at all.
P.S., what's with the three leading underscores before the function
name? And where's the return type?


That depends on what it should do. It is either X& or void, depending on
whether it should return the result or not (I believe it should not return
the result if the behavior is as in x = sin(x)).

I cannot imagine when you would ever write "x = sin(x)". The input
parameter is a value in degreees or radians, and the output is a value
between -1 and 1.

If you have some legitimate reason to modify x itself by computing its sine,
then feel free to do so. As to whether such a function should return a copy
of x or simply modify x, it all depends on how you intend to use it. If
you're going to want to write it as part of a longer equation (assignment
statement), such as "y = 3 * sin(x)", then you'll obviously want it to
return a useable value.

If you're *never* going to want to do that, then you can just as easily have
a void return type and modify the object in place. But in that case, you
might want to change the name from "sin" to "ConvertToSine" or something
similar, since using the name "sin" implies the usual mathematical function,
not a procedural call.

-Howard

Jul 19 '05 #6
franky backeljauw wrote:
I have the following code:

<code>
class X {
X sin(void) const;
};

X sin(const X& x) { return sin(y, x); }
</code>

Then what should X::sin(void) const do and return? Should it do the same
that is sin(y, x) and return y, or should it do sin(x, x)
that is compute the sine of itself and store it in itself?

I am stuck in choosing either of these
so I would like to know your opinion on this
and/or some examples of classes which have both these functionalities.


const X x;
assert(x.sin() == sin(x); // always true

Jul 19 '05 #7
On Mon, 8 Sep 2003 20:50:11 +0200, fr***************@ua.ac.be wrote:
On Mon, 8 Sep 2003, Howard wrote:
The sin function should *return* its result, not store it in an output
parameter. It should be defined something like this:

float sin( float x ); or double sin( double x );

I see no reason it should be a member of a class, either. The sin function
operates on a single value and returns a single value, so there's no reason
to have it a class member. But if, for some reason, you needed such a
function, then it should still return the result, not store it in a
parameter.
It feels strange, I know, but now I have a class which provides these
functions as member functions, and also as non-member functions. While
the non-member function behaves as in y = sin(x), that is it computes the
sine of x and returns the result (so it can be assigned to y), I thought
the member function would behave as in x = x.sin(), that is computing the
sine of the object and storing the result back in x. Otherwise, I would
see no reason to have these member functions at all.


Right, drop the member functions (making the non-members friends if
necessary).
P.S., what's with the three leading underscores before the function
name? And where's the return type?


That depends on what it should do. It is either X& or void, depending on
whether it should return the result or not (I believe it should not return
the result if the behavior is as in x = sin(x)).


I presume you mean "X or void", not "X& or void".

It would be counter-intuitive to have sin be a member function that
modifies the "this" pointer. The intuitive version is a non-member
that returns the sine of the argument.

Tom
Jul 19 '05 #8

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

Similar topics

14
by: Alexander Stippler | last post by:
Hi, simple question. Why does this not work: #include <algorithm> #include <cmath> #include <iostream> int main()
6
by: Victo | last post by:
I tried to use pow() and sin(). but even I included <math.h>, I can't use the 2 functions.... Why's that???? for example,
10
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) {
15
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?...
0
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...
8
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
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
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 ... ...
3
by: mariab | last post by:
hi, i'm having the problem with my first socket program...i have been staring at it for an hour but i cant find whats wrong with it...can u check it out pls? #include <stdio.h> #include...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.