American Citizen on Wed, 16 Apr 2025 08:42:18 +0200 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
doing radix conversions in floats |
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