Back to index

php5  5.3.10
nearzero.c
Go to the documentation of this file.
00001 /* nearzero.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 /* In some places we need to check if the number NUM is almost zero.
00042    Specifically, all but the last digit is 0 and the last digit is 1.
00043    Last digit is defined by scale. */
00044 
00045 char
00046 bc_is_near_zero (num, scale)
00047      bc_num num;
00048      int scale;
00049 {
00050   int  count;
00051   char *nptr;
00052 
00053   /* Error checking */
00054   if (scale > num->n_scale)
00055     scale = num->n_scale;
00056 
00057   /* Initialize */
00058   count = num->n_len + scale;
00059   nptr = num->n_value;
00060 
00061   /* The check */
00062   while ((count > 0) && (*nptr++ == 0)) count--;
00063 
00064   if (count != 0 && (count != 1 || *--nptr != 1))
00065     return FALSE;
00066   else
00067     return TRUE;
00068 }
00069