seq.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2020, Nico Weber <thakis@chromium.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/StdLibExtras.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. static double get_double(const char* name, const char* d_string, int* number_of_decimals)
  32. {
  33. char* end;
  34. double d = strtod(d_string, &end);
  35. if (d == 0 && end == d_string) {
  36. fprintf(stderr, "%s: invalid double value \"%s\"\n", name, d_string);
  37. exit(1);
  38. }
  39. if (char* dot = strchr(d_string, '.'))
  40. *number_of_decimals = strlen(dot + 1);
  41. else
  42. *number_of_decimals = 0;
  43. return d;
  44. }
  45. int main(int argc, const char* argv[])
  46. {
  47. if (pledge("stdio", nullptr) < 0) {
  48. perror("pledge");
  49. return 1;
  50. }
  51. if (unveil(nullptr, nullptr) < 0) {
  52. perror("unveil");
  53. return 1;
  54. }
  55. double start = 1, step = 1, end = 1;
  56. int number_of_start_decimals = 0, number_of_step_decimals = 0, number_of_end_decimals = 0;
  57. switch (argc) {
  58. case 2:
  59. end = get_double(argv[0], argv[1], &number_of_end_decimals);
  60. break;
  61. case 3:
  62. start = get_double(argv[0], argv[1], &number_of_start_decimals);
  63. end = get_double(argv[0], argv[2], &number_of_end_decimals);
  64. break;
  65. case 4:
  66. start = get_double(argv[0], argv[1], &number_of_start_decimals);
  67. step = get_double(argv[0], argv[2], &number_of_step_decimals);
  68. end = get_double(argv[0], argv[3], &number_of_end_decimals);
  69. break;
  70. default:
  71. fprintf(stderr, "%s: unexpected number of arguments\n", argv[0]);
  72. return 1;
  73. }
  74. if (step == 0) {
  75. fprintf(stderr, "%s: increment must not be 0\n", argv[0]);
  76. return 1;
  77. }
  78. #if 0
  79. // FIXME: Check for NaN once math.h has isnan().
  80. if (isnan(start) || isnan(step) || isnan(end)) {
  81. fprintf(stderr, "%s: start, step, and end must not be NaN\n", argv[0]);
  82. return 1;
  83. }
  84. #endif
  85. int number_of_decimals = max(number_of_start_decimals, max(number_of_step_decimals, number_of_end_decimals));
  86. int n = (end - start) / step;
  87. double d = start;
  88. for (int i = 0; i <= n; ++i) {
  89. char buf[40];
  90. snprintf(buf, sizeof(buf), "%f", d);
  91. if (char* dot = strchr(buf, '.')) {
  92. if (number_of_decimals == 0)
  93. *dot = '\0';
  94. else if ((dot - buf) + 1 + number_of_decimals < (int)sizeof(buf))
  95. dot[1 + number_of_decimals] = '\0';
  96. }
  97. printf("%s\n", buf);
  98. d += step;
  99. }
  100. return 0;
  101. }