Bill Allombert on Wed, 16 Apr 2025 10:24:05 +0200 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Re: doing radix conversions in floats |
On Tue, Apr 15, 2025 at 11:42:11PM -0700, American Citizen wrote: > 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". In GP you can do ? strprintf("0x%x",7123456) %89 = "0x6cb200" ? eval("0x6cb200") %90 = 7123456 > 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. You can start with something like d2h(x)=my(f=frac(x));strprintf("%x.%x",truncate(x-f),f*16^((bitprecision(f)+exponent(f))\4)) h2d(s)=my([a,b]=strsplit(s,"."));eval(Str("0x",a))+eval(Str("0x",b))/16^#b*bitprecision(1.,4*#b) ? d2h(sqrt(2)) %123 = "1.6a09e667f3bcc908b2fb1366ea957d3" ? h2d(%) %124 = 1.4142135623730950488016887242096980785 Cheers, Bill