5.16 How to convert from floating point to Hex?
From: Robert Israel (israel@math.ubc.ca)
Subject: Re: Getting non-integral results in hex
Newsgroups: comp.soft-sys.math.maple
Date: 2003-06-13 00:07:37 PST
I assume you mean floating-point numbers. Note that
Maple floats (as opposed to "hardware floats") are
in fact stored in base 10. To convert a float to hex
with n digits after the ".", you can use this:
> `convert/hexfloat`:= proc(x::numeric, n::nonnegint)
local A,B,ax,R;
if nargs = 1 then return procname(x,round(Digits*log[16](10))) fi;
if x = 0 then return cat(`0.`,`0`$n) fi;
ax:= abs(x);
A:= floor(ax);
B:= round(frac(ax)*16^n);
if B = 16^n then A:= A+1; B:= 0 fi;
R:= cat(convert(A,hex),`.`);
if x < 0 then R:= cat(`-`,R) fi;
cat(R,substring(convert(16^n+B,hex),2..-1));
end;
And then, e.g.:
> convert(1234.5678, hexfloat, 4);
4D2.915B