u/playsthebongcloud

Is there an explicit formula to obtain the numerator and/or denominator of a rational number?

I'm looking for two functions Q -> Z such that gcd(num(x), den(x)) = 1 and num(x)/den(x) = x. So, num(0.5) = 1, and den(0.5) = 2. I assume it will be an infinite series, but can't find anything online because it all just brings up elementary "what is a fraction" articles.

Edit: Here is the solution: https://www.reddit.com/r/learnmath/s/nkH0H0eaBv

reddit.com
u/playsthebongcloud — 18 hours ago

What is the exact formula to convert the components of an IEEE754 float of arbitrary precision a real number?

*to a real number

(For all of this, I'm ignoring the existence of special values such as nan or infinity.) A popular video on the quake 3 inverse square root algorithm claims the formula, given the mantissa, exponent, and sign of a 32 bit float is as follows:

(-1)^s * (2^(e - 127)) * (1 + m/(2^23))

However, plugging in zero for all variables does not result in zero, but rather 2^-127. Floats are capable of exactly representing zero, but this formula seems to suggest otherwise. Looking at the actual bits of the zero float in C confirms that floats do store it using the integer zero, so what's up here? Is zero a special value with an exception carved out for it?

I want to do the exact same math on these numbers that the computer is doing, so two's complement is needed in the exponent to denote sign; it is not allowed to be a negative number. I'm looking for a general result for all possible numbers of bits in the mantissa and exponent, so I would expect this formula to work identically for doubles as well (given that you swap E=8 for E=11 and M=23 for M=52).

So, to fully state my problem:

Given the components of a floating point number: E (the number of exponent bits), M (the number of mantissa bits), s (the sign, 0 or 1), e (the exponent, non-negative, less than 2^E), and m (the mantissa, non-negative, less than 2^M), what is the formula to convert the float to its corrosponding real?

reddit.com
u/playsthebongcloud — 5 days ago