in reply to enumerating values for slopes

Math::BigFloat
#!/usr/bin/env perl use strict; use warnings; use Math::Trig; use Math::BigFloat; use feature 'say'; my $pi = Math::BigFloat->bpi(10); my ($run,$theta) = (3,8); my $radians = Math::BigFloat->new($theta * $pi / 180); #my $rise = Math::BigFloat->new($run * tan($radians)); my $rise = Math::BigFloat->new($run * (sin($radians) / cos($radians))) +; say "rise is $rise"; #rise is 0.42162250410717434055

Here is what I came up with for an equivalent. Supposedly the Math:: packages offer up to arbitrary precision or accuracy. The tan function is not working for me though, I get an error: Can't call Math::BigFloat->_cartesian, not a valid method at /usr/local/share/perl/5.14.2/Math/Complex.pm line 928. This inspired me to use a trig identity in real, well, almost real life for the first time..

So, if you can install packages, (Math::Trig and Math::BigFloat come with Perl) you can use those modules for really high accuracy calculations. If you don't then you might have to lose some accuracy

In that case..
#!/usr/bin/env perl use strict; use warnings; my $pi = 355/113; my ($run,$theta) = (3,8); my $radians = $theta*$pi/180; my $rise = $run * (sin($radians) / cos($radians)); say "rise is $rise"; #rise is 0.421622540378273

Replies are listed 'Best First'.
Re^2: enumerating values for slopes
by Laurent_R (Canon) on Sep 21, 2014 at 09:57 UTC
    If high accuracy is important, maybe it would better to use a better approximation of pi (although I readily admit that 355/113 is already a pretty good approximation). For example:
    my $pi = 4 * atan2(1, 1);
    Or perhaps making it a constant:
    use constant PI => 4 * atan2(1, 1);
      Both Math::Trig and Math::BigFloat provide a pi constant
      $ perl -MMath::Trig -MMath::BigFloat=bpi -le " print for pi(), bpi(15) + " 3.14159265358979 3.14159265358979
        Yes, right, I was just commenting on the part of the code suggested by trippledubs not using these modules. And frankly, the BigFloat module is probably not very useful in the context, because bare Perl can make the slope calculations much more accurately than the measurements on which they are or seem to be based.

      One is never gonna outstrip a a c_double with measurements he makes with a carpenter's protractor, so Math::Trig has all the functionality I need here.