/usr/share/octave/packages/interval-2.1.0/@infsup/private/decimaladd.m is in octave-interval 2.1.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | ## Copyright 2014-2016 Oliver Heimlich
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @documentencoding UTF-8
## @deftypefun {@var{DECIMAL} =} decimaladd (@var{DECIMAL}, @var{ADDEND})
##
## Add two decimal numbers.
##
## @end deftypefun
## Author: Oliver Heimlich
## Created: 2014-10-21
function [decimal] = decimaladd (decimal, addend)
## Align mantissas
if (addend.e < decimal.e)
addend.m = [zeros(decimal.e - addend.e, 1); ...
addend.m];
addend.e = decimal.e;
elseif (addend.e > decimal.e)
decimal.m = [zeros(addend.e - decimal.e, 1); ...
decimal.m];
decimal.e = addend.e;
endif
if (length (decimal.m) > length (addend.m))
addend.m = [addend.m; ...
zeros(length (decimal.m) - ...
length (addend.m), 1)];
elseif (length (decimal.m) < length (addend.m))
decimal.m = [decimal.m; ...
zeros(length (addend.m) - ...
length (decimal.m), 1)];
endif
assert (length (decimal.m) == length (addend.m));
assert (decimal.e == addend.e);
## Add
if (decimal.s == addend.s)
decimal.m += addend.m;
else
decimal.m -= addend.m;
endif
## Carry
while (not (isempty (find (decimal.m >= 10))))
decimal.m = [0; rem(decimal.m, 10)] ...
+ [(decimal.m >= 10); 0];
if (decimal.m(1) == 0)
decimal.m(1) = [];
else
decimal.e ++;
endif
endwhile
## Resolve negative decimal digits
while (1)
highestnegative = find (decimal.m < 0, 1);
if (isempty (highestnegative))
break;
endif
highestpositive = find (decimal.m > 0, 1);
if (isempty (highestpositive) || ...
highestnegative < highestpositive)
## Flip sign
decimal.s = not (decimal.s);
decimal.m *= -1;
else
assert (decimal.m(1) >= 0);
decimal.m += 10 * (decimal.m < 0) ...
- [(decimal.m(2:end) < 0); 0];
endif
endwhile
clear highestnegative highestpositive;
## Normalize mantissa: remove leading zeroes
firstnonzerodigit = find (decimal.m ~= 0, 1, "first");
if (firstnonzerodigit > 1)
decimal.m = decimal.m(firstnonzerodigit:end);
decimal.e -= firstnonzerodigit - 1;
elseif (isempty (firstnonzerodigit)) # all digits are zero
decimal.s = false;
decimal.m = [];
decimal.e = int64 (0);
return
endif
## Remove trailing zeros
decimal.m = decimal.m(1 : find (decimal.m ~= 0, 1, "last"));
endfunction
|