Operating System - OpenVMS
1827587 Members
2689 Online
109965 Solutions
New Discussion

How to Convert "long double" to IEEE?

 
SOLVED
Go to solution
Ray Lu
Occasional Contributor

How to Convert "long double" to IEEE?

I'm trying to use the system function CVT$CONVERT_FLOAT to convert native "float", "double", and "long double" to IEEE standard types. I can make "float" and "double" work but not "long double".

In my program, I call the function in this way, "(void)CVT$CONVERT_FLOAT(&input, CVT$K_VAX_H, &output, CVT$K_IEEE_X, CVT$M_BIG_ENDIAN);". I see only CVT$K_VAX_H is close to "long double". It has the size of 16 byte. But "output" has no data.

What else should I do? Thanks.
8 REPLIES 8
John Gillings
Honored Contributor

Re: How to Convert "long double" to IEEE?

What is the return status?

Are you sure your input parameter contains a valid H float value?

Maybe post a complete example that demonstrates the problem?
A crucible of informative mistakes
Ian Miller.
Honored Contributor

Re: How to Convert "long double" to IEEE?

CVT$CONVERT_FLOAT will return a condition value (which you appear to be discarding) which will give you a better idea what the problems is.
____________________
Purely Personal Opinion
Ray Lu
Occasional Contributor

Re: How to Convert "long double" to IEEE?

Let me put my short program here. You can see what I'm trying to do. I see nothing in the printout of s2.

------------------------

#include
#include
#include
#include

int main(void)
{
long double f = 401.1234;
unsigned char s1[16];
unsigned char s2[16];
int i;
long l;

memcpy(s1, &f, 16);
printf("s1=0x");
for(i=15; i>=0; i--)
printf("%02x ", s1[i]);
printf("\n");

l = CVT$CONVERT_FLOAT(&f, CVT$K_VAX_H, s2, CVT$K_IEEE_X, CVT$M_ROUND_TO_NEAREST+CVT$M_BIG_ENDIAN);

printf("s2=0x");
for(i=15; i>=0; i--)
printf("%02x ", s2[i]);
printf("\n");

printf("l=%ld\n", l);

return 0;
}
Ian Miller.
Honored Contributor

Re: How to Convert "long double" to IEEE?

what is the value of l?
____________________
Purely Personal Opinion
Ian Miller.
Honored Contributor

Re: How to Convert "long double" to IEEE?

I get li= 100302857
which is %CVT-S-NORMAL
____________________
Purely Personal Opinion
Ray Lu
Occasional Contributor

Re: How to Convert "long double" to IEEE?

I got 100302857 for l, too. I've tried CVT$K_VAX_F and CVT$K_VAX_G. They worked. So the problem should be in CVT$K_VAX_H, not CVT$K_IEEE_X.
Craig A Berry
Honored Contributor
Solution

Re: How to Convert "long double" to IEEE?

As far as I know there is no data type in C that corresponds to H_FLOAT. In FORTRAN it would be REAL*16, but probably only on VAX. In C, saying "long double" by default means X_FLOAT, the IEEE 128-bit floating point format, though you can override that with compiler options to make it be a D_FLOAT, G_FLOAT, or T_FLOAT value.

What your program is doing is taking an X_FLOAT value, asking that it be interpreted as a big-endian H_FLOAT (a beast that I don't think ever existed in the wild as all native numerics are little endian) and then convert it into an X_FLOAT value, i.e., convert it to what it already was.

You can easily confirm that your long double is an X_FLOAT by building your program for the OpenVMS debugger and doing EXAMINE/X_FLOAT on the "f'" variable. If you are on the VAX platform, you can do EXAMINE/H_FLOAT and see what (little endian) H_FLOAT value you really have.

The nonsensical condition value is problematic and may reflect a bug in the routine. However, you are asking the routine to do something that is logically impossible. What is the real problem you are trying to solve? Do you really have H_FLOAT values sitting around that were written on a VAX?

What's non-intuitive here is that on Alpha and Itanium, your long doubles are 128-bit IEEE values by default regardless of what format your ordinary floats and doubles are in.
Craig A Berry
Honored Contributor

Re: How to Convert "long double" to IEEE?

Actually, I take that back about the return value. It's probably successfully and silently underflowing to zero.

If you want to see how H_FLOAT is laid out, go to:

http://h71000.www7.hp.com/doc/82final/6443/6443pro_046.html#sec_fltng_pt_vax

and for X_FLOAT:

http://h71000.www7.hp.com/doc/82final/6443/6443pro_027.html#sec_real16