How to extract dyadic fraction from float
Asked Answered
V

2

7

Now, floating and double-precision numbers, although they can approximate any sort of number (although the same could be said integers, floats are just more precise), they are represented as binary decimals internally. For example, one tenth would be approximated

0.00011001100110011... (... only goes to computers precision, not infinity)

Now, any number in binary with finite bits as something called a dyadic fraction representation in mathematics (has nothing to do with p-adic). This means you represent it as a fraction, where the denominator is a power of 2. For example, let's say our computer approximates one tenth as 0.00011. The dyadic fraction for that is 3/32 or 3/(2^5), which is close to one tenth. Now for my technical question. What would be the simplest way to extract the dyadic fraction from a floating number.

Irrelevant Note: If you are wondering why I would want to do this, it is because I am creating a surreal number library in Haskell. Dyadic fractions are easily translated into Surreal numbers, which is why it is convenient that binary is easily translated into dyadic, (I'll sure have trouble with the rational numbers though.)

Vernal answered 22/7, 2014 at 21:52 Comment(3)
+1 for a Surreal numbers library for Haskell.Lavinia
It would only be Surreals such that there left and right set is countable.Vernal
Probably only the ones where the left and right sets are computably enumerable, but that should be enough for anyone. :)Lavinia
T
5

The decodeFloat function seems useful for this. Technically, you should also check that floatRadix is 2, but as far I can see this is always the case in GHC.

Just be careful since it does not simplify mantissa and exponent. Here, if I evaluate decodeFloat (1.0 :: Double) I get an exponent of -52 and a mantissa of 2^52 which is not what I expected.

Also, toRational seems to generate a dyadic fraction. I am not sure this is always the case, though.

Tirol answered 22/7, 2014 at 22:3 Comment(2)
Well, all IEEE-754 floating point values are dyadic fractions. So if toRational is always exact, then its output will be.Petra
Yes, toRational is exact.Baggy
H
0

Hold your numbers in binary and convert to decimal for display.

Binary numbers are all dyatic. The numbers after the decimal place represent the number of powers of two for the denominator and the number evaluated without a decimal place is the numerator. That's binary numbers for you.

There is an ideal representation for surreal numbers in binary. I call them "sinary". It's this: 0s is Not a number 1s is zero 10s is neg one 11s is one 100s is neg two 101s is neg half 110s is half 111s is two ... etc...

so you see that the standard binary count matches the surreal birth order of numeric values when evaluated in sinary. The way to determine the numeric value of sinary is that the 1's are rights and the 0's are lefts. We start with +/-1's and then 1/2, 1/4, 1/8, etc. With sign equal to + for 1 and - for 0.

ex: evaluating sinary

1011011s 
-> is the 91st surreal number (because 64+16+8+2+1 = 91)
-> with a value of −0.28125, because...
1011011
NLRRLRR
+-++-++
+ 0 − 1 + 1/2 + 1/4 − 1/8 + 1/16 + 1/32
= 0 − 32/32 + 16/32 + 8/32 − 4/32 + 2/32 + 1/32
= − 9/32

The surreal numbers form a binary tree, so there is an ideal binary format matching their location on the tree according to the Left/Right pattern to reach the number. Assign 1 to right and 0 to left. Then the birth order of surreal number is equal to the binary count of this representation. ie: the 15th surreal number value represented in sinary is the 15th number representation in the standard binary count. The value of a sinary is the surreal label value. Strip the leading bit from the representation, and start adding +1's or -1's depending on if the number starts with 1 or 0 after the first one. Then once the bit flips, begin adding and subtracting halved values (1/2, 1/4, 1/8, etc) using + or - values according to the bit value 1/0.

I have tested this format and it seems to work well. And there are some other secrets... such as the left and right of any sinary representation is the same binary format with the tail clipped to the last 0 and last 1 respectively. Conversion to decimal into a dyatic is NOT required in order to preform the recursive functions requested by Conway.

Hardi answered 8/6, 2020 at 5:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.