sub make {
my $self = bless {}, shift;
my ($re, $im) = @_;
- $self->{cartesian} = [$re, $im];
+ $self->{'cartesian'} = [$re, $im];
$self->{c_dirty} = 0;
$self->{p_dirty} = 1;
return $self;
my $self = bless {}, shift;
my ($rho, $theta) = @_;
$theta += pi() if $rho < 0;
- $self->{polar} = [abs($rho), $theta];
+ $self->{'polar'} = [abs($rho), $theta];
$self->{p_dirty} = 0;
$self->{c_dirty} = 1;
return $self;
#
sub i () {
$i = bless {} unless $i; # There can be only one i
- $i->{cartesian} = [0, 1];
- $i->{polar} = [1, pi/2];
+ $i->{'cartesian'} = [0, 1];
+ $i->{'polar'} = [1, pi/2];
$i->{c_dirty} = 0;
$i->{p_dirty} = 0;
return $i;
# Attribute access/set routines
#
-sub cartesian {$_[0]->{c_dirty} ? $_[0]->update_cartesian : $_[0]->{cartesian}}
-sub polar {$_[0]->{p_dirty} ? $_[0]->update_polar : $_[0]->{polar}}
+sub cartesian {$_[0]->{c_dirty} ? $_[0]->update_cartesian : $_[0]->{'cartesian'}}
+sub polar {$_[0]->{p_dirty} ? $_[0]->update_polar : $_[0]->{'polar'}}
-sub set_cartesian { $_[0]->{p_dirty}++; $_[0]->{cartesian} = $_[1] }
-sub set_polar { $_[0]->{c_dirty}++; $_[0]->{polar} = $_[1] }
+sub set_cartesian { $_[0]->{p_dirty}++; $_[0]->{'cartesian'} = $_[1] }
+sub set_polar { $_[0]->{c_dirty}++; $_[0]->{'polar'} = $_[1] }
#
# ->update_cartesian
#
sub update_cartesian {
my $self = shift;
- my ($r, $t) = @{$self->{polar}};
+ my ($r, $t) = @{$self->{'polar'}};
$self->{c_dirty} = 0;
- return $self->{cartesian} = [$r * cos $t, $r * sin $t];
+ return $self->{'cartesian'} = [$r * cos $t, $r * sin $t];
}
#
#
sub update_polar {
my $self = shift;
- my ($x, $y) = @{$self->{cartesian}};
+ my ($x, $y) = @{$self->{'cartesian'}};
$self->{p_dirty} = 0;
- return $self->{polar} = [0, 0] if $x == 0 && $y == 0;
- return $self->{polar} = [sqrt($x*$x + $y*$y), atan2($y, $x)];
+ return $self->{'polar'} = [0, 0] if $x == 0 && $y == 0;
+ return $self->{'polar'} = [sqrt($x*$x + $y*$y), atan2($y, $x)];
}
#
my ($x, $y) = @{$z->cartesian};
my ($re, $im);
+ $x = int($x + ($x < 0 ? -1 : 1) * 1e-14)
+ if int(abs($x)) != int(abs($x) + 1e-14);
+ $y = int($y + ($y < 0 ? -1 : 1) * 1e-14)
+ if int(abs($y)) != int(abs($y) + 1e-14);
+
$re = "$x" if abs($x) >= 1e-14;
if ($y == 1) { $im = 'i' }
elsif ($y == -1) { $im = '-i' }
- elsif (abs($y) >= 1e-14) { $im = "${y}i" }
+ elsif (abs($y) >= 1e-14) { $im = $y . "i" }
my $str;
$str = $re if defined $re;
if (abs($nt) <= 1e-14) { $theta = 0 }
elsif (abs(pi-$nt) <= 1e-14) { $theta = 'pi' }
- return "\[$r,$theta\]" if defined $theta;
+ if (defined $theta) {
+ $r = int($r + ($r < 0 ? -1 : 1) * 1e-14)
+ if int(abs($r)) != int(abs($r) + 1e-14);
+ $theta = int($theta + ($theta < 0 ? -1 : 1) * 1e-14)
+ if int(abs($theta)) != int(abs($theta) + 1e-14);
+ return "\[$r,$theta\]";
+ }
#
# Okay, number is not a real. Try to identify pi/n and friends...
$theta = $nt unless defined $theta;
+ $r = int($r + ($r < 0 ? -1 : 1) * 1e-14)
+ if int(abs($r)) != int(abs($r) + 1e-14);
+ $theta = int($theta + ($theta < 0 ? -1 : 1) * 1e-14)
+ if int(abs($theta)) != int(abs($theta) + 1e-14);
+
return "\[$r,$theta\]";
}