www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Split Real / Float into Mantissa, Exponent, and Base

reply Jonathan M. Wilbur <jwilbur jwilbur.info> writes:
I have tried to come up with a good way to get the mantissa, 
exponent, and base from a real number, and I just can't come up 
with a good cross-platform way of doing it. I know about 
std.math.frexp(), but that function only gives you another real 
as the mantissa. I need an integral mantissa, exponent, and base.

Is there either (1) a crafty, cross-platform way of doing this or 
(2) a function in a standard library that does this that I 
somehow missed? If there is no such function, what are your 
thoughts on me implementing such a thing and submitting it to 
Phobos, probably similar to how frexp is implemented (elseifs for 
each FP format)?
Mar 03
next sibling parent pineapple <meapineapple gmail.com> writes:
On Friday, 3 March 2017 at 18:09:02 UTC, Jonathan M. Wilbur wrote:
 I have tried to come up with a good way to get the mantissa, 
 exponent, and base from a real number, and I just can't come up 
 with a good cross-platform way of doing it. I know about 
 std.math.frexp(), but that function only gives you another real 
 as the mantissa. I need an integral mantissa, exponent, and 
 base.

 Is there either (1) a crafty, cross-platform way of doing this 
 or (2) a function in a standard library that does this that I 
 somehow missed? If there is no such function, what are your 
 thoughts on me implementing such a thing and submitting it to 
 Phobos, probably similar to how frexp is implemented (elseifs 
 for each FP format)?
The various functions in std.math mostly extract these values on their own, using the floatTraits template to help. The mach library has the module mach.math.floats.extract: https://github.com/pineapplemachine/mach.d/tree/master/mach/math/floats
Mar 03
prev sibling parent Marc =?UTF-8?B?U2Now7x0eg==?= <schuetzm gmx.net> writes:
On Friday, 3 March 2017 at 18:09:02 UTC, Jonathan M. Wilbur wrote:
 I have tried to come up with a good way to get the mantissa, 
 exponent, and base from a real number, and I just can't come up 
 with a good cross-platform way of doing it. I know about 
 std.math.frexp(), but that function only gives you another real 
 as the mantissa. I need an integral mantissa, exponent, and 
 base.

 Is there either (1) a crafty, cross-platform way of doing this 
 or (2) a function in a standard library that does this that I 
 somehow missed? If there is no such function, what are your 
 thoughts on me implementing such a thing and submitting it to 
 Phobos, probably similar to how frexp is implemented (elseifs 
 for each FP format)?
See std.bitmanip.FloatRep and std.bitmanip.DoubleRep: https://dlang.org/phobos/std_bitmanip.html#.FloatRep https://dlang.org/phobos/std_bitmanip.html#.DoubleRep
Mar 06