MCPcopy Create free account
hub / github.com/coreutils/coreutils / print_numbers

Function print_numbers

src/seq.c:299–369  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

297 given or default stepping and format. */
298
299static void
300print_numbers (char const *fmt, struct layout layout,
301 long double first, long double step, long double last)
302{
303 bool out_of_range = (step < 0 ? first < last : last < first);
304
305 if (! out_of_range)
306 {
307 long double x = first;
308
309 for (long double i = 1; ; i++)
310 {
311 long double x0 = x;
312 if (printf (fmt, x) < 0)
313 write_error ();
314 if (out_of_range)
315 break;
316
317 /* Mathematically equivalent to 'x += step;', and typically
318 less subject to rounding error. */
319 x = first + i * step;
320
321 out_of_range = (step < 0 ? x < last : last < x);
322
323 if (out_of_range)
324 {
325 /* If the number just past LAST prints as a value equal
326 to LAST, and prints differently from the previous
327 number, then print the number. This avoids problems
328 with rounding. For example, with the x86 it causes
329 "seq 0 0.000001 0.000003" to print 0.000003 instead
330 of stopping at 0.000002. */
331
332 bool print_extra_number = false;
333 if (locale_ok)
334 setlocale (LC_NUMERIC, "C");
335 char *x_str;
336 int x_strlen = asprintf (&x_str, fmt, x);
337 if (locale_ok)
338 setlocale (LC_NUMERIC, "");
339 if (x_strlen < 0)
340 xalloc_die ();
341 x_str[x_strlen - layout.suffix_len] = '\0';
342
343 long double x_val;
344 if (xstrtold (x_str + layout.prefix_len, NULL,
345 &x_val, cl_strtold)
346 && x_val == last)
347 {
348 char *x0_str = NULL;
349 int x0_strlen = asprintf (&x0_str, fmt, x0);
350 if (x0_strlen < 0)
351 xalloc_die ();
352 x0_str[x0_strlen - layout.suffix_len] = '\0';
353 print_extra_number = !streq (x0_str, x_str);
354 free (x0_str);
355 }
356

Callers 1

mainFunction · 0.85

Calls 2

write_errorFunction · 0.85
xalloc_dieFunction · 0.85

Tested by

no test coverage detected