Skip to content
Tcl Source Code View Ticket
Login

View Ticket

lseq : inconsistent rounding

Closed / Fixede5f06285ded599f7fbb9951374b54867eeeb6508Created 4 years agoUpdated 3 years ago
Descriptionplain2022-11-01 19:13:24
using a non-integer increment, [lseq] rounding seems to be not consistent:
% lseq 4 40 0.1
4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.0 6.1 6.2 6.300000000000001 6.4 6.5 6.6 6.7 6.800000000000001 6.9 7.0 7.1 7.2 7.300000000000001 7.4 etc
Using a different starting point:
% lseq 6 40 0.1
6.0 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8.0 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 9.0 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 10.0 10.100000000000001 10.2 10.3 etc
Referenced by4
griffin Ticket [e5f06285de]: 3 changes [History diff]2023-04-24 00:04:15
oehharwiki2023-04-25 09:35:27

Thanks for the comment. Thanks to Brian to take it and work on it: check-in: [62eaef043c] , branch: [bug-fa00fbbbabe].

Here are my perhaps unrelated comments:

Displaying of a double in decimal is difficult and may have the effects seen here. This might be intentional, as it might be more exact on the double level.

So, lets try manually with TCL 8.6.13:

% expr {4 + 22 * 0.1} 6.2 % expr {4 + 23 * 0.1} 6.300000000000001 % expr {4 + 24 * 0.1} 6.4

So, the calculation is ok. Only the displayed numbers are "ugly" or "surprising", but this is due to the display as decimal of the double data type.

I would at least think twice, if changing this on the display side is an improvement. 6.3 is not equal to 6.300000000000001

So, the result of expr {4 + 23 * 0.1} will not be any more equal to the lseq element.

Harald

griffinmarkdown2023-04-26 05:40:02

Hi Harald! I appreciate your concerns.
In this case, the long numbers are the result inherent error in converting IEEE floating point representation to decimal, basically, trying to represent a binary number in decimal.
If you work out the math by hand, these extract tiny fractions do not appear.
With careful rounding, the "correct" result can be achieved.

griffinplain2023-04-26 16:26:55
Fix with check-in [a610e50b2faa8c52]
oehharwiki2023-04-27 06:36:58

Wow, great !

Then, also

% expr {4 + 23 * 0.1} 6.300000000000001

will also be corrected? Or is corrected in TCl 9.0?

Great ! Harald

anonymousplain2023-04-27 19:36:12
Thanks for this fix (and the initial implementation), Brian.
Looking forward to start using [lseq].
Harm Olthof
chrstphrchvzwiki2023-06-25 19:47:16

Will this issue be addressed in 8.7, where it is also present?

Log in (anonymous is fine) to add a comment.