Compile time float binary representation
Jeremie Pelletier
jeremiep at gmail.com
Fri Jul 31 19:15:29 PDT 2009
Andrei Alexandrescu Wrote:
> Jeremie Pelletier wrote:
> > Is there a way to convert a float (or double/real) to an integral number without changing its binary representation at compile time?
> >
> > I need to extract the sign, exponent and mantissa yet I cant use bit shifting.
> > "Error: 'R' is not of integral type, it is a real" is the error I get.
> >
> > The usual *cast(uint*)&value wont work either at compile time.
> >
> > Any suggestions?
>
> I got this to work, would it be applicable to your need?
>
> void main(string[] args)
> {
> union A
> {
> double x;
> int y;
> }
> enum A a = A(0.3);
> writeln(a.x);
> writeln(a.y);
> }
>
>
> Andrei
Good suggestion, however it doesnt work the way I need it:
---
template FloatParts(real R) {
union A {
real theReal;
struct {
ushort hi;
ulong lo;
}
}
A a = cast(A)R; // Error: cannot cast real to immutable(A)
enum A a = A(R); // members cannot be used at runtime
}
---
I need to gather .sign, .exp and .mantissa among others (possibly a .radix too) and use them at compile time (generic metaprogramming, or else i would just fallback to runtime).
Unless there are arithmetic methods to get these, I dont see how it can be done without changes in the compiler first.
To Walter: is this something possible for DMD2.32?
More information about the Digitalmars-d
mailing list