Math::NoCarry(3pm) | User Contributed Perl Documentation | Math::NoCarry(3pm) |
Math::NoCarry - Perl extension for no-carry arithmetic
use Math::NoCarry qw(:all); my $sum = add( 123, 456 ); my $difference = subtract( 123, 456 ); my $product = multiply( 123, 456 );
No-carry arithmetic doesn't allow you to carry digits to the next column. For example, if you add 8 and 4, you normally expect the answer to be 12, but that 1 digit is a carry. In no-carry arithmetic you can't do that, so the sum of 8 and 4 is just 2. In effect, this is addition modulo 10 in each column. I discard all of the carry digits in this example:
1234 + 5678 ------ 6802
For multiplication, the result of pair-wise multiplication of digits is the modulo 10 value of their normal, everyday multiplication.
123 x 456 ----- 8 6 x 3 2 6 x 2 6 6 x 1 5 5 x 3 0 5 x 2 5 5 x 1 2 4 x 3 8 4 x 2 + 4 4 x 1 ------- 43878
Since multiplication and subtraction are actually types of additions, you can multiply and subtract like this as well.
No carry arithmetic is both associative and commutative.
As of version 1.11, all of these functions are exportable on demand, or with the tag ":all" to get them all at once.
Return A if it is the only argument ( A x 1 );
Returns A if it is the only argument ( A + 0 )
Returns false if either number is negative.
Returns A if it is the only argument ( A - 0 )
Returns false if either number is negative.
This source is in GitHub:
https://github.com/briandfoy/math-nocarry
brian d foy, "<bdfoy@cpan.org>"
Copyright © 2002-2021, brian d foy <bdfoy@cpan.org>. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0.
2021-01-16 | perl v5.32.0 |