Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #14772 > unrolled thread

use of pointers

Started byahso <ahso47@yahoo.com>
First post2012-03-06 02:54 -0800
Last post2012-03-07 15:37 +0000
Articles 20 on this page of 33 — 17 participants

Back to article view | Back to comp.lang.c++


Contents

  use of pointers ahso <ahso47@yahoo.com> - 2012-03-06 02:54 -0800
    Re: use of pointers Marcel Müller <news.5.maazl@spamgourmet.com> - 2012-03-06 12:22 +0100
      Re: use of pointers nick_keighley_nospam@hotmail.com - 2012-03-07 00:32 -0800
        Re: use of pointers Juha Nieminen <nospam@thanks.invalid> - 2012-03-07 13:47 +0000
          Re: use of pointers nick_keighley_nospam@hotmail.com - 2012-03-07 07:06 -0800
            Re: use of pointers Juha Nieminen <nospam@thanks.invalid> - 2012-03-07 16:04 +0000
    Re: use of pointers Juha Nieminen <nospam@thanks.invalid> - 2012-03-06 12:29 +0000
      Re: use of pointers Victor Bazarov <v.bazarov@comcast.invalid> - 2012-03-06 08:04 -0500
        Re: use of pointers glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-03-06 14:05 +0000
          Re: use of pointers Victor Bazarov <v.bazarov@comcast.invalid> - 2012-03-06 11:25 -0500
            Re: use of pointers glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-03-06 20:56 +0000
        Re: use of pointers SG <s.gesemann@gmail.com> - 2012-03-06 06:27 -0800
          Re: use of pointers BGB <cr88192@hotmail.com> - 2012-03-06 13:05 -0700
          Re: use of pointers woodbrian77@gmail.com - 2012-03-07 05:38 -0800
        Re: use of pointers Rui Maciel <rui.maciel@gmail.com> - 2012-03-07 15:48 +0000
          Re: use of pointers BGB <cr88192@hotmail.com> - 2012-03-07 18:38 -0700
            Re: use of pointers Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2012-03-10 21:10 -0500
              Re: use of pointers Jeff Flinn <TriumphSprint2000@hotmail.com> - 2012-03-12 08:26 -0400
                Re: use of pointers Jeff Flinn <TriumphSprint2000@hotmail.com> - 2012-03-12 12:28 -0400
        Re: use of pointers Nobody <nobody@nowhere.com> - 2012-03-09 21:28 +0000
    Re: use of pointers Geoff <geoff@invalid.invalid> - 2012-03-06 21:31 -0800
      Re: use of pointers Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-03-07 11:26 +0000
        Re: use of pointers yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2012-03-07 13:56 +0000
          Re: use of pointers Maarten <maarten.sneep@knmi.nl> - 2012-03-07 06:14 -0800
            Re: use of pointers Rui Maciel <rui.maciel@gmail.com> - 2012-03-07 16:05 +0000
              Re: use of pointers yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2012-03-08 10:08 +0000
                Re: use of pointers Rui Maciel <rui.maciel@gmail.com> - 2012-03-08 21:17 +0000
              Re: use of pointers Maarten <maarten.sneep@knmi.nl> - 2012-03-08 04:55 -0800
            Re: use of pointers Nobody <nobody@nowhere.com> - 2012-03-09 21:45 +0000
          Re: use of pointers Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-03-07 20:08 +0000
        Re: use of pointers Nobody <nobody@nowhere.com> - 2012-03-09 21:50 +0000
          Re: use of pointers Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-03-09 22:37 +0000
    Re: use of pointers Rui Maciel <rui.maciel@gmail.com> - 2012-03-07 15:37 +0000

Page 1 of 2  [1] 2  Next page →


#14772 — use of pointers

Fromahso <ahso47@yahoo.com>
Date2012-03-06 02:54 -0800
Subjectuse of pointers
Message-ID<0ea866f3-d443-4014-93fe-507d76ee6528@em9g2000vbb.googlegroups.com>
Hi
I want to rewrite a function as below to not use pointers:

void NewPos(float angle, float speed, float *x, float *y){

	angle = (angle-90.0)*0.0174532925199433;

	*x = (cos(angle) * speed);
	*y = (sin(angle) * speed);

}

I also don't know what this is doing exactly. Any explanations and
also why this needs pointers?
Many thanks

[toc] | [next] | [standalone]


#14773

FromMarcel Müller <news.5.maazl@spamgourmet.com>
Date2012-03-06 12:22 +0100
Message-ID<4f55f361$0$7614$9b4e6d93@newsspool1.arcor-online.net>
In reply to#14772
On 06.03.2012 11:54, ahso wrote:
> Hi
> I want to rewrite a function as below to not use pointers:
>
> void NewPos(float angle, float speed, float *x, float *y){
>
> 	angle = (angle-90.0)*0.0174532925199433;
>
> 	*x = (cos(angle) * speed);
> 	*y = (sin(angle) * speed);
>
> }
>
> I also don't know what this is doing exactly. Any explanations and
> also why this needs pointers?

Well, it looks like old C code. The pointers are used as reference 
parameters in this case. The function in fact returns two values.

The C++ way to do so is
   void NewPos(float angle, float speed, float& x, float& y)
Now you pass x and y explicitely as references. Of course, you need to 
remove the * before x and y in the function and most probably & where 
the function is called.

A even better code might return a structure consisting of x and y.

By the way: the magic number 0.017... is bad programming style, since no 
one knows what it is good for. You should write 2*M_PI/360. which is 
more clearly a conversion between degrees and radians.


Marcel

[toc] | [prev] | [next] | [standalone]


#14794

Fromnick_keighley_nospam@hotmail.com
Date2012-03-07 00:32 -0800
Message-ID<16669570.7078.1331109163701.JavaMail.geo-discussion-forums@vbbed8>
In reply to#14773
On Tuesday, March 6, 2012 11:22:09 AM UTC, Marcel Müller wrote:
> On 06.03.2012 11:54, ahso wrote:

<snip>

> By the way: the magic number 0.017... is bad programming style, since no 
> one knows what it is good for. You should write 2*M_PI/360. which is 
> more clearly a conversion between degrees and radians.

or even better

inline double degreesToRadians (double d)
{
   return d * M_PI / 180.0;
}

[toc] | [prev] | [next] | [standalone]


#14803

FromJuha Nieminen <nospam@thanks.invalid>
Date2012-03-07 13:47 +0000
Message-ID<4f5766f9$0$4351$7b1e8fa0@news.nbl.fi>
In reply to#14794
nick_keighley_nospam@hotmail.com wrote:
> or even better
> 
> inline double degreesToRadians (double d)
> {
>   return d * M_PI / 180.0;
> }

  The compiler will most probably not replace "M_PI / 180.0" with its
result at compile time in the above expression, unless you specifically
set optimizations that allow it (eg. -ffast-math). If you want to avoid
the runtime division, do it like this:

    return d * (M_PI / 180.0);

  Now the compiler is allowed to do the division at compile time.
(OTOH, you should not assume that all compilers will do that when
dealing with floating point...)

[toc] | [prev] | [next] | [standalone]


#14807

Fromnick_keighley_nospam@hotmail.com
Date2012-03-07 07:06 -0800
Message-ID<21204423.216.1331132769074.JavaMail.geo-discussion-forums@vbfl9>
In reply to#14803
On Wednesday, March 7, 2012 1:47:37 PM UTC, Juha Nieminen wrote:
> nick_keighley wrote:
> > or even better
> > 
> > inline double degreesToRadians (double d)
> > {
> >   return d * M_PI / 180.0;
> > }
> 
>   The compiler will most probably not replace "M_PI / 180.0" with its
> result at compile time in the above expression, unless you specifically
> set optimizations that allow it (eg. -ffast-math). If you want to avoid
> the runtime division, do it like this:
> 
>     return d * (M_PI / 180.0);
> 
>   Now the compiler is allowed to do the division at compile time.
> (OTOH, you should not assume that all compilers will do that when
> dealing with floating point...)

inline double degreesToRadians (double d)
{
   const double radsPerDeg = M_PI / 180.0;
   return d * radsPerDeg;
}


[toc] | [prev] | [next] | [standalone]


#14810

FromJuha Nieminen <nospam@thanks.invalid>
Date2012-03-07 16:04 +0000
Message-ID<4f57870f$0$2791$7b1e8fa0@news.nbl.fi>
In reply to#14807
nick_keighley_nospam@hotmail.com wrote:
> inline double degreesToRadians (double d)
> {
>   const double radsPerDeg = M_PI / 180.0;
>   return d * radsPerDeg;
> }

  That still doesn't guarantee that the compiler will calculate the
division at compile time any more than with my version. Remember
that a 'double' isn't a compile-time constant in C++98.

  It's probably different in C++11, where you can use:

    constexpr double radPerDeg = M_PI / 180.0;

  Here the compiler has no choice (AFAIK) than to perform the calcuation
at compile time.

[toc] | [prev] | [next] | [standalone]


#14774

FromJuha Nieminen <nospam@thanks.invalid>
Date2012-03-06 12:29 +0000
Message-ID<4f560345$0$3087$7b1e8fa0@news.nbl.fi>
In reply to#14772
ahso <ahso47@yahoo.com> wrote:
> Hi
> I want to rewrite a function as below to not use pointers:
> 
> void NewPos(float angle, float speed, float *x, float *y){
> 
>        angle = (angle-90.0)*0.0174532925199433;
> 
>        *x = (cos(angle) * speed);
>        *y = (sin(angle) * speed);
> 
> }

  Since floats are small basic types, it's much, much cleaner (and
equally efficient) to do this:

struct Point { float x, y; };

Point NewPos(float angle, float speed)
{
    angle = (angle - 90.0f) * 0.0174532925199433f;
    Point p = { std::cos(angle) * speed, std::sin(angle) * speed };
    return p;
}

  (With C++11 you can skip creating the local variable 'p' and return
the initializer list directly.)

[toc] | [prev] | [next] | [standalone]


#14775

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2012-03-06 08:04 -0500
Message-ID<jj521m$ih2$1@dont-email.me>
In reply to#14774
On 3/6/2012 7:29 AM, Juha Nieminen wrote:
> ahso<ahso47@yahoo.com>  wrote:
>> Hi
>> I want to rewrite a function as below to not use pointers:
>>
>> void NewPos(float angle, float speed, float *x, float *y){
>>
>>         angle = (angle-90.0)*0.0174532925199433;
>>
>>         *x = (cos(angle) * speed);
>>         *y = (sin(angle) * speed);
>>
>> }
>
>    Since floats are small basic types, it's much, much cleaner (and
> equally efficient) to do this:
>
> struct Point { float x, y; };
>
> Point NewPos(float angle, float speed)
> {
>      angle = (angle - 90.0f) * 0.0174532925199433f;
>      Point p = { std::cos(angle) * speed, std::sin(angle) * speed };
>      return p;
> }
>
>    (With C++11 you can skip creating the local variable 'p' and return
> the initializer list directly.)

The name 'Point' is misleading IMNSHO and so is the name of the 
function.  What is returned is a velocity vector, not a position.

And I am not so sure one couldn't just return std::pair<float,float> 
since it essentially does the same thing, except that its members are 
not called 'x' and 'y'...  Why invent another type when a standard one 
exists?

V
-- 
I do not respond to top-posted replies, please don't ask

[toc] | [prev] | [next] | [standalone]


#14777

Fromglen herrmannsfeldt <gah@ugcs.caltech.edu>
Date2012-03-06 14:05 +0000
Message-ID<jj55ii$c4e$2@speranza.aioe.org>
In reply to#14775
Victor Bazarov <v.bazarov@comcast.invalid> wrote:

(snip, someone wrote)
>>    Since floats are small basic types, it's much, much cleaner (and
>> equally efficient) to do this:

>> struct Point { float x, y; };

(snip)
> The name 'Point' is misleading IMNSHO and so is the name of the 
> function.  What is returned is a velocity vector, not a position.

I suppose, but you could just consider it a point in velocity space.

> And I am not so sure one couldn't just return std::pair<float,float> 
> since it essentially does the same thing, except that its members are 
> not called 'x' and 'y'...  Why invent another type when a standard one 
> exists?

But velocity also has x and y components, so Point is fine.

-- glen

[toc] | [prev] | [next] | [standalone]


#14782

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2012-03-06 11:25 -0500
Message-ID<jj5dpp$rov$1@dont-email.me>
In reply to#14777
On 3/6/2012 9:05 AM, glen herrmannsfeldt wrote:
> Victor Bazarov<v.bazarov@comcast.invalid>  wrote:
>
> (snip, someone wrote)
>>>     Since floats are small basic types, it's much, much cleaner (and
>>> equally efficient) to do this:
>
>>> struct Point { float x, y; };
>
> (snip)
>> The name 'Point' is misleading IMNSHO and so is the name of the
>> function.  What is returned is a velocity vector, not a position.
>
> I suppose, but you could just consider it a point in velocity space.

I could.  But somebody who will likely assign a velocity "point" to a 
two-dimensional spatial (planar) point, shouldn't.  There is no "space" 
designation in the type, is there?

>> And I am not so sure one couldn't just return std::pair<float,float>
>> since it essentially does the same thing, except that its members are
>> not called 'x' and 'y'...  Why invent another type when a standard one
>> exists?
>
> But velocity also has x and y components, so Point is fine.

<sigh> It isn't.  But I don't see any use of arguing.  Not anymore. 
Sorry I brought it up.

V
-- 
I do not respond to top-posted replies, please don't ask

[toc] | [prev] | [next] | [standalone]


#14790

Fromglen herrmannsfeldt <gah@ugcs.caltech.edu>
Date2012-03-06 20:56 +0000
Message-ID<jj5tm8$faa$1@speranza.aioe.org>
In reply to#14782
Victor Bazarov <v.bazarov@comcast.invalid> wrote:
 (snip, someone wrote)
>>> The name 'Point' is misleading IMNSHO and so is the name of the
>>> function.  What is returned is a velocity vector, not a position.

>> I suppose, but you could just consider it a point in velocity space.

> I could.  But somebody who will likely assign a velocity "point" to a 
> two-dimensional spatial (planar) point, shouldn't.  There is no "space" 
> designation in the type, is there?

I expect that you will find that some people think one way one
some the other. Note that it is unusual (there might be one) for
programming languages to keep track of dimensional units.

Physics has, in addition to velocity space, which might not seem
too strange, momentum space. There are some problems that can easily
be solved if you think of them in momentum space. (Even 
relativistically when it isn't mass times velocity anymore.)

>>> And I am not so sure one couldn't just return std::pair<float,float>
>>> since it essentially does the same thing, except that its members are
>>> not called 'x' and 'y'...  Why invent another type when a standard one
>>> exists?

>> But velocity also has x and y components, so Point is fine.

> <sigh> It isn't.  But I don't see any use of arguing.  Not anymore. 
> Sorry I brought it up.

I have thought for some time now, that it is more usual for
physicists to include units in their variables, and for engineers
to factor them out.

In a physics book you will see F=ma.

In an engineering book F(Newtons) = m(kg) * a (m/s**2)

In the latter case, the variable holds the unitless value that,
when the specified unit is applied, has the appropriate value
and dimensions.

Now, with a name like Point, one could ask why it is 2D and
not 3D, or even 4D. You could name the class Vector2D, though.

-- glen

[toc] | [prev] | [next] | [standalone]


#14778

FromSG <s.gesemann@gmail.com>
Date2012-03-06 06:27 -0800
Message-ID<18fbdfe6-2f67-4ab8-a9f0-755ee0411b00@db5g2000vbb.googlegroups.com>
In reply to#14775
On 6 Mrz., 14:04, Victor Bazarov wrote:
> The name 'Point' is misleading IMNSHO

I think it's okay. I don't see the benefit of creating two types (for
points and vectors) with basically the same interface. And since we
already have vector<> in std:: ...

> Why invent another type when a standard one exists?

Because even "point" says more than "pair<float,float>" to the human
source code reader.

Cheers!
SG

[toc] | [prev] | [next] | [standalone]


#14788

FromBGB <cr88192@hotmail.com>
Date2012-03-06 13:05 -0700
Message-ID<jj5qpo$9c1$1@news.albasani.net>
In reply to#14778
On 3/6/2012 7:27 AM, SG wrote:
> On 6 Mrz., 14:04, Victor Bazarov wrote:
>> The name 'Point' is misleading IMNSHO
>
> I think it's okay. I don't see the benefit of creating two types (for
> points and vectors) with basically the same interface. And since we
> already have vector<>  in std:: ...
>
>> Why invent another type when a standard one exists?
>
> Because even "point" says more than "pair<float,float>" to the human
> source code reader.
>

I personally used vec2/vec3/vec4/quat (each as a struct), following a 
convention more like in GLSL, except that most operations have a 
v2/v3/v4 prefix (say: "v3dot(v0, v1)", ...), and some many new ones were 
added (note that, a lot of these are technically macros which redirect 
the calls to other functions, and many others are "static inline" and 
defined solely in the header).

one could potentially use classes and be like: "v0.dot(v1)", but this 
would look kind of funky (and also wouldn't be C-friendly).

as-is, when used from C++, it does gain some overloaded operators and 
similar.


but, a lot of the older code still follows a convention of passing 
around float-array pointers, but this convention is (gradually) being 
phased out in many places (due mostly to being more awkward and 
error-prone).

or such...

[toc] | [prev] | [next] | [standalone]


#14802

Fromwoodbrian77@gmail.com
Date2012-03-07 05:38 -0800
Message-ID<4746869.4082.1331127495191.JavaMail.geo-discussion-forums@yncc26>
In reply to#14778
On Tuesday, March 6, 2012 8:27:18 AM UTC-6, SG wrote:
> On 6 Mrz., 14:04, Victor Bazarov wrote:
> > The name 'Point' is misleading IMNSHO
> 
> I think it's okay. I don't see the benefit of creating two types (for
> points and vectors) with basically the same interface. And since we
> already have vector<> in std:: ...
> 
> > Why invent another type when a standard one exists?
> 
> Because even "point" says more than "pair<float,float>" to the human
> source code reader.
> 

I'd prefer the point also over a pair.  I'm not keen 
on the first and second names.  One disappoint with
the latest standard is there's still not a good
alternative to std::map.  

Brian
Ebenezer Enterprises
http://webEbenezer.net

[toc] | [prev] | [next] | [standalone]


#14809

FromRui Maciel <rui.maciel@gmail.com>
Date2012-03-07 15:48 +0000
Message-ID<jj800l$lko$1@speranza.aioe.org>
In reply to#14775
Victor Bazarov wrote:

> The name 'Point' is misleading IMNSHO and so is the name of the
> function.  What is returned is a velocity vector, not a position.
> 
> And I am not so sure one couldn't just return std::pair<float,float>
> since it essentially does the same thing, except that its members are
> not called 'x' and 'y'...  Why invent another type when a standard one
> exists?

User-defined data types improve code readability.  Under some circumstances, 
it also help reduce the propensity to inadvertently add bugs to the code.


Rui Maciel

[toc] | [prev] | [next] | [standalone]


#14824

FromBGB <cr88192@hotmail.com>
Date2012-03-07 18:38 -0700
Message-ID<jj92lq$51o$1@news.albasani.net>
In reply to#14809
On 3/7/2012 8:48 AM, Rui Maciel wrote:
> Victor Bazarov wrote:
>
>> The name 'Point' is misleading IMNSHO and so is the name of the
>> function.  What is returned is a velocity vector, not a position.
>>
>> And I am not so sure one couldn't just return std::pair<float,float>
>> since it essentially does the same thing, except that its members are
>> not called 'x' and 'y'...  Why invent another type when a standard one
>> exists?
>
> User-defined data types improve code readability.  Under some circumstances,
> it also help reduce the propensity to inadvertently add bugs to the code.
>

yep, and in many cases where points/vectors are in use, they are a 
fairly fundamental building-block type, rather than an "afterthought" 
(or "misc") type, making them more suited to having their own dedicated 
type-names and their own dedicated logic.


granted, a lot depends on the particular domain as well.
for example, I am prone to think of a "money" or "date" types as fairly 
misc, since I don't use them much for what I do, but there are people 
who write types of software where such things make a good deal more 
sense (and languages like SQL treat both as fundamental types).

nevermind other strangeness (observed in accounting contexts): thinking 
as dates strictly in terms of days, or even ignoring the existence of 
things like leap years and similar in calculations (vs the more "sane" 
option of, say, thinking as time more strictly in terms of an absolute 
number of seconds, as a real value, between events).

but, it could be argued that maybe for the accountant, there is little 
reason for the accountant to care if "this time next year" may easily 
differ by hours or maybe days from "this time next year" as far as a 
scientist or engineer would be concerned (in terms of the number of 
seconds between "now" and "this date and time exactly one year from 
now"...).

or, for that matter, mentioning doing something "tomorrow" when it is 
after midnight, and said "tomorrow" is maybe only 10 or 14 hours away 
and technically the same day (as far as the calender is concerned...).



and, say, if one writes a 3D engine, they will be seeing points, 
vectors, matrices, ... quite a fair amount.

so, yeah, sometimes it makes a lot of sense to make dedicated types for 
these sorts of things.

[toc] | [prev] | [next] | [standalone]


#14876

FromPavel <pauldontspamtolk@removeyourself.dontspam.yahoo>
Date2012-03-10 21:10 -0500
Message-ID<4f5c09a2$0$1841$c3e8da3$b1356c67@news.astraweb.com>
In reply to#14824
BGB wrote:
> On 3/7/2012 8:48 AM, Rui Maciel wrote:
>> Victor Bazarov wrote:
>>
>>> The name 'Point' is misleading IMNSHO and so is the name of the
>>> function. What is returned is a velocity vector, not a position.
>>>
>>> And I am not so sure one couldn't just return std::pair<float,float>
>>> since it essentially does the same thing, except that its members are
>>> not called 'x' and 'y'... Why invent another type when a standard one
>>> exists?
>>
>> User-defined data types improve code readability. Under some circumstances,
>> it also help reduce the propensity to inadvertently add bugs to the code.
>>
>
> yep, and in many cases where points/vectors are in use, they are a fairly
> fundamental building-block type, rather than an "afterthought" (or "misc") type,
> making them more suited to having their own dedicated type-names and their own
> dedicated logic.
>
>
> granted, a lot depends on the particular domain as well.
> for example, I am prone to think of a "money" or "date" types as fairly misc,
> since I don't use them much for what I do, but there are people who write types
> of software where such things make a good deal more sense (and languages like
> SQL treat both as fundamental types).
>
> nevermind other strangeness (observed in accounting contexts): thinking as dates
> strictly in terms of days, or even ignoring the existence of things like leap
> years and similar in calculations (vs the more "sane" option of, say, thinking
> as time more strictly in terms of an absolute number of seconds, as a real
> value, between events).
>
> but, it could be argued that maybe for the accountant, there is little reason
> for the accountant to care if "this time next year" may easily differ by hours
> or maybe days from "this time next year" as far as a scientist or engineer would
> be concerned (in terms of the number of seconds between "now" and "this date and
> time exactly one year from now"...).
>
> or, for that matter, mentioning doing something "tomorrow" when it is after
> midnight, and said "tomorrow" is maybe only 10 or 14 hours away and technically
> the same day (as far as the calender is concerned...).
>
>
>
> and, say, if one writes a 3D engine, they will be seeing points, vectors,
> matrices, ... quite a fair amount.
>
> so, yeah, sometimes it makes a lot of sense to make dedicated types for these
> sorts of things.
>
I think those suggesting change in the interface other than replacing pointers 
with references spoke without trying to understand how the function is used.

For example, if the call site converts separate arrays of the two velocity 
components, changing from output parameters to return type would do more harm 
than good.

On a side note, even though I am not crazy about using pointers that shall not 
be NULLs, I tend to think twice before changing existing APIs from pointers to 
references: if significant amount of client code is "C-style" or just follows 
the convention of always using pointers for output parameters, its maintainers 
may assume that the arguments passed for x and y are values and misread the 
code's intention.

I am seriously thinking of wrapping my passed-by-referenced parameters in "Ref" 
objects with explicit constructors -- with the only goal to better document call 
sites  (e.g. NewPos(angle, speed, FloatRef(x), FloatRef(y));). Does anybody know 
anything existing of the kind?

As for the OP's question, I would seriously think of leaving the interface as it 
is -- at least until there is a domain-area-originated reason for changing it.

-Pavel

[toc] | [prev] | [next] | [standalone]


#14906

FromJeff Flinn <TriumphSprint2000@hotmail.com>
Date2012-03-12 08:26 -0400
Message-ID<jjkq46$c93$1@dont-email.me>
In reply to#14876
Pavel wrote:
> BGB wrote:
>> On 3/7/2012 8:48 AM, Rui Maciel wrote:
>>> Victor Bazarov wrote:
>>>
>>>> The name 'Point' is misleading IMNSHO and so is the name of the
>>>> function. What is returned is a velocity vector, not a position.
>>>>
>>>> And I am not so sure one couldn't just return std::pair<float,float>
>>>> since it essentially does the same thing, except that its members are
>>>> not called 'x' and 'y'... Why invent another type when a standard one
>>>> exists?
>>>
>>> User-defined data types improve code readability. Under some 
>>> circumstances,
>>> it also help reduce the propensity to inadvertently add bugs to the 
>>> code.
>>>
>>
>> yep, and in many cases where points/vectors are in use, they are a fairly
>> fundamental building-block type, rather than an "afterthought" (or 
>> "misc") type,
>> making them more suited to having their own dedicated type-names and 
>> their own
>> dedicated logic.
>>
>>
>> granted, a lot depends on the particular domain as well.
>> for example, I am prone to think of a "money" or "date" types as 
>> fairly misc,
>> since I don't use them much for what I do, but there are people who 
>> write types
>> of software where such things make a good deal more sense (and 
>> languages like
>> SQL treat both as fundamental types).
>>
>> nevermind other strangeness (observed in accounting contexts): 
>> thinking as dates
>> strictly in terms of days, or even ignoring the existence of things 
>> like leap
>> years and similar in calculations (vs the more "sane" option of, say, 
>> thinking
>> as time more strictly in terms of an absolute number of seconds, as a 
>> real
>> value, between events).
>>
>> but, it could be argued that maybe for the accountant, there is little 
>> reason
>> for the accountant to care if "this time next year" may easily differ 
>> by hours
>> or maybe days from "this time next year" as far as a scientist or 
>> engineer would
>> be concerned (in terms of the number of seconds between "now" and 
>> "this date and
>> time exactly one year from now"...).
>>
>> or, for that matter, mentioning doing something "tomorrow" when it is 
>> after
>> midnight, and said "tomorrow" is maybe only 10 or 14 hours away and 
>> technically
>> the same day (as far as the calender is concerned...).
>>
>>
>>
>> and, say, if one writes a 3D engine, they will be seeing points, vectors,
>> matrices, ... quite a fair amount.
>>
>> so, yeah, sometimes it makes a lot of sense to make dedicated types 
>> for these
>> sorts of things.
>>
> I think those suggesting change in the interface other than replacing 
> pointers with references spoke without trying to understand how the 
> function is used.
> 
> For example, if the call site converts separate arrays of the two 
> velocity components, changing from output parameters to return type 
> would do more harm than good.

std::tuple<float, float>
NewPos(float angle, float speed, float *x, float *y)
{

    angle = (angle-90.0)*0.0174532925199433;

    return std::make_tuple( cos(angle) * speed
                          , sin(angle) * speed);
}

int main()
{
    ...

    std::tie(x[i], y[i]) = NewPos(angle, speed);
}

I see no harm here, in fact the intent is more explicit.

Jeff

[toc] | [prev] | [next] | [standalone]


#14916

FromJeff Flinn <TriumphSprint2000@hotmail.com>
Date2012-03-12 12:28 -0400
Message-ID<jjl88s$34i$1@dont-email.me>
In reply to#14906
Jeff Flinn wrote:
> Pavel wrote:
>> BGB wrote:
>>> On 3/7/2012 8:48 AM, Rui Maciel wrote:
>>>> Victor Bazarov wrote:
>>>>
>>>>> The name 'Point' is misleading IMNSHO and so is the name of the
>>>>> function. What is returned is a velocity vector, not a position.
>>>>>
>>>>> And I am not so sure one couldn't just return std::pair<float,float>
>>>>> since it essentially does the same thing, except that its members are
>>>>> not called 'x' and 'y'... Why invent another type when a standard one
>>>>> exists?
>>>>
>>>> User-defined data types improve code readability. Under some 
>>>> circumstances,
>>>> it also help reduce the propensity to inadvertently add bugs to the 
>>>> code.
>>>>
>>>
>>> yep, and in many cases where points/vectors are in use, they are a 
>>> fairly
>>> fundamental building-block type, rather than an "afterthought" (or 
>>> "misc") type,
>>> making them more suited to having their own dedicated type-names and 
>>> their own
>>> dedicated logic.
>>>
>>>
>>> granted, a lot depends on the particular domain as well.
>>> for example, I am prone to think of a "money" or "date" types as 
>>> fairly misc,
>>> since I don't use them much for what I do, but there are people who 
>>> write types
>>> of software where such things make a good deal more sense (and 
>>> languages like
>>> SQL treat both as fundamental types).
>>>
>>> nevermind other strangeness (observed in accounting contexts): 
>>> thinking as dates
>>> strictly in terms of days, or even ignoring the existence of things 
>>> like leap
>>> years and similar in calculations (vs the more "sane" option of, say, 
>>> thinking
>>> as time more strictly in terms of an absolute number of seconds, as a 
>>> real
>>> value, between events).
>>>
>>> but, it could be argued that maybe for the accountant, there is 
>>> little reason
>>> for the accountant to care if "this time next year" may easily differ 
>>> by hours
>>> or maybe days from "this time next year" as far as a scientist or 
>>> engineer would
>>> be concerned (in terms of the number of seconds between "now" and 
>>> "this date and
>>> time exactly one year from now"...).
>>>
>>> or, for that matter, mentioning doing something "tomorrow" when it is 
>>> after
>>> midnight, and said "tomorrow" is maybe only 10 or 14 hours away and 
>>> technically
>>> the same day (as far as the calender is concerned...).
>>>
>>>
>>>
>>> and, say, if one writes a 3D engine, they will be seeing points, 
>>> vectors,
>>> matrices, ... quite a fair amount.
>>>
>>> so, yeah, sometimes it makes a lot of sense to make dedicated types 
>>> for these
>>> sorts of things.
>>>
>> I think those suggesting change in the interface other than replacing 
>> pointers with references spoke without trying to understand how the 
>> function is used.
>>
>> For example, if the call site converts separate arrays of the two 
>> velocity components, changing from output parameters to return type 
>> would do more harm than good.
> 
> std::tuple<float, float>
> NewPos(float angle, float speed, float *x, float *y)

Oops that should be:
std::tuple<float, float> NewPos(float angle, float speed)

> {
> 
>    angle = (angle-90.0)*0.0174532925199433;
> 
>    return std::make_tuple( cos(angle) * speed
>                          , sin(angle) * speed);
> }
> 
> int main()
> {
>    ...
> 
>    std::tie(x[i], y[i]) = NewPos(angle, speed);
> }
> 
> I see no harm here, in fact the intent is more explicit.
> 
> Jeff

[toc] | [prev] | [next] | [standalone]


#14855

FromNobody <nobody@nowhere.com>
Date2012-03-09 21:28 +0000
Message-ID<pan.2012.03.09.21.28.39.261000@nowhere.com>
In reply to#14775
On Tue, 06 Mar 2012 08:04:54 -0500, Victor Bazarov wrote:

> And I am not so sure one couldn't just return std::pair<float,float> 
> since it essentially does the same thing, except that its members are 
> not called 'x' and 'y'...  Why invent another type when a standard one 
> exists?

1. Clarity. A specific type provides clues as to the meaning of the data.

2. Safety. If you have separate Position and Velocity types, you'll get a
type error if you try to use one where the other is required.

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.c++


csiph-web