On 12/20/20 5:01 AM, ToddAndMargo via perl6-users wrote:
On 12/20/20 2:24 AM, ToddAndMargo via perl6-users wrote:
Hi All,
Now what am I doing wrong?
-T
class Angle {
# has num64 $.degrees;
method AngleCosine( Rat:D: --> Rat:D ) {
my Numeric $radians = self*π/180;
print "self = <" ~ self ~ "\n";
print "radians = <$radians>\n";
return $radians.cos;
}
}
say (45.0).AngleCosine
No such method 'AngleCosine' for invocant of type 'Rat'
in block <unit> at <unknown file> line 1
I can't get this to work either:
class String2 {
method PrintStr( Str:D: ) {
print "self = <" ~ self ~ ">\n";
}
}
say "abc".PrintStr
No such method 'PrintStr' for invocant of type 'Str'
in block <unit> at <unknown file> line 2
Hi All,
This is what I have so far:
Example 3:
class Angle {
has Numeric $.degrees;
method AngleCosine() {
my Numeric $radians = self.degrees * π / 180;
my Numeric $cosine = $radians.cos;
print "self = <" ~ self.degrees ~ ">\n";
print "radians = <$radians>\n";
print "Cosine of " ~ self.degrees ~ " degrees is <" ~
$cosine ~ ">\n";
return $cosine;
}
}
my $x = Angle.new( degrees => 45 );
say $x.AngleCosine;
self = <45>
radians = <0.7853981633974483>
Cosine of 45 degrees is <0.7071067811865476>
0.7071067811865476
What I would like to eventually be able to do is
say 45.AngleCosine;
I am getting fancy:
Example 4:
class Angle {
has Numeric $.degrees;
method AngleCosine( $debug = False ) {
my Str $SubName = &?ROUTINE.name;
my Numeric $radians = self.degrees * π / 180;
my Numeric $cosine = $radians.cos;
if $debug {
print "$SubName debugging:\n";
print " self = <" ~ self.degrees ~ ">\n";
print " radians = <$radians>\n";
print " Cosine of " ~ self.degrees ~ " degrees is <" ~
$cosine ~ ">\n\n";
}
return $cosine;
}
}
say $x.AngleCosine;
0.7071067811865476
say $x.AngleCosine( True );
AngleCosine debugging:
self = <45>
radians = <0.7853981633974483>
Cosine of 45 degrees is <0.7071067811865476>
0.7071067811865476