File Coverage

blib/lib/Language/Befunge/lib/MODU.pm
Criterion Covered Total %
statement 10 36 27.7
branch 0 8 0.0
condition 0 9 0.0
subroutine 4 7 57.1
pod 4 4 100.0
total 18 64 28.1


line stmt bran cond sub pod time code
1             #
2             # This file is part of Language-Befunge
3             #
4             # This software is copyright (c) 2003 by Jerome Quelin.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9 1     1   3 use strict;
  1         1  
  1         26  
10 1     1   3 use warnings;
  1         0  
  1         57  
11              
12             package Language::Befunge::lib::MODU;
13             # ABSTRACT: Modulo Arithmetic extension
14             $Language::Befunge::lib::MODU::VERSION = '5.000';
15 1     1   690 use POSIX qw{ floor };
  1         4606  
  1         4  
16              
17 1     1 1 2 sub new { return bless {}, shift; }
18              
19              
20             # -- modulus
21              
22             #
23             # $mod = M( $x, $y );
24             #
25             # signed-result modulo: x MOD y = x - FLOOR(x / y) * y
26             #
27             sub M {
28 0     0 1   my ($self, $lbi) = @_;
29 0           my $ip = $lbi->get_curip;
30 0           my $y = $ip->spop;
31 0           my $x = $ip->spop;
32 0 0         my $mod = $y == 0
33             ? 0
34             : $x - floor($x/$y)*$y;
35 0           $ip->spush($mod);
36             }
37              
38              
39             #
40             # $mod = U( $x, $y );
41             #
42             # Sam Holden's unsigned-result modulo... No idea who this Sam Holden is
43             # or if he has a special algorithm for this, therefore always returning
44             # absolute value of standard modulo.
45             #
46             sub U {
47 0     0 1   my ($self, $lbi) = @_;
48 0           my $ip = $lbi->get_curip;
49 0           my $y = $ip->spop;
50 0           my $x = $ip->spop;
51 0 0         if ( $y == 0 ) {
52 0           $ip->spush(0);
53 0           return;
54             }
55 0           my $mod = $x % $y;
56 0           $ip->spush(abs($mod));
57             }
58              
59              
60             #
61             # $mod = R( $x, $y );
62             #
63             # C-language integer remainder: old C leaves negative modulo undefined
64             # but C99 defines it as the same sign as the dividend so that's what we're
65             # going with.
66             #
67             sub R {
68 0     0 1   my ($self, $lbi) = @_;
69 0           my $ip = $lbi->get_curip;
70 0           my $y = $ip->spop;
71 0           my $x = $ip->spop;
72 0 0         if ( $y == 0 ) {
73 0           $ip->spush(0);
74 0           return;
75             }
76              
77 0           my $mod = $x % $y;
78 0 0 0       if ( ($x <= 0 && $mod <= 0) || ($x >= 0 && $mod >= 0)) {
      0        
      0        
79 0           $ip->spush( $mod );
80             } else {
81 0           $ip->spush( -$mod );
82             }
83             }
84              
85              
86             1;
87              
88             __END__