help-bash
[Top][All Lists]
Advanced

[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 16:36:54 +0200

On Tue, Aug 6, 2024, 16:35 alex xmb sw ratchev <fxmbsw7@gmail.com> wrote:

>
>
> 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))
>>
>
> just .. what u do diff % 60 , 'SECONDS' it is already .. % 60 ( if it
> counts as how many 60thiet times ) is minutes already
> i suppose u mean $2(seconds) -1(seconds) , = seconds
> if thats not ur case plz explain
>

also subseconds , its / 10 or / 100 or / 1000 ..
60 , is one minute in seconds
there is no second / 60 thing
afik ?

  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 :)
>>
>>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]