xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Test to strtod etc for numbers like x000...0000.000e-nn. |
| 2 | This file is part of the GNU C Library. |
| 3 | Copyright (C) 2001-2016 Free Software Foundation, Inc. |
| 4 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2001. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | |
| 25 | int |
| 26 | main (void) |
| 27 | { |
| 28 | char buf[300]; |
| 29 | int cnt; |
| 30 | int result = 0; |
| 31 | |
| 32 | for (cnt = 0; cnt < 200; ++cnt) |
| 33 | { |
| 34 | ssize_t n; |
| 35 | float f; |
| 36 | |
| 37 | n = sprintf (buf, "%d", cnt); |
| 38 | memset (buf + n, '0', cnt); |
| 39 | sprintf (buf + n + cnt, ".000e-%d", cnt); |
| 40 | f = strtof (buf, NULL); |
| 41 | |
| 42 | if (f != (float) cnt) |
| 43 | { |
| 44 | printf ("strtof(\"%s\") failed for cnt == %d (%g instead of %g)\n", |
| 45 | buf, cnt, f, (float) cnt); |
| 46 | result = 1; |
| 47 | } |
| 48 | else |
| 49 | printf ("strtof() fine for cnt == %d\n", cnt); |
| 50 | } |
| 51 | |
| 52 | for (cnt = 0; cnt < 200; ++cnt) |
| 53 | { |
| 54 | ssize_t n; |
| 55 | double f; |
| 56 | |
| 57 | n = sprintf (buf, "%d", cnt); |
| 58 | memset (buf + n, '0', cnt); |
| 59 | sprintf (buf + n + cnt, ".000e-%d", cnt); |
| 60 | f = strtod (buf, NULL); |
| 61 | |
| 62 | if (f != (double) cnt) |
| 63 | { |
| 64 | printf ("strtod(\"%s\") failed for cnt == %d (%g instead of %g)\n", |
| 65 | buf, cnt, f, (double) cnt); |
| 66 | result = 1; |
| 67 | } |
| 68 | else |
| 69 | printf ("strtod() fine for cnt == %d\n", cnt); |
| 70 | } |
| 71 | |
| 72 | for (cnt = 0; cnt < 200; ++cnt) |
| 73 | { |
| 74 | ssize_t n; |
| 75 | long double f; |
| 76 | |
| 77 | n = sprintf (buf, "%d", cnt); |
| 78 | memset (buf + n, '0', cnt); |
| 79 | sprintf (buf + n + cnt, ".000e-%d", cnt); |
| 80 | f = strtold (buf, NULL); |
| 81 | |
| 82 | if (f != (long double) cnt) |
| 83 | { |
| 84 | printf ("strtold(\"%s\") failed for cnt == %d (%Lg instead of %Lg)\n", |
| 85 | buf, cnt, f, (long double) cnt); |
| 86 | result = 1; |
| 87 | } |
| 88 | else |
| 89 | printf ("strtold() fine for cnt == %d\n", cnt); |
| 90 | } |
| 91 | |
| 92 | return result; |
| 93 | } |