[Tutor] Need a better name for this function

Richard D. Moores rdmoores at gmail.com
Thu Dec 17 00:06:19 CET 2009


On Wed, Dec 16, 2009 at 14:48, Richard D. Moores <rdmoores at gmail.com> wrote:

I just realized that my question was absurd, in that given real
numbers n and x there is no x such that both x < n and x is greater
than all other numbers less than n.

So after inserting "(in 14 hex digits)" at 2 places, my question is:

How can I compute the equivalents of your
"0x1.9999999999999p-4 -- 0.1" and "0x1.999999999999bp-4 -- 0.1", which
are, respectively, the closest hex value (in 14 hex digits) to a.hex()
that is smaller
than a.hex(), and the the closest hex value (in 14 hex digits) to
a.hex() that is greater
than a.hex()?

> This is what I have so far:
> ======================================================
> import decimal
> float2decimal = decimal.Decimal.from_float
>
> a = .435
> stringval = a.hex()
> print("internal repr of", a, "is ", stringval)
>
>
> def test(stringval):
>    floatval = float.fromhex(stringval)
>    decimalval = float2decimal(floatval)
>    print( stringval, "--", floatval, "--", decimalval )
>
> test(stringval)
>
> OUTPUT:
> internal repr of 0.435 is  0x1.bd70a3d70a3d7p-2
> 0x1.bd70a3d70a3d7p-2 -- 0.435 --
> 0.434999999999999997779553950749686919152736663818359375
> =============================================
>
> Thanks,
>
> Dick
>


More information about the Tutor mailing list