#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11163460 use warnings; use List::AllUtils qw( zip_by before ); $SIG{__WARN__} = sub { die @_ }; sub bigdivide { my ($t, $d) = map s/^0+(?=.)//r, @_; my $q = ''; my @pad = before { length $t < length $d . 0 x $_ || length $t == $_ + length $d && $t lt $d . 0 x $_ } 0 .. length $t; for( reverse @pad ) { my $digit = 0; while( length $t > $_ + length $d || length $t == $_ + length $d && $t ge $d . 0 x $_ ) { $digit++; $t = bigsubtract( $t, $d . 0 x $_ ); } $q .= $digit; } return $q || 0; } my $xx = '1238888888888888888888888888888888888888888888884555555555888888888888888888888888888555555555555'; my $yy = '555333577778888888888888888888888888887555555'; my $quo = bigdivide($xx, $yy); print "bigdivide $quo\n"; { use bigint; my $tmp = ($xx+0) / ($yy+0); print " bigint $tmp\n"; } sub bigsubtract { my ($top, $bottom) = map s/^0+(?=.)//r, @_; if( length $top > length $bottom || length $top == length $bottom && $top ge $bottom ) { my @top = reverse split //, $top; my @bottom = reverse split //, $bottom; my $borrow = 0; my $answer = reverse join '', zip_by { my $digit = $_[0] - ($_[1] // 0) - $borrow; $borrow = $digit >= 0 ? 0 : 1; $digit >= 0 ? $digit : $digit % 10; } \@top, \@bottom; return $answer =~ s/^0+(?=.)//r; } print "failed\n"; use Data::Dump 'dd'; dd undef; return undef; } #### bigdivide 2230891374953314563617132232227474545270207356483795 bigint 2230891374953314563617132232227474545270207356483795