[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Can Bash do simple math?
From: |
alex xmb sw ratchev |
Subject: |
Re: Can Bash do simple math? |
Date: |
Tue, 6 Aug 2024 01:02:20 +0200 |
no floating point math yet , only whole numbers
On Tue, Aug 6, 2024, 01:01 alex xmb sw ratchev <fxmbsw7@gmail.com> wrote:
> ~ $ bash ext.dateseconds 300 700
>
> 700 - 300 ; 400 6 0
>
> ~ $ cat ext.dateseconds
>
> (( sec = $2 - $1 , min = sec / 60 , hr = min / 60 ))
>
> res="$2 - $1 ; $sec $min $hr"
>
> printf %s\\n "$res"
>
> On Tue, Aug 6, 2024, 00:34 <bash@blaklinten.xyz> wrote:
>
>> I have a somewhat strange issue.
>> In a script I use the `((VAR = expression ))` syntax to calculate a
>> simple remainder by division:
>>
>> ((DIFF = $2 - $1))
>> ((SECONDS = DIFF % 60))
>> ((MINUTES = (DIFF % 3600) / 60))
>> ((HOURS = DIFF / 3600))
>>
>> echo "$HOURS:$MINUTES:$SECONDS"
>>
>> However, it seems as if Bash sometimes miscalculates the remainder and
>> prints an errorneous result string.
>>
>> I have written a small example and had some friends run it to confirm
>> this issue is not only on my machine.
>> I highly doubt Bash _sometimes_ miscalculates basic math and neither
>> me or my friends can spot any errors in my code. Am I stupid? What am
>> I missing?
>>
>> The minimal example code to produce an error:
>> https://gist.github.com/blaklinten/3dd29f3f73b75337a86b8d7cdb59bdd7
>>
>> I am eager to hear thoughts on this :)
>>
>>
Re: Can Bash do simple math?, alex xmb sw ratchev, 2024/08/06