doing radix conversions in floats

American Citizen <[email protected]>
Newsgroups gmane.comp.mathematics.pari.user
Message-ID <[email protected]>
Has anyone ever written a radix conversion program for converting 
decimal floats to other radixes such a base 16?

I wrote a dec2hex and hex2dec for integers which seems to work, if I 
stipulated that the output of the numbers in other bases than 10 is a 
delimited string "0xnnn".

I post the code for both below, but ask if anyone has come up with radix 
conversions to the right of the decimal point (and in the current 
working precision of gp-pari) ?

For example the square root of 2 to 39 digits is 
1.41421356237309504880168872420969807857 but the equivalent base 16 
representation is 1.6a09e667f3bcc908b2fb1366ea957d3e3adec1751277509a 
when I make an attempt to have the equivalent hexadecimal number.

Randall

You are welcome to use this code, btw.

{dec2hex(n,b=16)=
my(m,d,s);
m=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
d=digits(n,b);
s="0x";
for(i=1,#d,s=strexpand(s,m[d[i]+1]););
return(s);
}
addhelp(dec2hex,"dec2hex(n,{base=16}) returns hexadecimal string \"0xn\" 
for decimal input n. Other bases (2..15) are possible");

{hex2dec(h,b=16)=
my(m,v,s);
m=Map(["0",0; "1",1; "2",2; "3",3; "4",4; "5",5; "6",6; "7",7; "8",8; 
"9",9; "a",10; "b",11; "c",12; "d",13; "e",14; "f",15]);
if(type(h)!="t_STR",return;);
v=Vecrev(strsplit(h));
s=0;
for(i=1,#v-2,s+=mapget(m,v[i])*(b^(i-1)););
return(s);
}
addhelp(hex2dec,"hex2dec(n,{base=16}) returns decimal n for hexadecimal 
string input h. Other number strings in bases(2..15) can be converted 
also");

Perhaps I will write up the gp-pari code to do the radix floats
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.