Page MenuHomeFreeBSD

Bug 217149 - seq(1) inconsistently omits 'last' when using float increment
Needs ReviewPublic

Authored by yuripv_icloud.com on Jan 18 2018, 2:10 PM.
Tags
None
Referenced Files
Unknown Object (File)
Thu, Apr 18, 1:25 AM
Unknown Object (File)
Dec 22 2023, 10:24 PM
Unknown Object (File)
Dec 13 2023, 5:00 AM
Unknown Object (File)
Dec 12 2023, 8:11 AM
Unknown Object (File)
Dec 4 2023, 5:54 PM
Unknown Object (File)
Dec 1 2023, 6:40 AM
Unknown Object (File)
Nov 17 2023, 4:01 PM
Unknown Object (File)
Nov 14 2023, 12:35 PM
Subscribers

Details

Summary

Don't accumulate rounding errors:

loki:yuri:~/ws/pr217149/usr.bin/seq$ seq 1 .1 2
1
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
loki:yuri:~/ws/pr217149/usr.bin/seq$ /usr/obj/home/yuri/ws/pr217149/amd64.amd64/usr.bin/seq/seq 1 .1 2
1
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
2
loki:yuri:~/ws/pr217149/usr.bin/seq$ seq 1.6 .05 2
1.6
1.65
1.7
1.75
1.8
1.85
1.9
1.95
loki:yuri:~/ws/pr217149/usr.bin/seq$ /usr/obj/home/yuri/ws/pr217149/amd64.amd64/usr.bin/seq/seq 1.6 .05 2
1.6
1.65
1.7
1.75
1.8
1.85
1.9
1.95
2

Diff Detail

Repository
rS FreeBSD src repository - subversion
Lint
Lint Skipped
Unit
Tests Skipped

Event Timeline

Someone had a problem and thought solving it with float was a good idea. Then we had 1.999999 problems. :)
This fixes it to use the right calculation that doesn't accumulate error.

This revision is now accepted and ready to land.Jan 18 2018, 2:46 PM
usr.bin/seq/seq.c
189โ€“191

Now that I look at this, I'm thinking it might be better to compute an exact number of steps and just do a simple for loop with step...

for (step = 0; step < max_step; step++)

190

since step starts at one, doesn't this skip the first step?

yuripv_icloud.com added inline comments.
usr.bin/seq/seq.c
189โ€“191

I thought about it, but it doesn't make this any more readable, at least in the way I was able to implement it.

190

No, on first iteration we print cur set to first, and only then compute cur using step being 1.

yuripv_icloud.com marked 2 inline comments as done.

fix the loop

This revision now requires review to proceed.Jan 18 2018, 3:38 PM
usr.bin/seq/seq.c
190

Actually, that was good question, initial revision would break seq 1 2 2 which should print only 1, but did print 1 3, updated.

yuripv_icloud.com added a reviewer: kevans.

Add basic tests for fp increment.

I have added Kyle to reviewers in case he could look if tests are correct..

usr.bin/seq/tests/seq_test.sh
51

Can you split these two sanity checks out into another test, so we don't conflate what's actually a floating point test with what's a test of standard operation? The supported workflow for running tests is to make check or run with kyua, which will run all of the tests, so it's better to structure our tests accordingly.

yuripv_icloud.com marked an inline comment as done.
yuripv_icloud.com added inline comments.
usr.bin/seq/tests/seq_test.sh
51

Done, and thanks for the handholding, it's all a bit new for me.

yuripv_icloud.com marked an inline comment as done.

updated copyright in test script as noted by Kyle.

yuripv_icloud.com added a reviewer: jilles.

update to fix the test case Jilles provided -- compare the string representation as an additional check we didn't just hit rounding error; add relevant test case.