Back to index

glibc  2.9
bug-strtod.c
Go to the documentation of this file.
00001 /* Test to strtod etc for numbers like x000...0000.000e-nn.
00002    This file is part of the GNU C Library.
00003    Copyright (C) 2001 Free Software Foundation, Inc.
00004    Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
00005 
00006    The GNU C 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.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C 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.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 
00025 
00026 int
00027 main (void)
00028 {
00029   char buf[300];
00030   int cnt;
00031   int result = 0;
00032 
00033   for (cnt = 0; cnt < 200; ++cnt)
00034     {
00035       ssize_t n;
00036       float f;
00037 
00038       n = sprintf (buf, "%d", cnt);
00039       memset (buf + n, '0', cnt);
00040       sprintf (buf + n + cnt, ".000e-%d", cnt);
00041       f = strtof (buf, NULL);
00042 
00043       if (f != (float) cnt)
00044        {
00045          printf ("strtof(\"%s\") failed for cnt == %d (%g instead of %g)\n",
00046                 buf, cnt, f, (float) cnt);
00047          result = 1;
00048        }
00049       else
00050        printf ("strtof() fine for cnt == %d\n", cnt);
00051     }
00052 
00053   for (cnt = 0; cnt < 200; ++cnt)
00054     {
00055       ssize_t n;
00056       double f;
00057 
00058       n = sprintf (buf, "%d", cnt);
00059       memset (buf + n, '0', cnt);
00060       sprintf (buf + n + cnt, ".000e-%d", cnt);
00061       f = strtod (buf, NULL);
00062 
00063       if (f != (double) cnt)
00064        {
00065          printf ("strtod(\"%s\") failed for cnt == %d (%g instead of %g)\n",
00066                 buf, cnt, f, (double) cnt);
00067          result = 1;
00068        }
00069       else
00070        printf ("strtod() fine for cnt == %d\n", cnt);
00071     }
00072 
00073   for (cnt = 0; cnt < 200; ++cnt)
00074     {
00075       ssize_t n;
00076       long double f;
00077 
00078       n = sprintf (buf, "%d", cnt);
00079       memset (buf + n, '0', cnt);
00080       sprintf (buf + n + cnt, ".000e-%d", cnt);
00081       f = strtold (buf, NULL);
00082 
00083       if (f != (long double) cnt)
00084        {
00085          printf ("strtold(\"%s\") failed for cnt == %d (%Lg instead of %Lg)\n",
00086                 buf, cnt, f, (long double) cnt);
00087          result = 1;
00088        }
00089       else
00090        printf ("strtold() fine for cnt == %d\n", cnt);
00091     }
00092 
00093   return result;
00094 }