Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,058 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,560 people online right now. Registration is fast and FREE... Join Now!




Any Base to Any base converter stops after 16 digits

 
Reply to this topicStart new topic

Any Base to Any base converter stops after 16 digits, any one got any ideas on how get 4000 digits of conversion

evolivid
25 Jul, 2008 - 11:00 PM
Post #1

New D.I.C Head
*

Joined: 24 Jul, 2008
Posts: 28


My Contributions





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;
    }









User is offlineProfile CardPM
+Quote Post

baavgai
RE: Any Base To Any Base Converter Stops After 16 Digits
26 Jul, 2008 - 02:49 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,018



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Looks like the value is still maxing out at 32bits.

Maybe you want this?
CODE

use Math::BigInt;

my $n = Math::BigInt->new("12345678901234567890");
print $n->as_bin(), "\n";


User is offlineProfile CardPM
+Quote Post

evolivid
RE: Any Base To Any Base Converter Stops After 16 Digits
27 Jul, 2008 - 07:20 PM
Post #3

New D.I.C Head
*

Joined: 24 Jul, 2008
Posts: 28


My Contributions
QUOTE(baavgai @ 26 Jul, 2008 - 03:49 AM) *

Looks like the value is still maxing out at 32bits.

Maybe you want this?
CODE

use Math::BigInt;

my $n = Math::BigInt->new("12345678901234567890");
print $n->as_bin(), "\n";



thank you that helped ! now i just need to spend some time improving the code
thankx`s again


" what I get done today I dont have to do Tomarrow ! "
User is offlineProfile CardPM
+Quote Post

evolivid
RE: Any Base To Any Base Converter Stops After 16 Digits
2 Aug, 2008 - 02:44 PM
Post #4

New D.I.C Head
*

Joined: 24 Jul, 2008
Posts: 28


My Contributions
ok i got this to work to an digit heres the code

CODE


#!perl.exe
  
require 'Bigfloat.pl';
use Math::BigInt;

#my $n = Math::BigInt->new("12345678901234567890");
#
#print $n->as_bin(), "\n";


##my $num = Math::BigInt->new($sample);




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);
    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*= Math::BigInt->new($num);                ##$n*= $num;
    }
    return $n;
    }
    sub get_in {
    #my $n = Math::BigInt->new($num);
    #print $n->as_bin(), "\n";
                  
    print shift() . ' ';
    my $data=<STDIN>;
    chomp $data;
    return $data;
  
    }



" what I get done today I dont have to do Tomarrow ! "
[/quote]

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 06:09PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month