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


Groups > comp.lang.c > #71692 > unrolled thread

Learning functions; roots of the quadratic; two return values

Started byAlla _ <modelling.data@gmail.com>
First post2015-10-03 10:43 -0700
Last post2015-10-06 07:34 +0200
Articles 20 on this page of 104 — 24 participants

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


Contents

  Learning functions; roots of the quadratic; two return values Alla _ <modelling.data@gmail.com> - 2015-10-03 10:43 -0700
    Re: Learning functions; roots of the quadratic; two return values Andrew Cooper <amc96@cam.ac.uk> - 2015-10-03 19:00 +0100
      Re: Learning functions; roots of the quadratic; two return values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-03 19:34 +0100
        Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-03 21:29 +0200
          Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-03 21:36 +0200
          Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-03 21:43 +0200
          Re: Learning functions; roots of the quadratic; two return values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-03 20:46 +0100
            Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-03 22:46 +0200
              Re: Learning functions; roots of the quadratic; two return values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-03 23:18 +0100
                Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-04 01:08 +0200
                Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-04 01:36 +0200
                  Re: Learning functions; roots of the quadratic; two return values Ian Collins <ian-news@hotmail.com> - 2015-10-04 12:52 +1300
                  Re: Learning functions; roots of the quadratic; two return values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-04 15:42 +0100
            Re: Learning functions; roots of the quadratic; two return values jacobnavia <jacob@jacob.remcomp.fr> - 2015-10-03 23:05 +0200
              Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-03 23:41 +0200
                Re: Learning functions; roots of the quadratic; two return values jacobnavia <jacob@jacob.remcomp.fr> - 2015-10-04 00:14 +0200
              Re: Learning functions; roots of the quadratic; two return values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-03 23:07 +0100
                Re: Learning functions; roots of the quadratic; two return values jacobnavia <jacob@jacob.remcomp.fr> - 2015-10-04 00:32 +0200
                Re: Learning functions; roots of the quadratic; two return values Keith Thompson <kst-u@mib.org> - 2015-10-04 12:56 -0700
                  Re: Learning functions; roots of the quadratic; two return values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-04 23:41 +0100
                    Re: Learning functions; roots of the quadratic; two return values Keith Thompson <kst-u@mib.org> - 2015-10-04 18:24 -0700
                Re: Learning functions; roots of the quadratic; two return values James Kuyper <jameskuyper@verizon.net> - 2015-10-04 16:36 -0400
                  Re: Learning functions; roots of the quadratic; two return values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-05 00:01 +0100
                    Re: Learning functions; roots of the quadratic; two return values James Kuyper <jameskuyper@verizon.net> - 2015-10-04 19:24 -0400
            Re: Learning functions; roots of the quadratic; two return values Ian Collins <ian-news@hotmail.com> - 2015-10-04 10:18 +1300
              Re: Learning functions; roots of the quadratic; two return values jacobnavia <jacob@jacob.remcomp.fr> - 2015-10-03 23:27 +0200
                Re: Learning functions; roots of the quadratic; two return values Ian Collins <ian-news@hotmail.com> - 2015-10-04 11:08 +1300
                  Re: Learning functions; roots of the quadratic; two return values Richard Damon <Richard@Damon-Family.org> - 2015-10-03 18:38 -0400
                    Re: Learning functions; roots of the quadratic; two return values Ian Collins <ian-news@hotmail.com> - 2015-10-04 11:47 +1300
            Re: Learning functions; roots of the quadratic; two return values raltbos@xs4all.nl (Richard Bos) - 2015-10-04 14:07 +0000
              Re: Learning functions; roots of the quadratic; two return values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-04 15:48 +0100
                Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-04 18:59 +0200
                  Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-04 20:06 +0200
                Re: Learning functions; roots of the quadratic; two return values Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-04 15:21 -0700
                  Re: Learning functions; roots of the quadratic; two return values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-05 05:25 +0000
          Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-03 21:48 +0200
            Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-04 00:31 +0200
      Re: Learning functions; roots of the quadratic; two return values alla.rashitova@gmail.com - 2015-10-05 08:40 -0700
        Re: Learning functions; roots of the quadratic; two return values Richard Heathfield <rjh@cpax.org.uk> - 2015-10-05 17:10 +0100
        Re: Learning functions; roots of the quadratic; two return values Barry Schwarz <schwarzb@dqel.com> - 2015-10-05 10:41 -0700
    Re: Learning functions; roots of the quadratic; two return values "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-10-03 12:04 -0700
      Re: Learning functions; roots of the quadratic; two return values Philip Lantz <prl@canterey.us> - 2015-10-03 12:48 -0700
      Re: Learning functions; roots of the quadratic; two return values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-03 20:55 +0100
        Re: Learning functions; roots of the quadratic; two return values "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-10-03 13:27 -0700
        Re: Learning functions; roots of the quadratic; two return values jacobnavia <jacob@jacob.remcomp.fr> - 2015-10-03 23:08 +0200
          Re: Learning functions; roots of the quadratic; two return values Richard Heathfield <rjh@cpax.org.uk> - 2015-10-03 22:52 +0100
      Re: Learning functions; roots of the quadratic; two return values Rosario19 <Ros@invalid.invalid> - 2015-10-04 20:46 +0200
        Re: Learning functions; roots of the quadratic; two return values Keith Thompson <kst-u@mib.org> - 2015-10-04 15:47 -0700
        Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-05 03:06 +0200
          Re: Learning functions; roots of the quadratic; two return values Rosario19 <Ros@invalid.invalid> - 2015-10-05 05:31 +0200
            Re: Learning functions; roots of the quadratic; two return values Rosario19 <Ros@invalid.invalid> - 2015-10-05 11:58 +0200
              Re: Learning functions; roots of the quadratic; two return values Rosario19 <Ros@invalid.invalid> - 2015-10-05 12:00 +0200
                Re: Learning functions; roots of the quadratic; two return values Rosario19 <Ros@invalid.invalid> - 2015-10-05 12:06 +0200
            Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-05 12:39 +0200
              Re: Learning functions; roots of the quadratic; two return values Bartc <bc@freeuk.com> - 2015-10-05 12:20 +0100
                Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-05 13:50 +0200
                Re: Learning functions; roots of the quadratic; two return values Richard Heathfield <rjh@cpax.org.uk> - 2015-10-05 12:57 +0100
          Re: Learning functions; roots of the quadratic; two return values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-05 05:40 +0000
            Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-05 08:30 +0200
            Re: Learning functions; roots of the quadratic; two return values Philip Lantz <prl@canterey.us> - 2015-10-05 10:03 -0700
              Re: Learning functions; roots of the quadratic; two return values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-05 22:25 +0000
                Re: Learning functions; roots of the quadratic; two return values James Kuyper <jameskuyper@verizon.net> - 2015-10-05 19:26 -0400
                  Re: Learning functions; roots of the quadratic; two return values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-06 03:44 +0000
                    Re: Learning functions; roots of the quadratic; two return values James Kuyper <jameskuyper@verizon.net> - 2015-10-06 10:55 -0400
                      Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-06 23:07 +0200
                        Re: Learning functions; roots of the quadratic; two return values James Kuyper <jameskuyper@verizon.net> - 2015-10-06 18:35 -0400
                          Re: Learning functions; roots of the quadratic; two return values raltbos@xs4all.nl (Richard Bos) - 2015-10-14 11:08 +0000
                          Re: Learning functions; roots of the quadratic; two return values James Kuyper <jameskuyper@verizon.net> - 2015-10-14 11:11 -0400
                            Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-14 22:22 +0200
                              Re: Learning functions; roots of the quadratic; two return values James Kuyper <jameskuyper@verizon.net> - 2015-10-14 17:17 -0400
                                Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-14 23:45 +0200
                            Re: Learning functions; roots of the quadratic; two return values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-15 00:11 +0000
                              Re: Learning functions; roots of the quadratic; two return values Bartc <bc@freeuk.com> - 2015-10-15 01:37 +0100
                                Re: Learning functions; roots of the quadratic; two return values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-15 05:52 +0000
                              Re: Learning functions; roots of the quadratic; two return values James Kuyper <jameskuyper@verizon.net> - 2015-10-14 21:54 -0400
                                Re: Learning functions; roots of the quadratic; two return values asetofsymbols@gmail.com - 2015-10-14 22:40 -0700
                                  Re: Learning functions; roots of the quadratic; two return values asetofsymbols@gmail.com - 2015-10-15 05:00 -0700
                                  Re: Learning functions; roots of the quadratic; two return values supercat@casperkitty.com - 2015-10-15 11:58 -0700
                                Re: Learning functions; roots of the quadratic; two return values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-15 06:04 +0000
                      Re: Learning functions; roots of the quadratic; two return values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-06 23:18 +0000
                        Re: Learning functions; roots of the quadratic; two return values supercat@casperkitty.com - 2015-10-06 16:31 -0700
                          Re: Learning functions; roots of the quadratic; two return values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-07 00:38 +0000
                            Re: Learning functions; roots of the quadratic; two return values supercat@casperkitty.com - 2015-10-06 21:19 -0700
                        Re: Learning functions; roots of the quadratic; two return values James Kuyper <jameskuyper@verizon.net> - 2015-10-06 22:15 -0400
                          Re: Learning functions; roots of the quadratic; two return values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-07 08:45 +0000
                        Re: Learning functions; roots of the quadratic; two return values Melzzzzz <mel@zzzzz.com> - 2015-10-07 05:19 +0200
                          Re: Learning functions; roots of the quadratic; two return values Keith Thompson <kst-u@mib.org> - 2015-10-07 09:12 -0700
                            Re: Learning functions; roots of the quadratic; two return values Melzzzzz <mel@zzzzz.com> - 2015-10-08 00:53 +0200
                              Re: Learning functions; roots of the quadratic; two return values Keith Thompson <kst-u@mib.org> - 2015-10-07 16:23 -0700
                Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-06 02:16 +0200
                  Re: Learning functions; roots of the quadratic; two return values supercat@casperkitty.com - 2015-10-06 15:43 -0700
                    Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-07 02:48 +0200
                      Re: Learning functions; roots of the quadratic; two return values Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-14 14:00 -0700
                        Re: Learning functions; roots of the quadratic; two return values regis <regis.barbanchon@free.fr> - 2015-10-14 23:23 +0200
    Re: Learning functions; roots of the quadratic; two return values Geoff <geoff@invalid.invalid> - 2015-10-03 12:50 -0700
    Re: Learning functions; roots of the quadratic; two return values Bartc <bc@freeuk.com> - 2015-10-03 21:16 +0100
      Re: Learning functions; roots of the quadratic; two return values alla.rashitova@gmail.com - 2015-10-05 09:02 -0700
        Re: Learning functions; roots of the quadratic; two return values Richard Heathfield <rjh@cpax.org.uk> - 2015-10-05 17:13 +0100
    Re: Learning functions; roots of the quadratic; two return values Barry Schwarz <schwarzb@dqel.com> - 2015-10-03 15:37 -0700
    Re: Learning functions; roots of the quadratic; two return values Rosario19 <Ros@invalid.invalid> - 2015-10-05 23:44 +0200
      Re: Learning functions; roots of the quadratic; two return values Geoff <geoff@invalid.invalid> - 2015-10-05 18:37 -0700
      Re: Learning functions; roots of the quadratic; two return values Barry Schwarz <schwarzb@dqel.com> - 2015-10-05 19:24 -0700
        Re: Learning functions; roots of the quadratic; two return values Rosario19 <Ros@invalid.invalid> - 2015-10-06 07:36 +0200
      Re: Learning functions; roots of the quadratic; two return values Rosario19 <Ros@invalid.invalid> - 2015-10-06 07:34 +0200

Page 3 of 6 — ← Prev page 1 2 [3] 4 5 6  Next page →


#71707

From"Rick C. Hodgin" <rick.c.hodgin@gmail.com>
Date2015-10-03 12:04 -0700
Message-ID<623fef72-989a-4973-9d04-b1a867a00703@googlegroups.com>
In reply to#71692
On Saturday, October 3, 2015 at 1:43:37 PM UTC-4, Alla _ wrote:
> Thank you!

Here is one solution to the issue of returning two values:

     float a, b, c, x1, x2;

     // Set a, b, c values somewhere

     // Solve
     if (solve_quadratic(a, b, c, x1, x2) >= 0)
     {
         // Valid, x1 and x2 are set
     }

-----
And the called function's code:

     // Function to find roots of a quadratic
     int solve_quadratic(float a, float b, float c, float& x1, float& x2)
     {
         float discriminant = pow(b,2) - 4 * a * c;
        
         if (discriminant < 0)
         {
             printf("Roots are imaginary\n");
             return -1;
         }

         // Compute
         x1 = (-b + approx_sqrt(discriminant)) / (2 * a);
         x2 = (-b - approx_sqrt(discriminant)) / (2 * a);

         // Success
         return 0;
     }

Best regards,
Rick C. Hodgin

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


#71721

FromPhilip Lantz <prl@canterey.us>
Date2015-10-03 12:48 -0700
Message-ID<MPG.3079d0e7287ff2ad6f@news.eternal-september.org>
In reply to#71707
Rick C. Hodgin wrote:
> 
> On Saturday, October 3, 2015 at 1:43:37 PM UTC-4, Alla _ wrote:
> > Thank you!
> 
> Here is one solution to the issue of returning two values:
> 
>      float a, b, c, x1, x2;
> 
>      // Set a, b, c values somewhere
> 
>      // Solve
>      if (solve_quadratic(a, b, c, x1, x2) >= 0)
>      {
>          // Valid, x1 and x2 are set
>      }
> 
> -----
> And the called function's code:
> 
>      // Function to find roots of a quadratic
>      int solve_quadratic(float a, float b, float c, float& x1, float& x2)
>      {
>          float discriminant = pow(b,2) - 4 * a * c;
>         
>          if (discriminant < 0)
>          {
>              printf("Roots are imaginary\n");
>              return -1;
>          }
> 
>          // Compute
>          x1 = (-b + approx_sqrt(discriminant)) / (2 * a);
>          x2 = (-b - approx_sqrt(discriminant)) / (2 * a);
> 
>          // Success
>          return 0;
>      }

This is comp.lang.c, not comp.lang.c++, so that should be:

     float a, b, c, x1, x2;

     // Set a, b, c values somewhere

     // Solve
     if (solve_quadratic(a, b, c, &x1, &x2) >= 0)
     {
         // Valid, x1 and x2 are set
     }

-----
And the called function's code:

     // Function to find roots of a quadratic
     int solve_quadratic(float a, float b, float c,
                         float* x1, float* x2)
     {
         float discriminant = pow(b,2) - 4 * a * c;
        
         if (discriminant < 0)
         {
             printf("Roots are imaginary\n");
             return -1;
         }

         // Compute
         *x1 = (-b + approx_sqrt(discriminant)) / (2 * a);
         *x2 = (-b - approx_sqrt(discriminant)) / (2 * a);

         // Success
         return 0;
     }

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


#71723

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-10-03 20:55 +0100
Message-ID<87wpv3ohfs.fsf@bsb.me.uk>
In reply to#71707
"Rick C. Hodgin" <rick.c.hodgin@gmail.com> writes:

> On Saturday, October 3, 2015 at 1:43:37 PM UTC-4, Alla _ wrote:
>> Thank you!
>
> Here is one solution to the issue of returning two values:
>
>      float a, b, c, x1, x2;
>
>      // Set a, b, c values somewhere
>
>      // Solve
>      if (solve_quadratic(a, b, c, x1, x2) >= 0)

Not in C.  In C I can be sure that if solve_quadratic is a function
(rather than a macro) it won't change the values of any objects used to
provide argument values.  Argument passing is always by value.  Many
people regard this as a huge advantage of the design.

<snip>
> And the called function's code:
>
>      // Function to find roots of a quadratic
>      int solve_quadratic(float a, float b, float c, float& x1, float& x2)

That's not C, it's C++.

<snip>
-- 
Ben.

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


#71730

From"Rick C. Hodgin" <rick.c.hodgin@gmail.com>
Date2015-10-03 13:27 -0700
Message-ID<d1edccb6-6694-4a5b-83b6-467d33e0c0d8@googlegroups.com>
In reply to#71723
On Saturday, October 3, 2015 at 3:55:43 PM UTC-4, Ben Bacarisse wrote:
> "Rick C. Hodgin" <rick.c.hodgin@gmail.com> writes:
> >      // Function to find roots of a quadratic
> >      int solve_quadratic(float a, float b, float c, float& x1, float& x2)
> 
> That's not C, it's C++.

Thank you for the correction.

Note:  It will be part of C code allowed in RDC, along with function
overloading, and function declaration default assignments for empty
parameters, though I will also extend that ability to include this:

    int abc(int x = 2, int y = 3, int z);

    printf("%d\n", abc(,,3));

Parameters 1 and 2 are not required because they have default
initializers.  Parameter 3 is required, however.

Best regards,
Rick C. Hodgin

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


#71736

Fromjacobnavia <jacob@jacob.remcomp.fr>
Date2015-10-03 23:08 +0200
Message-ID<mupg0j$3l1$2@dont-email.me>
In reply to#71723
Le 03/10/2015 21:55, Ben Bacarisse a écrit :
> <snip>
>> And the called function's code:
>>
>>       // Function to find roots of a quadratic
>>       int solve_quadratic(float a, float b, float c, float& x1, float& x2)
>
> That's not C, it's C++.
>


The lcc-win compiler accepts that without any trouble. Is that bad?

Of course many people here can start arguing that is bad. But then...

... the lcc-win compiler supports that anyway since several years. It is 
useful...


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


#71743

FromRichard Heathfield <rjh@cpax.org.uk>
Date2015-10-03 22:52 +0100
Message-ID<mupijf$bmh$1@dont-email.me>
In reply to#71736
On 03/10/15 22:08, jacobnavia wrote:
> Le 03/10/2015 21:55, Ben Bacarisse a écrit :
>> <snip>
>>> And the called function's code:
>>>
>>>       // Function to find roots of a quadratic
>>>       int solve_quadratic(float a, float b, float c, float& x1,
>>> float& x2)
>>
>> That's not C, it's C++.
>>
>
>
> The lcc-win compiler accepts that without any trouble. Is that bad?

No, in a way it's actually quite good. If the compiler is invoked in 
conforming mode, a diagnostic message is required, but that doesn't mean 
the compiler can't go ahead and Do The Right Thing anyway.

<snip>

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

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


#71836

FromRosario19 <Ros@invalid.invalid>
Date2015-10-04 20:46 +0200
Message-ID<pnr21b1287fff5arfericujjih1vk7to5b@4ax.com>
In reply to#71707
On Sat, 3 Oct 2015 12:04:25 -0700 (PDT), "Rick C. Hodgin" wrote:

>On Saturday, October 3, 2015 at 1:43:37 PM UTC-4, Alla _ wrote:
>> Thank you!
>
>Here is one solution to the issue of returning two values:
>
>     float a, b, c, x1, x2;
>
>     // Set a, b, c values somewhere
>
>     // Solve
>     if (solve_quadratic(a, b, c, x1, x2) >= 0)
>     {
>         // Valid, x1 and x2 are set
>     }
>
>-----
>And the called function's code:
>
>     // Function to find roots of a quadratic
>     int solve_quadratic(float a, float b, float c, float& x1, float& x2)
>     {
>         float discriminant = pow(b,2) - 4 * a * c;
>        
>         if (discriminant < 0)
>         {
>             printf("Roots are imaginary\n");
>             return -1;
>         }
>         // Compute
>         x1 = (-b + approx_sqrt(discriminant)) / (2 * a);
>         x2 = (-b - approx_sqrt(discriminant)) / (2 * a);
>
>         // Success
>         return 0;
>     }

#define f64  double
#define R    return
#define P    printf
#define i32  int


// ax^2+bx+c=0
// return 0 for 0   solutions
// return 1 for one solution  find write in *x1 
// return 2 for 2   solutions find write in *x1 *x2
// -1 for error
// overflow not detect in error case 
i32 solveQudratic(f64* x1, f64* x2, f64 a, f64 b, f64  c)
{f64   delta;
 
 if(x1==0||x2==0)  R  -1;        
 *x1=0; *x2=0; 

 if(-0.1e-10<a&&a<0.1e-10)
        {*x1=-c/b; R   1;} 
 delta=b*b-4*a*c;
 if(delta<0)       R   0;
 delta=sqrt(delta); 
 // Compute
 *x1 =(-b- delta)/ (2*a);
 *x2 =(-b+ delta)/ (2*a);
 R  2;
}


>Best regards,
>Rick C. Hodgin

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


#71883

FromKeith Thompson <kst-u@mib.org>
Date2015-10-04 15:47 -0700
Message-ID<lnsi5qw8ro.fsf@kst-u.example.com>
In reply to#71836
Rosario19 <Ros@invalid.invalid> writes:
[...]
> #define f64  double
> #define R    return
> #define P    printf
> #define i32  int
[...]

No.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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


#71912

Fromregis <regis.barbanchon@free.fr>
Date2015-10-05 03:06 +0200
Message-ID<5611ccfd$0$3060$426a34cc@news.free.fr>
In reply to#71836
On 10/04/15 20:46, Rosario19 wrote:

>   delta= sqrt(delta);

No ;o)

Variable aren't just places in memory to store values.
They have names. And names have meanings.
Hence variables have meanings.

And because their names don't change through their lifetime,
their meaning cannot change either through their lifetime.

Hence, once  delta means the discriminant,
it should change its meaning to its square root,
and a second variable is conceptually required
with a distinct name reflecting this very difference.

The abuse of the same name yields this absurdity:

 >   *x1 =(-b- delta)/ (2*a);
 >   *x2 =(-b+ delta)/ (2*a);

Conceptually wrong, even if algorithmically correct.

-- 
Regis

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


#71925

FromRosario19 <Ros@invalid.invalid>
Date2015-10-05 05:31 +0200
Message-ID<jhr31b58dqnsrblak6mmiqjcbs98hvevop@4ax.com>
In reply to#71912
On Mon, 05 Oct 2015 03:06:05 +0200, regis <regis.barbanchon@free.fr>
wrote:

>On 10/04/15 20:46, Rosario19 wrote:
>
>>   delta= sqrt(delta);
>
>No ;o)
>
>Variable aren't just places in memory to store values.
>They have names. And names have meanings.
>Hence variables have meanings.
>
>And because their names don't change through their lifetime,
>their meaning cannot change either through their lifetime.
>
>Hence, once  delta means the discriminant,
>it should change its meaning to its square root,
>and a second variable is conceptually required
>with a distinct name reflecting this very difference.
>
>The abuse of the same name yields this absurdity:
>
> >   *x1 =(-b- delta)/ (2*a);
> >   *x2 =(-b+ delta)/ (2*a);
>
>Conceptually wrong, even if algorithmically correct.

do you want the correct name for variables?
do you want 2 only variables? one for the error conditions and info
one for one array of float double result?

#define f64  double
#define R    return
#define P    printf
#define i32  int


// "x" has to be one pointer to double that point
// one array of 2 double at last
// ax^2+bx+c=0
// return  0 for 0   solutions
// return  1 for one solution  find write in x[0] 
// return  2 for 2   solutions find write in x[0] x[1]
// return -1 for error
// overflow not detect in error case 
i32 solveQudratic(f64* x, f64 a, f64 b, f64  c)
{f64   delta;
 
 if(x==0)       R -1;        
 x[0]=0; x[1]=0; 

 if(-0.1e-10<a&&a<0.1e-10)
    {if(-0.1e-10<b&&b<0.1e-10)
                R  0;
     x[0]=-c/b; R  1;
    } 
 delta=(b*b)-(4*a*c);
 if(delta<0)    R  0;

#define sqDelta  delta
 sqDelta=sqrt(delta); 
 // Compute
 x[0] =(-b- sqDelta)/ (2*a);
 x[1] =(-b+ sqDelta)/ (2*a);
#undef  sqDelta

 R  2;
}

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


#71958

FromRosario19 <Ros@invalid.invalid>
Date2015-10-05 11:58 +0200
Message-ID<b9i41bhaciarol9n2gb3r8rcgqnl639mgb@4ax.com>
In reply to#71925
On Mon, 05 Oct 2015 05:31:27 +0200, Rosario19 wrote:

>i32 solveQudratic(f64* x, f64 a, f64 b, f64  c)

in 90% of cases
i'm ok with convention that a function return
as error condition + other info 
one int or i32 or better int32_t

the first argument is a pointer to memory to what return
the other argumets are input of the function 

as above

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


#71959

FromRosario19 <Ros@invalid.invalid>
Date2015-10-05 12:00 +0200
Message-ID<0gi41blhms79ek1npgtke2ba56fn65e7md@4ax.com>
In reply to#71958
On Mon, 05 Oct 2015 11:58:57 +0200, Rosario19 <Ros@invalid.invalid>
wrote:

>On Mon, 05 Oct 2015 05:31:27 +0200, Rosario19 wrote:
>
>>i32 solveQudratic(f64* x, f64 a, f64 b, f64  c)
>
>in 90% of cases
>i'm ok with convention that a function return
>as error condition + other info 
>one int or i32 or better int32_t

on error all returns -1 or some negative values
and 0..intmax can be used too

>the first argument is a pointer to memory to what return
>the other argumets are input of the function 
>
>as above
>

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


#71960

FromRosario19 <Ros@invalid.invalid>
Date2015-10-05 12:06 +0200
Message-ID<0ni41b11o5a5fnj20l8p9tjn1d9892c3p0@4ax.com>
In reply to#71959
On Mon, 05 Oct 2015 12:00:10 +0200, Rosario19 wrote:

>>>i32 solveQudratic(f64* x, f64 a, f64 b, f64  c)
>>
>>in 90% of cases
>>i'm ok with convention that a function return
>>as error condition + other info 
>>one int or i32 or better int32_t
>
>on error all returns -1 or some negative values

>and 0..intmax can be used too

>>the first argument is a pointer to memory to what return

so in that pointer there is the otuput of the function
and 0..maxint that function return to the caller
can be used for describe what kind of solution there is in 
the pointer as above

>>the other argumets are input of the function 

>>as above
>>

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


#71963

Fromregis <regis.barbanchon@free.fr>
Date2015-10-05 12:39 +0200
Message-ID<5612537a$0$3299$426a74cc@news.free.fr>
In reply to#71925
On 10/05/15 05:31, Rosario19 wrote:

> do you want the correct name for variables?

If you write: int myWeightInKg= 65; ...; myWeightInKg += 5;

the value of myWeightInKg changes, but not its meaning:
I'm heavier by 5 kilograms, but it's still my weight in kilograms.

But when you write: double delta= b*b-4*a*c; ...; delta= sqrt(delta);
Things are different: You don't just change the value here,
you also change the meaning of delta.
It once meant the discriminant as it should
(as delta is universally used to denote the discriminant),
and then you make its name a liar by changing its meaning.

So you should use 2 variables here: sqrtDelta= sqrt (delta);


Never use meaningless names for identifiers.
Never use a meaning for another in identifier names.
Never change the meaning of a variable during its lifetime.

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


#71964

FromBartc <bc@freeuk.com>
Date2015-10-05 12:20 +0100
Message-ID<mutmap$37d$1@dont-email.me>
In reply to#71963
On 05/10/2015 11:39, regis wrote:
> On 10/05/15 05:31, Rosario19 wrote:
>
>> do you want the correct name for variables?
>
> If you write: int myWeightInKg= 65; ...; myWeightInKg += 5;
>
> the value of myWeightInKg changes, but not its meaning:
> I'm heavier by 5 kilograms, but it's still my weight in kilograms.

OK, here myWeightInKg clearly refers to a physical quantity with a 
certain 'dimension'. So you couldn't do myWeightInKg *= myWeightInKg as 
the dimension then becomes kg-squared (with no meaning afaik).

> But when you write: double delta= b*b-4*a*c; ...; delta= sqrt(delta);
> Things are different: You don't just change the value here,
> you also change the meaning of delta.

But what's delta? Isn't it just a number? (Until now I'd never even 
heard of delta in connection with solving quadratic equations).

> It once meant the discriminant as it should
> (as delta is universally used to denote the discriminant),
> and then you make its name a liar by changing its meaning.
>
> So you should use 2 variables here: sqrtDelta= sqrt (delta);

> Never use meaningless names for identifiers.

So you'd never use x, y, z? My math(s) textbooks used to be full of 
them! What about the a, b, c used for the coefficients of the equation?

> Never use a meaning for another in identifier names.
> Never change the meaning of a variable during its lifetime.

Sometimes that makes sense; if DataArray points to an int array for 
example, you don't to step DataArray along itself, because at the end if 
won't refer to the original array at all.

But otherwise I think you're worrying too much!

-- 
Bartc

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


#71965

Fromregis <regis.barbanchon@free.fr>
Date2015-10-05 13:50 +0200
Message-ID<5612641c$0$3018$426a74cc@news.free.fr>
In reply to#71964
On 10/05/15 13:20, Bartc wrote:
>
>> Never use meaningless names for identifiers.
>
> So you'd never use x, y, z? My math(s) textbooks used to be full of
> them! What about the a, b, c used for the coefficients of the equation?

???

 From where did you derive that I would suggest x,y,z are meaningless in 
the context of coordinate system ? or a,b,c, or delta would be
meaningless in the context of the quadratic equation ?
Because they are single-letter identifiers ?

I precisely just wrote the opposite in fact.

I wrote that in context of solving a quadratic equation, delta has a 
well defined meaning, universally understood as the discriminant,
and hence could not be used later in code for denoting its the square root.

Regis

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


#71966

FromRichard Heathfield <rjh@cpax.org.uk>
Date2015-10-05 12:57 +0100
Message-ID<mutof4$bh0$1@dont-email.me>
In reply to#71964
On 05/10/15 12:20, Bartc wrote:
> On 05/10/2015 11:39, regis wrote:
>> On 10/05/15 05:31, Rosario19 wrote:
>>
>>> do you want the correct name for variables?
>>
>> If you write: int myWeightInKg= 65; ...; myWeightInKg += 5;
>>
>> the value of myWeightInKg changes, but not its meaning:
>> I'm heavier by 5 kilograms, but it's still my weight in kilograms.
>
> OK, here myWeightInKg clearly refers to a physical quantity with a
> certain 'dimension'. So you couldn't do myWeightInKg *= myWeightInKg as
> the dimension then becomes kg-squared (with no meaning afaik).

kg^2 occurs in force calculations. The gravitational constant G has 
units Nm^2/kg^2. This doesn't affect your point, of course.

>
>> But when you write: double delta= b*b-4*a*c; ...; delta= sqrt(delta);
>> Things are different: You don't just change the value here,
>> you also change the meaning of delta.
>
> But what's delta? Isn't it just a number? (Until now I'd never even
> heard of delta in connection with solving quadratic equations).

"delta" is generally used to mean "small difference", although "small" 
is kind of optional. Personally, I'd write it:

   delta = sqrt(b * b - 4 * a * c);

thus sidestepping the issue completely.

>> It once meant the discriminant as it should
>> (as delta is universally used to denote the discriminant),
>> and then you make its name a liar by changing its meaning.
>>
>> So you should use 2 variables here: sqrtDelta= sqrt (delta);
>
>> Never use meaningless names for identifiers.
>
> So you'd never use x, y, z?

It depends on whether they are meaningful. I generally use x for the 
horizontal component of a vector, and y for the vertical component. If I 
need a depth component too, I use z. Spatial axes are typically referred 
to by these letters, so they do have meaning.

Similarly, something like 'i' is often a perfectly adequate name for a 
loop control. Within the context of:

for(i = 0; i < len; i++)
{
   c[i] = p[i] ^ key[i % klen];
}

it is perfectly obvious what i means.

> My math(s) textbooks used to be full of
> them! What about the a, b, c used for the coefficients of the equation?

Again, a, b, and c are the usual names given to those coefficients, and 
that is what gives the names meaning.

The point regis is making is a good one: meaningful names make for 
readable code, and it's easier to make a readable program correct than 
it is to make a correct program readable (and thus make it easier to 
maintain, and thus less likely to break in the future).

When the name of a 'variable' (if I may use the term) has no real 
connection with the concept it is modelling, or is overloaded with more 
than one meaning, it makes the code harder to read.

>> Never use a meaning for another in identifier names.

Right.

>> Never change the meaning of a variable during its lifetime.

Right.

> Sometimes that makes sense; if DataArray points to an int array for
> example, you don't to step DataArray along itself, because at the end if
> won't refer to the original array at all.

That's easy to deal with, though, because one simply chooses a better name:

void cpystr(char *target, const char *source)
{
   while(*target++ = *source++)
   {
     continue;
   }
   return;
}

> But otherwise I think you're worrying too much!

I think that probably quite a lot of programmers would agree with you. 
Personally, however, I think regis is (a) not worrying at all, and (b) 
exactly right in his advice to choose good, meaningful names and to 
maintain the correlation between the name of an object and its purpose.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

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


#71942

Fromglen herrmannsfeldt <gah@ugcs.caltech.edu>
Date2015-10-05 05:40 +0000
Message-ID<mut2g2$6m1$1@speranza.aioe.org>
In reply to#71912
regis <regis.barbanchon@free.fr> wrote:

(snip)

>>   delta= sqrt(delta);
 
> Variable aren't just places in memory to store values.
> They have names. And names have meanings.
> Hence variables have meanings.

I suppose, but I suspect I have done this before.

Sometimes I would rather make two simpler statements instead
of one complicated one. That usually means assigning to some
variable and then using the result in the next statement. 

I suspect that is the case here. 

So, yes, it would be better to use a separate variable, that
is only used once, for the value used as the argument of sqrt.
The additional bytes are small enough now, though many years
ago they would have been significant. 
 
> And because their names don't change through their lifetime,
> their meaning cannot change either through their lifetime.

I suspect with an appropriate comment, and if the argument
comes from the immediate preceding statement, one could live with it.
 
> Hence, once  delta means the discriminant,
> it should change its meaning to its square root,
> and a second variable is conceptually required
> with a distinct name reflecting this very difference.

Conceptually required, but that adds complication. More variables
for the human reader to keep track of. 
 
> The abuse of the same name yields this absurdity:
 
> >   *x1 =(-b- delta)/ (2*a);
> >   *x2 =(-b+ delta)/ (2*a);

What is wrong with this one?
 
> Conceptually wrong, even if algorithmically correct.

Also, numerically suspect in many cases.

-- glen

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


#71945

Fromregis <regis.barbanchon@free.fr>
Date2015-10-05 08:30 +0200
Message-ID<561218f9$0$3855$426a74cc@news.free.fr>
In reply to#71942
On 10/05/15 07:40, glen herrmannsfeldt wrote:

> Conceptually required, but that adds complication. More variables
> for the human reader to keep track of.

No, it's the opposite.
If correctly named with its meaning: e.g. sqrtOfDelta,
you don't have to track to anything,
because you use words matching their meanings.

While if you reuse the same variable,
you have to notice and remember that delta
does not mean the discriminant it universally means anymore:
it's  mental mapping.

And because it did earlier mean the discriminant,
it's mental juggling too.

>> >The abuse of the same name yields this absurdity:
>
>>> > >   *x1 =(-b- delta)/ (2*a);
>>> > >   *x2 =(-b+ delta)/ (2*a);
> What is wrong with this one?

It simply reads as if the discriminant is added.


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


#71987

FromPhilip Lantz <prl@canterey.us>
Date2015-10-05 10:03 -0700
Message-ID<MPG.307c4d1b6913286e74@news.eternal-september.org>
In reply to#71942
glen herrmannsfeldt wrote:
> regis wrote:
> >>   delta= sqrt(delta);
>  
> > Variable aren't just places in memory to store values.
> > They have names. And names have meanings.
> > Hence variables have meanings.
> 
> I suppose, but I suspect I have done this before.
> 
> Sometimes I would rather make two simpler statements instead
> of one complicated one. That usually means assigning to some
> variable and then using the result in the next statement. 
> 
> I suspect that is the case here. 
> 
> So, yes, it would be better to use a separate variable, that
> is only used once, for the value used as the argument of sqrt.
> The additional bytes are small enough now, though many years
> ago they would have been significant. 

Why would you think that there would be any additional bytes?

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


Page 3 of 6 — ← Prev page 1 2 [3] 4 5 6  Next page →

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


csiph-web