[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Arithmetic expression: evaluation order bug
From: |
Alain D D Williams |
Subject: |
Re: Arithmetic expression: evaluation order bug |
Date: |
Thu, 29 Dec 2022 17:35:48 +0000 |
User-agent: |
Mutt/1.10.1 (2018-07-13) |
On Thu, Dec 29, 2022 at 06:23:09PM +0100, Steffen Nurpmeso wrote:
> Hello.
>
> Name: bash
> Path: /usr/ports/core
> Version: 5.2.15
> Release: 1
>
> $ i=10 j=20;echo $(( i += j += i += j ));echo $i,$j
> 60
> 60,50
> $ i=10 j=20;echo $(( i += j += i += i ));echo $i,$j
> 50
> 50,40
You are modifying something that is used elsewhere in an expression. I am not
surprised that you do not get what you expect; others might expect something
different.
At most there should be a note in the documentation that this sort of thing
leads to undefined behaviour -- that is what happens in other languages.
This is very much not a bug in bash.
C does it differently. I would not be surprised to see different C compilers
produce different results or different results with different levels of
optimisation.
> $ cat t.c
> #include <stdio.h>
> int main(void){
>
> int i, j;
>
> i = 10, j = 20;
> i += j += i += j;
> printf("%d,%d\n", i, j);
>
> i = 10, j = 20;
> i += j += i += i;
> printf("%d,%d\n", i, j);
>
> return 0;
> }
> $ tcc -run t.c
> 80,50
> 60,40
>
> --steffen
> |
> |Der Kragenbaer, The moon bear,
> |der holt sich munter he cheerfully and one by one
> |einen nach dem anderen runter wa.ks himself off
> |(By Robert Gernhardt)
>
--
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT
Lecturer.
+44 (0) 787 668 0256 https://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information:
https://www.phcomp.co.uk/Contact.html
#include <std_disclaimer.h>
- Arithmetic expression: evaluation order bug, Steffen Nurpmeso, 2022/12/29
- Re: Arithmetic expression: evaluation order bug,
Alain D D Williams <=
- Re: Arithmetic expression: evaluation order bug, Steffen Nurpmeso, 2022/12/29
- Re: Arithmetic expression: evaluation order bug, Emanuele Torre, 2022/12/29
- Re: Arithmetic expression: evaluation order bug, Alain D D Williams, 2022/12/29
- Re: Arithmetic expression: evaluation order bug, Steffen Nurpmeso, 2022/12/29
- Re: Arithmetic expression: evaluation order bug, Alain D D Williams, 2022/12/29
- Re: Arithmetic expression: evaluation order bug, Andreas Schwab, 2022/12/29
- Re: Arithmetic expression: evaluation order bug, Steffen Nurpmeso, 2022/12/29
- Re: Arithmetic expression: evaluation order bug, Andreas Schwab, 2022/12/29
- Re: Arithmetic expression: evaluation order bug, Steffen Nurpmeso, 2022/12/29
- Re: Arithmetic expression: evaluation order bug, Andreas Schwab, 2022/12/29