fix #75 Missing methods _sadd + _ssub in Math::BigInt::LTM
Karel Miko
2 years ago
429 | 429 | return $n; |
430 | 430 | } |
431 | 431 | |
432 | ### same as _sadd() in Math::BigInt::Lib | |
433 | # Signed addition. If the flag is false, $xa might be modified, but not $ya. If | |
434 | # the false is true, $ya might be modified, but not $xa. | |
435 | sub _sadd { | |
436 | my $class = shift; | |
437 | my ($xa, $xs, $ya, $ys, $flag) = @_; | |
438 | my ($za, $zs); | |
439 | ||
440 | # If the signs are equal we can add them (-5 + -3 => -(5 + 3) => -8) | |
441 | ||
442 | if ($xs eq $ys) { | |
443 | if ($flag) { | |
444 | $za = $class -> _add($ya, $xa); | |
445 | } else { | |
446 | $za = $class -> _add($xa, $ya); | |
447 | } | |
448 | $zs = $class -> _is_zero($za) ? '+' : $xs; | |
449 | return $za, $zs; | |
450 | } | |
451 | ||
452 | my $acmp = $class -> _acmp($xa, $ya); # abs(x) = abs(y) | |
453 | ||
454 | if ($acmp == 0) { # x = -y or -x = y | |
455 | $za = $class -> _zero(); | |
456 | $zs = '+'; | |
457 | return $za, $zs; | |
458 | } | |
459 | ||
460 | if ($acmp > 0) { # abs(x) > abs(y) | |
461 | $za = $class -> _sub($xa, $ya, $flag); | |
462 | $zs = $xs; | |
463 | } else { # abs(x) < abs(y) | |
464 | $za = $class -> _sub($ya, $xa, !$flag); | |
465 | $zs = $ys; | |
466 | } | |
467 | return $za, $zs; | |
468 | } | |
469 | ||
470 | ### same as _ssub() in Math::BigInt::Lib | |
471 | # Signed subtraction. If the flag is false, $xa might be modified, but not $ya. | |
472 | # If the false is true, $ya might be modified, but not $xa. | |
473 | sub _ssub { | |
474 | my $class = shift; | |
475 | my ($xa, $xs, $ya, $ys, $flag) = @_; | |
476 | ||
477 | # Swap sign of second operand and let _sadd() do the job. | |
478 | $ys = $ys eq '+' ? '-' : '+'; | |
479 | $class -> _sadd($xa, $xs, $ya, $ys, $flag); | |
480 | } | |
481 | ||
432 | 482 | ### same as _log_int() in Math::BigInt::Lib |
433 | 483 | sub _log_int { |
434 | 484 | # calculate integer log of $x to base $base |