in reply to Re: enumerating values for slopes
in thread enumerating values for slopes

$ perl slope3.pl Prototype mismatch: sub math1::pi () vs none at ../math1.pm line 13. Constant subroutine pi redefined at ../math1.pm line 7. Subroutine main::pi redefined at /usr/share/perl/5.18/Exporter.pm line + 66. at slope3.pl line 6. Prototype mismatch: sub main::pi: none vs () at /usr/share/perl/5.18/E +xporter.pm line 66. at slope3.pl line 6. slopes are 0 5 8 11 14 30 90 runs are 2.25 3.25 4 5 An angle of 0 degrees, or 0 radians, and run of 2.25 has a rise of 0 An angle of 0 degrees, or 0 radians, and run of 3.25 has a rise of 0 An angle of 0 degrees, or 0 radians, and run of 4 has a rise of 0 An angle of 0 degrees, or 0 radians, and run of 5 has a rise of 0 An angle of 5 degrees, or 0.0872664625997165 radians, and run of 2.25 +has a rise of 0.196849492933329 An angle of 5 degrees, or 0.0872664625997165 radians, and run of 3.25 +has a rise of 0.284338156459253 An angle of 5 degrees, or 0.0872664625997165 radians, and run of 4 has + a rise of 0.349954654103696 An angle of 5 degrees, or 0.0872664625997165 radians, and run of 5 has + a rise of 0.43744331762962 An angle of 8 degrees, or 0.139626340159546 radians, and run of 2.25 h +as a rise of 0.316216878080381 An angle of 8 degrees, or 0.139626340159546 radians, and run of 3.25 h +as a rise of 0.456757712782772 An angle of 8 degrees, or 0.139626340159546 radians, and run of 4 has +a rise of 0.562163338809566 An angle of 8 degrees, or 0.139626340159546 radians, and run of 5 has +a rise of 0.702704173511957 An angle of 11 degrees, or 0.191986217719376 radians, and run of 2.25 +has a rise of 0.437355695559867 An angle of 11 degrees, or 0.191986217719376 radians, and run of 3.25 +has a rise of 0.631736004697585 An angle of 11 degrees, or 0.191986217719376 radians, and run of 4 has + a rise of 0.777521236550874 An angle of 11 degrees, or 0.191986217719376 radians, and run of 5 has + a rise of 0.971901545688592 An angle of 14 degrees, or 0.244346095279206 radians, and run of 2.25 +has a rise of 0.560988006397157 An angle of 14 degrees, or 0.244346095279206 radians, and run of 3.25 +has a rise of 0.810316009240337 An angle of 14 degrees, or 0.244346095279206 radians, and run of 4 has + a rise of 0.997312011372723 An angle of 14 degrees, or 0.244346095279206 radians, and run of 5 has + a rise of 1.2466400142159 An angle of 30 degrees, or 0.523598775598299 radians, and run of 2.25 +has a rise of 1.29903810567666 An angle of 30 degrees, or 0.523598775598299 radians, and run of 3.25 +has a rise of 1.87638837486628 An angle of 30 degrees, or 0.523598775598299 radians, and run of 4 has + a rise of 2.3094010767585 An angle of 30 degrees, or 0.523598775598299 radians, and run of 5 has + a rise of 2.88675134594813 An angle of 90 degrees, or 1.5707963267949 radians, and run of 2.25 ha +s a rise of 3.67452885446896e+16 An angle of 90 degrees, or 1.5707963267949 radians, and run of 3.25 ha +s a rise of 5.3076527897885e+16 An angle of 90 degrees, or 1.5707963267949 radians, and run of 4 has a + rise of 6.53249574127815e+16 An angle of 90 degrees, or 1.5707963267949 radians, and run of 5 has a + rise of 8.16561967659768e+16 pi is 3.14159265358979 $ cat slope3.pl #!/usr/bin/perl -w use strict; use 5.010; BEGIN { push @INC, ".."; } use math1; use Math::Trig; my @slope = (0.0, 5.0, 8.0, 11.0, 14.0, 30.0, 90.0); say "slopes are @slope"; my @run = (2.25, 3.25, 4, 5); say "runs are @run"; foreach my $var1 (@slope) { my $s = degrees_to_radians($var1); foreach my $var2 (@run) { my $t = $var2 * tan($s); say "An angle of $var1 degrees, or $s radians, and run of $var2 has +a rise of $t"; } } my $a = pi(); say "pi is $a"; __END__ $

I made this as quick and dirty as I had to to get results. I like the results partially, in that I believe they're correct. The aspiration to have my own module was admirable for its valor, but not with its merits. I figured out that what I was doing came within spitting distance of *literally* re-inventing the circle.

$ cat math1.pm package math1; require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw( pi degrees_to_radians); sub pi{ use 5.010; use Math::Trig ':pi'; my $a = pi; return $a; } sub degrees_to_radians{ use 5.010; use Math::Trig qw(deg2rad); my $a = shift; my $b = deg2rad($a); return $b; } 1; $

I'm still fishing for people's experience with this, but I'll post a cleaned-up version of this that relies on cpan properly, instead of wrapping the calls and confusing perl.exe. Thanks all for comments.

Replies are listed 'Best First'.
Re^3: enumerating values for slopes
by Laurent_R (Canon) on Sep 22, 2014 at 06:15 UTC
    Although it probably does not have any consequences here, I would suggest that you avoid using the $a (and $b) variable, because $a and $b are special purpose global variables used for sorting data (and a few other specific uses).
      $ perl slope5.pl Can't use string ("4.3125") as an ARRAY ref while "strict refs" in use + at ../utils1.pm line 39. $ cat slope5.pl #!/usr/bin/perl -w use strict; use 5.010; use Math::Trig; BEGIN { push @INC, ".."; } use utils1; my @AoA; my $aoa_ref = \@AoA; my @vector = (4.3125, 0.4375); push @AoA, @vector; @vector = (4.375, 0.375); push @AoA, @vector; @vector = (4.4375, 0.375); print_aoa($aoa_ref); __END__ $

      Thought I would be comparing hypotenuses to heights but can't see why perl doesn't this is ok.

        The error occurs in the module, but the coding error is actually located in your calling program. When you do this:
        my @vector = (4.3125, 0.4375); push @AoA, @vector; @vector = (4.375, 0.375); push @AoA, @vector;
        you are not creating an array of arrays (AoA), but a simple array whose 4 elements are:
        (4.3125, 0.4375, 4.375, 0.375)
        You need to push on @AoA a reference to @vector:
        my @vector = (4.3125, 0.4375); push @AoA, \@vector; @vector = (4.375, 0.375); push @AoA, \@vector;
        or build
        directly an array reference:
        push @AoA, [4.3125, 0.4375]; push @AoA, [4.375, 0.375];
        I guess this should solve your issue.

        As I said earlier, you should avoid using the $a and $b variables, they are special variables used among other things for sorting, they behave differently from other variables, and using as you do them might lead to difficult-to-track bugs. You may use $c or $d, if you like, this will not be a problem, but I would personally put a more meaningful name helping comprehension of what they represent.

        Update: As pointed below by farang the first method above for building an AoA is actually buggy. Use the second one. Thanks to farang for picking that.

        Your error is in the ../utils1.pm module, but you don't show the code for that module. Difficult to help you in such conditions.

        What I found when field measuring was that it was much more attainable to get hypotenuses and heights, so that left me looking for arcsin. I had hoped to get this processed as an AoA, but I'm not quite there yet....

        $ perl slope8.pl bath-left theta is 5.82263230478133 bath-middle theta is 4.91710033552881 bath-right theta is 4.84767847916148 dining-south theta is 15.2575232904564 john-north theta is 16.0133944239485 middle-front theta is 24.6243183521641 south-front theta is 14.4775121859299 $ cat slope8.pl #!/usr/bin/perl -w use strict; use 5.010; use Math::Trig; BEGIN { push @INC, ".."; } use utils1; my @AoA; my $aoa_ref = \@AoA; my $tja = 0.4375/4.3125; say "bath-left"; my $hja = asin $tja; my $degrees = rad2deg($hja); say "theta is $degrees"; $tja = 0.375/4.375; $hja = asin $tja; say "bath-middle"; $degrees = rad2deg($hja); say "theta is $degrees"; say "bath-right"; $tja = 0.375/4.4375; $hja = asin $tja; $degrees = rad2deg($hja); say "theta is $degrees"; say "dining-south"; $tja = 1.25/4.75; $hja = asin $tja; $degrees = rad2deg($hja); say "theta is $degrees"; say "john-north"; $tja = 1.0/3.625; $hja = asin $tja; $degrees = rad2deg($hja); say "theta is $degrees"; say "middle-front"; $tja = 1.875/4.5; $hja = asin $tja; $degrees = rad2deg($hja); say "theta is $degrees"; say "south-front"; $tja = 1.125/4.5; $hja = asin $tja; $degrees = rad2deg($hja); say "theta is $degrees"; __END__ $