Newbie question: missing math.h functions?

Newbie question: missing math.h functions?

Post by Ahran S. Dunsmo » Thu, 14 Jul 1994 06:05:30

        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;
  u.ll = (u.ll & 0x8000000000000000LL) | 0x3fe0000000000000LL;
  u.ll = (u.ll & 0x8000000000000000LL) | 0x4000000000000000LL;
  return x + u.d;



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;
                        u.l = (u.l & 0x8000L) | 0x4000L;
                        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

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.*.
        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:
What I get is:
If I do it the other way:
        unioun {float f; unigned longl; } u;
        u.f = 0x3fe0L;
        cout << u.f << "\n";
        cout << u.l << "\n";                      

I get:
16352                   <- This is the number I should get its binary is
                           0011 1111 1110 0000

        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,


       Drexel University                Johns Hopkins University    
    Electrical Engineering             Applied Physics Laboratory  

~~~~~~~~~~~~~~~~~~~~~~ oPInIOnS ArE mY OwN ~~~~~~~~~~~~~~~~~~~~~~~~~~~~


1. BSD math library function equivs in Solris 2.2

Hi all,
        I hope I am posting to the right group this
time. ( I have just been gently flamed for posting to
the wrong group )

        I am trying to port the program DSTOOL from
BSD based SunOS to SysVr4 based Solris2.2, but I
am having problems with no-longer available c-library
routines :
                nint & iszero - both SunOS 4.1.x only      
        Does anyone know the routines that can replace these
 under Solaris 2.x ?

        I successfully replaced getwd with getcwd , but this
is marked as alternate in Sun's dec. 1992 tech. bulletin.

The errors I recieve are as follows:
Undefined                       first referenced
 symbol                             in file
iszero                              /nova1/dstool/src/computation/complib/complib.a(blas.o)
nint                                /nova1/dstool/src/computation/complib/complib.a(color_filter.o)
ld: fatal: Symbol referencing errors. No output written to /nova1/dstool/bin/dstool
*** Error code 1
make: Fatal error: Command failed for target `dstool'
Current working directory /nova1/dstool/src
*** Error code 1
make: Fatal error: Command failed for target `dstool'


                        Colin Linahan
                        Unix System Administrator
                        Mathematics & Computing
                        Ballarat University College

2. need date/time for NCSA httpd

3. GCC math functions not found

4. login Problems

5. why are the float math functions unimplemented or defined ?

6. Linux Frequently Asked Questions with Answers (Part 3 of 6)

7. Looking for IEEE math functions

8. ThinkPad A30 Problems w/ X

9. Math functions in STREAMS Modules/Drivers

10. Math functions

11. Help: compiler doesn't recognize math functions

12. Trouble finding C's math functions: log10(), sin, etc...

13. Weird problems with math functions ( linking etc.)