Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of ldexp and frexp?

Tags:

python

c

math

Python and C have frexp and ldexp functions (they basically go back and forth between y, and x and i in y = x*2^i).

I guess that exp means exponent (of 2). What is the meaning of "ld" and "fr"? I see in http://pubs.opengroup.org/onlinepubs/009695399/functions/ldexp.html that "ld" could mean "load" (from mantissa/exponent form), but even this is not fully clear. I am not sure about "fr", though (http://pubs.opengroup.org/onlinepubs/9699919799/functions/frexp.html does not similarly give a clue).

So, what is the meaning of the names frexp and ldexp? I am hoping that this could help remembering which is which.

like image 652
Eric O Lebigot Avatar asked Aug 15 '16 09:08

Eric O Lebigot


People also ask

What is Ldexp?

The C library function double ldexp(double x, int exponent) returns x multiplied by 2 raised to the power of exponent.

What is Ldexp in Python?

ldexp() method returns x * (2**i) of the given numbers x and i, which is the inverse of math.


1 Answers

It's very simple. As you already discovered earlier ldexp means load exponent: just multiply number by 2 raised to the power of some value. And the inverse function for ldexp which called frexp means extraction of float radix from a value:

Breaks the floating point number x into its binary significand (a floating point with an absolute value between 0.5(included) and 1.0(excluded)) and an integral exponent for 2, such that.*

ldexp and frexp function in Python came from C since old C90 standart. So, it's very old beard.

*http://www.cplusplus.com/reference/cmath/frexp/

like image 76
Egor Zamotaev Avatar answered Sep 30 '22 13:09

Egor Zamotaev