Commit graph

28 commits

Author SHA1 Message Date
Jeffrey Finkelstein
294bde8e08 seq: correct width for certain negative decimals
Fix a bug in which a negative decimal input would not be displayed with
the correct width in the output. Before this commit, the output was
incorrectly

    $ seq -w -.1 .1 .11
    -0.1
    0.0
    0.1

After this commit, the output is correctly

    $ seq -w -.1 .1 .11
    -0.1
    00.0
    00.1

The code was failing to take into account that the input decimal "-.1"
needs to be displayed with a leading zero, like "-0.1".
2021-12-23 20:37:29 -05:00
Sylvestre Ledru
177374aa5a
Merge pull request #2740 from jfinkels/seq-inf-width-spaces
seq: correct fixed-width spacing for inf sequences
2021-11-12 21:16:40 +01:00
Thomas Queiroz
c9624725ab
tests: use CmdResult::usage_error 2021-11-09 17:37:05 -03:00
Jeffrey Finkelstein
0b86afa858 seq: correct fixed-width spacing for inf sequences
Pad infinity and negative infinity values with spaces when using the
`-w` option to `seq`. This corrects the behavior of `seq` to match that
of the GNU version:

    $ seq -w 1.000 inf inf | head -n 4
    1.000
      inf
      inf
      inf

Previously, it incorrectly padded with 0s instead of spaces.
2021-11-08 20:12:54 -05:00
jfinkels
2e12316ae1
seq: use BigDecimal to represent floats (#2698)
* seq: use BigDecimal to represent floats

Use `BigDecimal` to represent arbitrary precision floats in order to
prevent numerical precision issues when iterating over a sequence of
numbers. This commit makes several changes at once to accomplish this
goal.

First, it creates a new struct, `PreciseNumber`, that is responsible for
storing not only the number itself but also the number of digits (both
integer and decimal) needed to display it. This information is collected
at the time of parsing the number, which lives in the new
`numberparse.rs` module.

Second, it uses the `BigDecimal` struct to store arbitrary precision
floating point numbers instead of the previous `f64` primitive
type. This protects against issues of numerical precision when
repeatedly accumulating a very small increment.

Third, since neither the `BigDecimal` nor `BigInt` types have a
representation of infinity, minus infinity, minus zero, or NaN, we add
the `ExtendedBigDecimal` and `ExtendedBigInt` enumerations which extend
the basic types with these concepts.

* fixup! seq: use BigDecimal to represent floats

* fixup! seq: use BigDecimal to represent floats

* fixup! seq: use BigDecimal to represent floats

* fixup! seq: use BigDecimal to represent floats

* fixup! seq: use BigDecimal to represent floats
2021-11-06 15:44:42 +01:00
vulppine
cddd40b4e1 seq: Updates hex parse readability, adds hex test 2021-10-05 18:41:28 -07:00
vulppine
4e1f945e86 seq: Adds testing for large hex numbers 2021-10-03 09:50:49 -07:00
vulppine
aad0682a40 seq: Adds testing for hexadecimal integer parsing 2021-10-02 08:46:09 -07:00
Jan Verbeek
7ea2bfbe26 seq: replace loops with a single format string
Replace two loops that print leading and trailing 0s when printing a
number in fixed-width mode with a single call to `write!()` with the
appropriate formatting parameters.
2021-09-18 10:27:03 -04:00
Jeffrey Finkelstein
bfb1327ad4 seq: use print_seq_integers() regardless of last
Ensure that the `print_seq_integers()` function is called when the first
number and the increment are integers, regardless of the type of the
last value specified.
2021-09-18 10:27:03 -04:00
Jeffrey Finkelstein
2ac5dc0a70 seq: compute correct width for scientific notation
Change the way `seq` computes the number of digits needed to print a
number so that it works for inputs given in scientific notation.
Specifically, this commit parses the input string to determine whether
it is an integer, a float in decimal notation, or a float in scientific
notation, and then computes the number of integral digits and the number
of fractional digits based on that. This also supports floating point
negative zero, expressed in both decimal and scientific notation.
2021-09-17 23:49:54 -04:00
Jeffrey Finkelstein
60025800c3 seq: trim leading whitespace from inputs 2021-09-13 21:46:17 -04:00
Jeffrey Finkelstein
96b8616a1a seq: use stdout.write_all() instead of print!()
Change from using `print!()` to using `stdout.write_all()` in order to
allow the main function to handle broken pipe errors gracefully.
2021-09-10 20:00:26 -04:00
Jeffrey Finkelstein
5cd55391ec seq: compute width of numbers after parsing
Fix a bug in `seq` where the number of characters needed to print the
number was computed incorrectly in some cases. This commit changes the
computation of the width to be after parsing the number instead of
before, in order to accommodate inputs like `1e3`, which requires four
digits when printing the number, not three.
2021-08-28 17:43:36 -04:00
Jeffrey Finkelstein
2c66d9e0a7 seq: print negative zero at start of integer seq. 2021-08-27 21:44:09 -04:00
Michael Debertol
5f2335829a refactor ~ revert to single quotes for "Try '{0 --help'"
This is a test expectation for gnu.
2021-08-14 17:22:09 +02:00
Roy Ivy III
4da46d93c7 tests ~ fix tests for new execution_phrase!() and usage phrasing 2021-08-14 14:01:34 +02:00
Sylvestre Ledru
6aa53ead7c rustfmt the recent change 2021-06-02 18:43:35 +02:00
Michael Debertol
5329d77cc2 seq: adapt output to GNU seq 2021-06-01 20:35:18 +02:00
Michael Debertol
9b29ac98a5 seq: reject NaN arguments
Move the validation logic to an argument validator.
2021-06-01 18:30:18 +02:00
Michael Debertol
4cf18e96f3 seq: change default value for -t and remove dubious escape sequences
GNU seq does not support -t, but always outputs a newline at the end.
Therefore, our default for -t should be \n.

Also removes support for escape sequences (interpreting a literal "\n"
as a newline). This is not what GNU seq is doing, and unexpected.
2021-05-31 21:20:19 +02:00
Michael Debertol
6ccc305513 seq: implement integer sequences
If we notice that we can represent all arguments as BigInts, take a
different code path. Just like GNU seq this means we can print an
infinite amount of numbers in this case.
2021-05-31 21:20:12 +02:00
Sylvestre Ledru
2c09556964 rustfmt some tests 2021-03-13 23:30:47 +01:00
James Robson
0dbed0fd59 Do not allow seq to run with an increment of zero 2021-02-18 21:10:53 +00:00
Sylvestre Ledru
94e293f2bf fix formatting fr test_seq 2021-01-02 10:14:18 +01:00
Sylvestre Ledru
469abf2427 bug(seq) - Allow 'seq 6 -1 0'
Was failing with
```
Found argument '-1' which wasn't expected, or isn't valid in this context
```
otherwise
2020-12-19 11:55:43 +01:00
Sylvestre Ledru
b4969c6cc2 test(seq): add a test to check that we don't accept more than 3 args 2020-10-25 10:44:50 -05:00
Roy Ivy III
de0375f909 tests ~ reorganize tests 2020-06-01 18:30:04 -05:00
Renamed from tests/test_seq.rs (Browse further)