ok i got this perl code from born2code and it stops after 16 digits
here is the out put
Enter the sample base: 10
Enter base to convert to: 2
12345678901234567890
Digit 0 (0) is 0
Digit 1 (9) is 90
Digit 2 (8) is 800
Digit 3 (7) is 7000
Digit 4 (6) is 60000
Digit 5 (5) is 500000
Digit 6 (4) is 4000000
Digit 7 (3) is 30000000
Digit 8 (2) is 200000000
Digit 9 (1) is 1000000000
Digit 10 (0) is 0
Digit 11 (9) is 900000000000
Digit 12 (8) is 8000000000000
Digit 13 (7) is 70000000000000
Digit 14 (6) is 600000000000000
Digit 15 (5) is 5e+015
Digit 16 (4) is 4e+016
Digit 17 (3) is 3e+017
Digit 18 (2) is 2e+018
Digit 19 (1) is 1e+019
Base is 1.23456789012346e+019
1010101101010100101010011000110011101011000111110000100000000000
notice here at digit 15 it stops working properly
here is what it is suppose to look like in binary (thanks to Mathematica)
1010101101010100101010011000110011101011000111110000101011010010
so we can see that this might need bigfloat.pl or somthing else
i just cant figure out how to go about it
i tried to use fdiv(1,@num, 4000) in the code i lefta note where i tried to
use it but it didnt work is there somthing else that i could do
or can some one point me to another spot?
CODE
#!perl.exe
require 'Bigfloat.pl';
my ($sbase,$tbase,$sample)=@ARGV;
$sbase= get_in( 'Enter the sample base:') unless($sbase );
$tbase= get_in( 'Enter base to convert to:') unless($tbase );
if($sample) {
print convert_base($sbase,$sample,$tbase),"\n";
} else {
while(<STDIN>) {
chomp;
print convert_base($sbase,$_,$tbase),"\n";
}
}
sub convert_base {
my ($sbase,$sample,$tbase)=@_;
my @num=split(//,$sample);
my ($num,$i,@res,@symb,%base);
foreach(0..9,A..Z,a..z) {
push @symb,$_;
}
foreach(0..@symb-1) { #no @symb-1
$base{$_}=$symb[$_];
}
@num=reverse(@num); ## i tried to use fdiv(1,@num, 4000) here
foreach(0..@num-1) { #no @num-1
print "Digit $_ (" . $num[$_] . ") is "; ##
$t=$base{$num[$_]} * expn($sbase,$_);
$t=$base{$num[$_]} if($_==0);
print $t . "\n"; ##
$num+=$t;
}
print "Base is $num\n"; ## print "Base is $num\n"; //so dont use fdiv here
while(expn($tbase,$i)<=$num) {
$i++;
}
$i--;
foreach(0..$i) {
$_=$i-$_;
$res[$_]='0';
while($num>=expn($tbase,$_)) {
$num-=expn($tbase,$_);
$res[$_]++;
}
}
foreach(@res) {
$_=$symb[$_];
}
@res=reverse(@res);
return join('',@res);
}
sub expn {
my ($num,$expn)=@_;
my $n=1;
foreach(1..$expn) {
$n*=$num;
}
return $n;
}
sub get_in {
print shift() . ' ';
my $data=<STDIN>;
chomp $data;
return $data;
}