--
Hello,
Somehow two functions, irint and nint, have dissapeared from our C
library here at work. I've been given the wonderful task of rewriting them :-)
The first, nint(), seems pretty straightforward.
From the man page:
nint() converts x into int format rounding to the nearest int value,
except halfway cases are rounded to the int value larger in magnitude.
The second, irint,is giving me some trouble.
From the man page:
irint converts x to an integral value according to the current IEEE
rounding direction.
I looked around in some GNU source and found the following sample of
code. It looked like something that I could change to fit my needs.
/*************************************************************************/
static __inline__ __const__ int __inline_irint (double x)
{
union { double d; unsigned long long ll; } u;
u.d = x;
#if _IEEE_FLOAT_
u.ll = (u.ll & 0x8000000000000000LL) | 0x3fe0000000000000LL;
#else
u.ll = (u.ll & 0x8000000000000000LL) | 0x4000000000000000LL;
#endif
return x + u.d;
/************************************************************************/Quote:}
I modified this so that it would work on my machine. sizeof double = 8 bytes,
sizeof unsigned long = 4 bytes.
int irint (double x)
{
union { double d; unsigned long l; } u;
u.d = x;
#if _IEEE_FLOAT_
u.l = (u.l & 0x8000L) | 0x3fe0L;
#else
u.l = (u.l & 0x8000L) | 0x4000L;
#endif
return (x + u.d);
}
One thing that bothered me is that double and unsigned long were different sizes
so I'v also tried changing the first two lines to:
union { float f; unsigned long l; } u;
u.f = (float) x;
I hadn't ever seen "unsigned long long" before. I assume it's machine
specific?
To figure out what is actually going on I've tried a couple of little test
programs composed of the function with some values inserted and the if/else
statements removed but nothing seems to work how I would expect. I think I
understand the IEEE floating point definition :-) and I think I understand what's
going on in when value is written to u.*.
main(){
union { float f; unsigned long l; } u;
u.l = 0x3fe0L; // binary- 0011 111 1110 000
cout << u.f << "\n";
cout << u.l << "\n";
I would expect to get:Quote:}
1.5
16352
What I get is:
2.2914e-41
16352
If I do it the other way:
main(){
unioun {float f; unigned longl; } u;
u.f = 0x3fe0L;
cout << u.f << "\n";
cout << u.l << "\n";
I get:Quote:}
16352 <- This is the number I should get its binary is
0011 1111 1110 0000
1182760960
If anyone would be so kind as to point be in the right direction with
this problem I would really appreciate it. If you happen to have access to the
sorce for these functions I would love to take a look, as I figure my summer job
should be about learning as much as it is about completing a task.
Thanks in advance,
-AHRAN
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Drexel University Johns Hopkins University
Electrical Engineering Applied Physics Laboratory
~~~~~~~~~~~~~~~~~~~~~~ oPInIOnS ArE mY OwN ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~