Back to index

php5  5.3.10
int2num.c
Go to the documentation of this file.
00001 /* int2num.c: bcmath library file. */
00002 /*
00003     Copyright (C) 1991, 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
00004     Copyright (C) 2000 Philip A. Nelson
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Lesser General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Lesser General Public License for more details.  (COPYING.LIB)
00015 
00016     You should have received a copy of the GNU Lesser General Public
00017     License along with this library; if not, write to:
00018 
00019       The Free Software Foundation, Inc.
00020       59 Temple Place, Suite 330
00021       Boston, MA 02111-1307 USA.
00022 
00023     You may contact the author by:
00024        e-mail:  philnelson@acm.org
00025       us-mail:  Philip A. Nelson
00026                 Computer Science Department, 9062
00027                 Western Washington University
00028                 Bellingham, WA 98226-9062
00029        
00030 *************************************************************************/
00031 
00032 #include <config.h>
00033 #include <stdio.h>
00034 #include <assert.h>
00035 #include <stdlib.h>
00036 #include <ctype.h>
00037 #include <stdarg.h>
00038 #include "bcmath.h"
00039 #include "private.h"
00040 
00041 
00042 /* Convert an integer VAL to a bc number NUM. */
00043 
00044 void
00045 bc_int2num (num, val)
00046      bc_num *num;
00047      int val;
00048 {
00049   char buffer[30];
00050   char *bptr, *vptr;
00051   int  ix = 1;
00052   char neg = 0;
00053 
00054   /* Sign. */
00055   if (val < 0)
00056     {
00057       neg = 1;
00058       val = -val;
00059     }
00060 
00061   /* Get things going. */
00062   bptr = buffer;
00063   *bptr++ = val % BASE;
00064   val = val / BASE;
00065 
00066   /* Extract remaining digits. */
00067   while (val != 0)
00068     {
00069       *bptr++ = val % BASE;
00070       val = val / BASE;
00071       ix++;          /* Count the digits. */
00072     }
00073 
00074   /* Make the number. */
00075   bc_free_num (num);
00076   *num = bc_new_num (ix, 0);
00077   if (neg) (*num)->n_sign = MINUS;
00078 
00079   /* Assign the digits. */
00080   vptr = (*num)->n_value;
00081   while (ix-- > 0)
00082     *vptr++ = *--bptr;
00083 }
00084