Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem with dumping long double

18 views
Skip to first unread message

takanaev

unread,
May 18, 2011, 1:15:25 PM5/18/11
to
At the first sorry for my English.

Here I have a problem with following code:

[code]
#include <stdio.h>
#include <float.h>

#if defined(FLOAT) && !defined(DOUBLE) && !defined(LONG_DOUBLE)
typedef float real;
#elif !defined(FLOAT) && defined(DOUBLE) && !defined(LONG_DOUBLE)
typedef double real;
#elif !defined(FLOAT) && !defined(DOUBLE) && defined(LONG_DOUBLE)
typedef long double real;
#else
#error Error: define FLOAT or DOUBLE or LONG_DOUBLE only
#endif

typedef union { real r; unsigned char ca[sizeof(real)]; } utype;

unsigned char get_max_unity(void)
{
volatile unsigned char i, j;

for(i = j = 1; j != 0; j <<= 1)
{
i = j;
}

return i;
}

void dump(utype number)
{
static unsigned char max_unity = 0;
unsigned char c;
int i, count;

if(!max_unity)
max_unity = get_max_unity();

for(count = 0, i = sizeof(real) - 1; i >= 0; i--)
{
for(c = max_unity; c != 0; c >>= 1, count++)
{
#if defined(FLOAT)
if(count == 1 || count == 9)
printf("*");
#elif defined(DOUBLE)
if(count == 1 || count == 12)
printf("*");
#elif defined(LONG_DOUBLE)
if(count == 1 || count == 16)
printf("*");
#endif

if((unsigned char)(number.ca[i] & c) == (unsigned char)0)
printf("%u", 0);
else
printf("%u", 1);
}
}
printf("\n");
}

int main(void)
{
real get_epsilon(void);
utype ut;

ut.r = -1.0;
dump(ut);
}
[/code]

Compling gcc main.c -DFLOAT or gcc main.c -DDOUBLE gives me correct
binary representation of float or double:
(* separates sign, exponent and mantissa)

FLOAT: 1*01111111*00000000000000000000000
DOUBLE:
1*01111111111*0000000000000000000000000000000000000000000000000000

but gcc -main.c -DLONG_DOUBLE gives incorrect result:

0*000000000000000*10111111111111111000000000000000000000000000000000000000000000000000000000000000

(wanted:
1*0111111111111111*0000000000000000000000000000000000000000000000000000000000000000000000000000000
)

Could you advice solution, pls?
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

0 new messages