Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

3 votes
3 answers
867 views
How much guarantee is there that glibc math functions are consistent across versions?
I am working with a Monte-Carlo based simulation program for particle physics and I am trying to understand some of the randomness I observe. The principle of this program is that it injects a particle into some medium and then it uses a random number generator to decide what type of interactions it...
I am working with a Monte-Carlo based simulation program for particle physics and I am trying to understand some of the randomness I observe. The principle of this program is that it injects a particle into some medium and then it uses a random number generator to decide what type of interactions it has. This allows it to create multiple possible paths for the particle to take. As a user, I can specify the seed values used to initiate the RNG, to ensure reproducibility later on. Now, I recently came across something weird. The computing cluster I am working on did an **OS upgrade**. All the files were preserved, as well as the binary of the simulation program. However, **the simulation no longer produces the same results** given the identical seed values as before. So somehow the same binary, on the same hardware, but with a different Linux version, is producing a different sequence of interactions. Looking at the documentation of the simulation framework, I found that the RNG used internally is the RANMAR generator (https://paulbourke.net/miscellaneous/random/) . This is supposed to be a completely portable RNG, thus not dependent on the system you use. Hence this should not be the source of the different output. After talking to the developers, they concluded that the difference would probably be in how some mathematical manipulations are done (for example trigonometric functions). After all, the binary is dynamically linked to the standard library and this was indeed changed during the OS upgrade. **Specifically, the glibc version was upgraded from 2.17 to 2.34**. I have tried to look online whether this _can_ make a difference for functions included in libc and libm, but I have not been able to find any conclusive answer. I found [some](https://access.redhat.com/solutions/7032163) mention of differences in newer versions, but this sounds more like a bug that was later on patched. Therefore, my question: Is there a guarantee that mathematical functions in newer versions of glibc will produce the exact same results? And maybe related, is there any easy way to check what changed between two versions of the library?
Satjaiga (49 rep)
Feb 12, 2025, 08:49 AM • Last activity: Feb 13, 2025, 03:53 PM
-1 votes
1 answers
59 views
Unix 'find' utility : Question about relation to Kleene Star in Mathematics
Does anyone know how the Unix `find` search -name queries with `_?` in leftmost column relate to Kleene Star as defined on [Wikipedia][1]? How can I accomplish the rows with `_?` using `find`? . : empty string, X : cartesian product -name \A\B Language A X B -name \ABC Language A X B X C -name : nam...
Does anyone know how the Unix find search -name queries with _? in leftmost column relate to Kleene Star as defined on Wikipedia ? How can I accomplish the rows with _? using find? . : empty string, X : cartesian product -name \A\B Language A X B -name \ABC Language A X B X C -name : name at leafs, builds set of string in language -name \. V^0 {e, ""} language only of the empty string A with only one leg? -name \? V^1 V language of single characters -name \???... V^n language of n character strings, concatenates strings in language -name \* V^* langauge of \cup_{i\gte0} V^i a? -name \n V^n like \{n\} in grep with integer? b? -name \*n (V^*)^n language of n words, word can be empty -name \*\??... (V^*)^n -name \?\* V^+ langauge of \cup_{i\gte1} V^i -name \?\*\??... (V^+)^n language of n words, word must have character It appears that . is a graph A with only one leg defined with length. grep uses \{n\} for a?, however I can't seem to find the equivalent with find.
notaorb (101 rep)
Jan 24, 2025, 09:16 AM • Last activity: Jan 24, 2025, 02:46 PM
0 votes
0 answers
26 views
In manjaro linux, how to get the math rendering extension to work with inkscape
I have downloaded inkscape through the software manager (GUI) in manjaro. By default it doesn't seem to render math under the extensions drop down. So I have tried multiple solutions. I have installed texlive and textext through pacman and pamac, closed inkscape and even restarted the computer, but...
I have downloaded inkscape through the software manager (GUI) in manjaro. By default it doesn't seem to render math under the extensions drop down. So I have tried multiple solutions. I have installed texlive and textext through pacman and pamac, closed inkscape and even restarted the computer, but I don't get the option in the drop down still. What should I do?
Makogan (113 rep)
Sep 13, 2024, 01:14 AM
1 votes
2 answers
116 views
7.5 * 2 results in 15., with a trailing dot. How can I get rid of it?
Using `zsh`: - `width=7.25; echo $(( width * 2 ))`: the output is `14.5` - `width=7.5; echo $(( width * 2 ))`: the output is `15.`, with a trailing dot This seems to be a very basic thing, but I haven't figured it out. How can I get just `15`, without the trailing dot, in the second example?
Using zsh: - width=7.25; echo $(( width * 2 )): the output is 14.5 - width=7.5; echo $(( width * 2 )): the output is 15., with a trailing dot This seems to be a very basic thing, but I haven't figured it out. How can I get just 15, without the trailing dot, in the second example?
jsx97 (1347 rep)
Aug 23, 2024, 07:01 PM • Last activity: Aug 25, 2024, 01:51 PM
2 votes
2 answers
542 views
primality test binary
I'm generating numbers using a Java program (BigIntegers) and I want to know if there's a binary readily available that I could use to run primality tests on the numbers generated.... suppose I feed them through a pipe from my java program into the binary. Is it out there? I'm trying to find package...
I'm generating numbers using a Java program (BigIntegers) and I want to know if there's a binary readily available that I could use to run primality tests on the numbers generated.... suppose I feed them through a pipe from my java program into the binary. Is it out there? I'm trying to find packages for aks on apt but I see nothing "straightforward", only libs that I could use to program stuff (like, based on GMP).
eftshift0 (707 rep)
Aug 5, 2017, 02:18 AM • Last activity: Aug 3, 2024, 06:32 PM
0 votes
3 answers
728 views
arbitrary base conversion from base 10 using only builtins in bash
I'm trying to sort out a bash command to convert a number from base 10 to an arbitrary base, using a specified set of characters (for example, to base 26 using letters a-z, although that's not actually my use-case). I *have* solved this problem before, but not in bash (which I have limited experienc...
I'm trying to sort out a bash command to convert a number from base 10 to an arbitrary base, using a specified set of characters (for example, to base 26 using letters a-z, although that's not actually my use-case). I *have* solved this problem before, but not in bash (which I have limited experience in), and not in quite some time. Any ideas?
Hate9 (21 rep)
Jun 27, 2023, 01:40 PM • Last activity: Jan 31, 2024, 02:31 PM
1 votes
1 answers
30 views
Cartesian application software recommendation in Debian
Is there a software recommendation for an application for the Cartesian field in Debian?
Is there a software recommendation for an application for the Cartesian field in Debian?
Darevil294 (13 rep)
Jan 4, 2024, 02:22 AM • Last activity: Jan 4, 2024, 07:18 AM
-1 votes
1 answers
159 views
SpeedCrunch is Providing Slow Math Calculations
I'm having a performance issue with SpeedCrunch 0.12 in Debian 12: sudo apt install speedcrunch First of all, I love the UI of SpeedCrunch, as a calculator, but currently (for me) it is giving very slow results for even simple math problems like `1 + 1 = 2`. The tool tip shows the result instantly,...
I'm having a performance issue with SpeedCrunch 0.12 in Debian 12: sudo apt install speedcrunch First of all, I love the UI of SpeedCrunch, as a calculator, but currently (for me) it is giving very slow results for even simple math problems like 1 + 1 = 2. The tool tip shows the result instantly, but when I press the enter key (to output the result) it takes about 7 seconds to output the calculation. For just doing one calculation, I can endure the wait, but this 7 second delay (when doing multiple consecutive calculations) after every calculation is unbearable. Is this only happening for me? If so, what might be causing this? If this is a bug, are there other calculators, with a similar interface, that anyone can recommend for precise mathematics (until this is fixed)?
Lonnie Best (5415 rep)
Nov 17, 2023, 04:51 PM • Last activity: Nov 22, 2023, 12:20 PM
0 votes
2 answers
103 views
math problem or variable value problem im not sure
I am having an issue comparing epoch times. I'm really not sure what I'm doing wrong. I have been tasked with identifying and actioning files beyond and during certain date ranges. In this case, it's a cleanup script. It takes a few parameters to do the search and then checks the dates. My dates are...
I am having an issue comparing epoch times. I'm really not sure what I'm doing wrong. I have been tasked with identifying and actioning files beyond and during certain date ranges. In this case, it's a cleanup script. It takes a few parameters to do the search and then checks the dates. My dates are like this: earliest date we can delete a file ->
-d "-30days" +"%s"

earliest date we can compress a file ->
-d "-14days" +"%s"

get the file last modified time:
-c "%y" "/path/to/file"

get the epoch from that last modified time:
-d "" +"%s"
The math goes like this:
if (( FILE_TIMESTAMP >= EARLIEST_DELETE_DATE ))
then
    delete file;
elif (( FILE_TIMESTAMP >= EARLIEST_COMPRESS_DATE ))
then
    compress file;
else
    echo "file skipped as it wasnt in teh date ranges";
fi
(edit to the above code: I removed the check for 2023-05-07 03:20:29.000000000 -0400 [Time: ][Thread: 883837][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:100] [Method: manageFileLifecycle.sh] - FILE_TIMESTAMP -> 1683444029
The file shown is older than 6 months but was not triggered by the >= delete clause. not sure what I'm doing wrong

For @markp-fuso:
declare -- FILE_TIMESTAMP="1688178017" declare -i EARLIEST_DELETE_DATE="1697037095" declare -i EARLIEST_COMPRESS_DATE="1698419495" FILE_TO_PROCESS -> /opt/cws/log/S93206319.log, FILE_DATESTAMP -> 2023-06-30 22:20:17.000000000 -0400 FILE_TIMESTAMP -> 1688178017, EARLIEST_DELETE_DATE -> 1697037095, EARLIEST_COMPRESS_DATE -> 1698419495
In the log output I see this file being skipped, even though it shouldn't have been.

Also, I must apologize - I cannot share the whole script as it is company property =(

More information:

Specific file pulled that ISN'T working:
declare -- FILE_TIMESTAMP="1679107439" declare -i EARLIEST_DELETE_DATE="1697040305" declare -i EARLIEST_COMPRESS_DATE="1698422705" FILE_TO_PROCESS -> /opt/cws/log/S93382366.log, FILE_DATESTAMP -> 2023-03-17 22:43:59.000000000 -0400 FILE_TIMESTAMP -> 1679107439, EARLIEST_DELETE_DATE -> 1697040305, EARLIEST_COMPRESS_DATE -> 1698422705 file skipped as it wasnt in teh date ranges
and
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:93] [Method: manageFileLifecycle.sh] - processRequestedFile -> enter [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:94] [Method: manageFileLifecycle.sh] - Provided arguments: /opt/cws/log/S93382366.log [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:95] [Method: manageFileLifecycle.sh] - ERROR_COUNT -> 0 [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:96] [Method: manageFileLifecycle.sh] - FILE_TO_PROCESS -> /opt/cws/log/S93382366.log [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:97] [Method: manageFileLifecycle.sh] - FILE_CORE_NAME -> S93382366 [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:98] [Method: manageFileLifecycle.sh] - FILE_CORE_EXT -> log [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:99] [Method: manageFileLifecycle.sh] - FILE_DATESTAMP -> 2023-03-17 22:43:59.000000000 -0400 [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:100] [Method: manageFileLifecycle.sh] - FILE_TIMESTAMP -> 1679107439 [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:107] [Method: manageFileLifecycle.sh] - FILE_CORE_EXT and FILE_CORE_NAME do not match - executing with a file extension [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:139] [Method: manageFileLifecycle.sh] - File has extension log [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:298] [Method: manageFileLifecycle.sh] - RETURN_CODE -> 0 [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:299] [Method: manageFileLifecycle.sh] - processRequestedFile -> exit
A file that appears to have at least hit the delete clause:

Output from the typeset:
declare -- FILE_TIMESTAMP="1703308529" declare -i EARLIEST_DELETE_DATE="1697040305" declare -i EARLIEST_COMPRESS_DATE="1698422705" FILE_TO_PROCESS -> /opt/cws/log/S20504215.log, FILE_DATESTAMP -> 2023-12-23 00:15:29.000000000 -0500 FILE_TIMESTAMP -> 1703308529, EARLIEST_DELETE_DATE -> 1697040305, EARLIEST_COMPRESS_DATE -> 1698422705
Output from the debug log:
[Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:93] [Method: manageFileLifecycle.sh] - processRequestedFile -> enter [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:94] [Method: manageFileLifecycle.sh] - Provided arguments: /opt/cws/log/S20504215.log [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:95] [Method: manageFileLifecycle.sh] - ERROR_COUNT -> 0 [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:96] [Method: manageFileLifecycle.sh] - FILE_TO_PROCESS -> /opt/cws/log/S20504215.log [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:97] [Method: manageFileLifecycle.sh] - FILE_CORE_NAME -> S20504215 [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:98] [Method: manageFileLifecycle.sh] - FILE_CORE_EXT -> log [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:99] [Method: manageFileLifecycle.sh] - FILE_DATESTAMP -> 2023-12-23 00:15:29.000000000 -0500 [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:100] [Method: manageFileLifecycle.sh] - FILE_TIMESTAMP -> 1703308529 [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:107] [Method: manageFileLifecycle.sh] - FILE_CORE_EXT and FILE_CORE_NAME do not match - executing with a file extension [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:139] [Method: manageFileLifecycle.sh] - File has extension log [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:143] [Method: manageFileLifecycle.sh] - File timestamp is greater than or equal to the delete date. File -> /opt/cws/log/S20504215.log, Timestamp -> 1703308529, EARLIEST_DELETE_DATE -> 1697035331 [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:148] [Method: manageFileLifecycle.sh] - Deleting file /opt/cws/log/S20504215.log as it falls after the earliest delete date range. [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: processRequestedFile:149] [Method: manageFileLifecycle.sh] - EXEC: deleteRequestedFile /opt/cws/log/S20504215.log [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: deleteRequestedFile:462] [Method: manageFileLifecycle.sh] - deleteRequestedFile -> enter [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: deleteRequestedFile:463] [Method: manageFileLifecycle.sh] - Provided arguments: /opt/cws/log/S20504215.log [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: deleteRequestedFile:464] [Method: manageFileLifecycle.sh] - REMOVABLE_FILE -> /opt/cws/log/S20504215.log [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: deleteRequestedFile:465] [Method: manageFileLifecycle.sh] - REMOVABLE_FILE_NAME -> S20504215.log [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: deleteRequestedFile:466] [Method: manageFileLifecycle.sh] - REMOVABLE_FILE_PATH -> /opt/cws/log [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: deleteRequestedFile:470] [Method: manageFileLifecycle.sh] - Checking if /opt/cws/log exists... [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: deleteRequestedFile:516] [Method: manageFileLifecycle.sh] - Returning to previous directory /opt/cws/log [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: deleteRequestedFile:520] [Method: manageFileLifecycle.sh] - Previous directory: /opt/cws/log, New working directory: /opt/cws/log [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: deleteRequestedFile:530] [Method: manageFileLifecycle.sh] - RETURN_CODE -> 1 [Time: ][Thread: 8066][Log: debug.log][Level: DEBUG] - [File: deleteRequestedFile:531] [Method: manageFileLifecycle.sh] - deleteRequestedFile -> exit ``` The goal is to delete any file older than 30 days from the current date, compress anything older than 14 days from the current date but not older than 30 days from the current date (these entries would fall under the delete) and anything younger than 14 days is left alone.
Kevin Huntly (74 rep)
Nov 10, 2023, 01:33 PM • Last activity: Nov 10, 2023, 08:48 PM
0 votes
0 answers
52 views
After cpu time hits 8128 seconds my calculations stops
I'm new to Linux and for my bachelor thesis I'm using it in order to calculate properties of materials in physics, therefore heavy calculations are involved. I've noticed that whenever Cpu time hits 8128 second ALL of my calculations are shut down and the code is like this: - pw.x is the program tha...
I'm new to Linux and for my bachelor thesis I'm using it in order to calculate properties of materials in physics, therefore heavy calculations are involved. I've noticed that whenever Cpu time hits 8128 second ALL of my calculations are shut down and the code is like this: - pw.x is the program that takes an input file in order to start calculations (diaginalize matrices , find eigenvalues, stuff like that); - Example.in is the input file for instance ; - The program gives me back Example.out The line of code in terminal is : mpirun.openmpi -np 8 pw.x Example.out $ And when I type: tail -f Example.out to see my progress I can see that the program gives me back the time spent by the CPU in doing calculations. When this time hits 8162 seconds everything stops, even if I press via terminal "top" I cannot see the pw.x programme running :( Can you please help me?
CoolerThanACooler (101 rep)
Jul 23, 2023, 05:21 PM • Last activity: Jul 24, 2023, 01:35 AM
3 votes
2 answers
24451 views
How do you type a number with a bar symbol over it?
100 divided by 3 equals 33.3 [bar][1]. How can I put a bar symbol over the three like this: [![enter image description here][2]][2] You'd think there would be 9 Unicode characters dedicated to this, but I couldn't find them. [1]: https://en.wikipedia.org/wiki/Vinculum_(symbol) [2]: https://i.sstatic...
100 divided by 3 equals 33.3 bar . How can I put a bar symbol over the three like this: enter image description here You'd think there would be 9 Unicode characters dedicated to this, but I couldn't find them.
Lonnie Best (5415 rep)
Dec 5, 2021, 05:13 AM • Last activity: Jun 17, 2023, 10:00 AM
0 votes
3 answers
89303 views
Prime Number Calculation using shell scripting
Last line is not executing when the number is prime. Please help #!/bin/bash echo -e "Enter Number : \c" read n while [ $n -gt 2 ] do for((i=2; i<=$n/2; i++)) do ans=$(( n%i )) if [ $ans -eq 0 ] then echo "$n is not a prime number." exit 0 fi done done echo "$n is a prime number." --- The new code:...
Last line is not executing when the number is prime. Please help #!/bin/bash echo -e "Enter Number : \c" read n while [ $n -gt 2 ] do for((i=2; i<=$n/2; i++)) do ans=$(( n%i )) if [ $ans -eq 0 ] then echo "$n is not a prime number." exit 0 fi done done echo "$n is a prime number." --- The new code: #!/bin/bash echo -e "Enter Number : \c" read n for((i=2; i<=$n/2; i++)) do ans=$(( n%i )) if [ $ans -eq 0 ] then echo "$n is not a prime number." exit 0 fi done echo "$n is a prime number."
Ankit Kumar (13 rep)
Mar 21, 2020, 02:21 PM • Last activity: Apr 9, 2023, 11:24 AM
4 votes
4 answers
709 views
awk or perl to eval mathematical expressions in each line
I want a script to make awk to become an interactive mathematical calculator, to eval mathematical expressions given in each line. I.e., instead of constructing awk commands to calculate expressions like the following: ``` $ awk 'BEGIN{print 180/1149}' 0.156658 $ awk 'BEGIN{print (150+141)/1149}' 0....
I want a script to make awk to become an interactive mathematical calculator, to eval mathematical expressions given in each line. I.e., instead of constructing awk commands to calculate expressions like the following:
$ awk 'BEGIN{print 180/1149}'
0.156658

$ awk 'BEGIN{print (150+141)/1149}'
0.253264
I want my script to take my mathematical expressions as input and do the calculation interactively. So the session will look like (alternative of input and output):
180/1149
0.156658
(150+141)/1149
0.253264
1 + 2
3
2 * 3 - 5
1
However I'm not able to do that myself:
$ awk '{print}'
180/1149
180/1149
^C

$ awk '{print $0}'
180/1149
180/1149
1 + 2
1 + 2
^C
If there is no simple solution to awk, what else, like perl?
xpt (1858 rep)
Mar 24, 2023, 04:53 PM • Last activity: Mar 25, 2023, 12:00 PM
2 votes
1 answers
110 views
Galculator weird behavior
Why does `galculator` miscalculate `4000225165312/512` and gets `781293977.6`? **Note** This is intended as a **canonical question** cautioning against the unreflected use of `galculator` 1.2.4. * Please do not use it. Here [(1)][1] [(2)][2] [(3)][3] are some good reasons why. * I've already uninsta...
Why does galculator miscalculate 4000225165312/512 and gets 781293977.6? **Note** This is intended as a **canonical question** cautioning against the unreflected use of galculator 1.2.4. * Please do not use it. Here (1) (2) (3) are some good reasons why. * I've already uninstalled it because I was ashamed by "my" calculations today. I've replaced it with mate-calc which computes all of these expressions properly.
Artem S. Tashkinov (32730 rep)
Feb 12, 2023, 08:46 PM • Last activity: Feb 13, 2023, 02:00 PM
4 votes
2 answers
7477 views
How to sum a bash array of numbers (some in scientific notation)?
Since the following command using bc does not work for numbers in scientific notation, I was wondering about an alternative, e.g. using awk? sum=$( IFS="+"; bc <<< "${arrValues[*]}" )
Since the following command using bc does not work for numbers in scientific notation, I was wondering about an alternative, e.g. using awk? sum=$( IFS="+"; bc <<< "${arrValues[*]}" )
MaVe (339 rep)
Nov 6, 2013, 03:59 PM • Last activity: Jan 18, 2023, 02:12 PM
3 votes
5 answers
4792 views
Round/truncate digit in string in zsh (or with external tool)
I'm trying to do an interface to `bc` so it can be used intuitively and without the annoyance of getting "stuck" in it. I haven't got around to test it that much, because I got stuck on another detail, namely how to present the result (which is, I think, a string). Rounding or truncating does not ma...
I'm trying to do an interface to bc so it can be used intuitively and without the annoyance of getting "stuck" in it. I haven't got around to test it that much, because I got stuck on another detail, namely how to present the result (which is, I think, a string). Rounding or truncating does not matter, either one is fine. Take a look below, and you'll understand immediately. I use zsh but an external tool will be just fine as I won't use this in any time or otherwise critical context, it's just a desktop tool. calc () { result=`bc <noglob gets away with the quotes! But, the use of a dot to force floating point calculation is something I'll never remember (you don't use a normal calculator like that). And it is even a bit risky, especially for calculations when it's not obvious that floating point would yield an altogether different result (most likely the one you wanted). Also, the calculations below show some un-pretty output (the too long real, and the trailing dot). Perhaps I should combine this (some of it) with the output formatting of @Gille's [answer](https://unix.stackexchange.com/a/66621/13136) below? When I get it to work perfectly, I'll post the result here. (**Edit:** The accepted [answer](https://unix.stackexchange.com/a/66755/13136) works great. Be sure to read the comments to that answer, as well.) calc () { echo $(($*)); } alias calc='noglob calc' calc 1./3 0.33333333333333331 calc 7.5 - 2.5 5.
Emanuel Berg (7101 rep)
Mar 1, 2013, 10:26 PM • Last activity: Dec 30, 2022, 01:21 PM
9 votes
8 answers
3861 views
Command line expression solver?
I'm looking for a Linux-compatible TTY-based calculator. For example: user@host:~$ calculate > 2 2 user@host:~$ calculate > 8*6-4 44 user@host:~$ calculate > 8*(6-4) 16 Is there anything like this that supports basic operations, some built-in functions like `atan()`, and possibly custom functions vi...
I'm looking for a Linux-compatible TTY-based calculator. For example: user@host:~$ calculate > 2 2 user@host:~$ calculate > 8*6-4 44 user@host:~$ calculate > 8*(6-4) 16 Is there anything like this that supports basic operations, some built-in functions like atan(), and possibly custom functions via scripting?
Lucas Phillips (449 rep)
Dec 14, 2013, 02:00 AM • Last activity: Nov 14, 2022, 02:23 PM
-1 votes
2 answers
220 views
Trying to complete Project Euler #5, passes all syntax checks but doesn't work
trying to complete [Project Euler #5][1]. The code I have should logically work, and it passes ShellCheck, but gives no output for some reason. The code is below. Thanks and sorry if this should be in a different stack exchange site #!/bin/bash i=1 while [[ $((i % 2)) -eq 0 && $((i % 3)) -eq 0 && $(...
trying to complete Project Euler #5 . The code I have should logically work, and it passes ShellCheck, but gives no output for some reason. The code is below. Thanks and sorry if this should be in a different stack exchange site #!/bin/bash i=1 while [[ $((i % 2)) -eq 0 && $((i % 3)) -eq 0 && $((i % 5)) -eq 0 && $((i % 7)) -eq 0 && $((i % 11)) -eq 0 && $((i % 13)) -eq 0 && $((i % 17)) -eq 0 && $((i % 19)) -eq 0 ]] do i=$((i+1)) done echo $i
Egrodo (207 rep)
Nov 23, 2016, 06:59 PM • Last activity: Sep 13, 2022, 07:43 PM
2 votes
3 answers
167 views
Replacing math equations by result
What is the best way to replace, in a file, equations found using some predefined regex with the result. Let's say each equation formula is compatible with `bc -l` (basic calculator with float numbers handling). For example, saying equations are delimited by `[[` and `]]` (just as an example): Input...
What is the best way to replace, in a file, equations found using some predefined regex with the result. Let's say each equation formula is compatible with bc -l (basic calculator with float numbers handling). For example, saying equations are delimited by [[ and ]] (just as an example): Input would be: Results show that each unit should generate approx. [[7*9/2.0]]Wh per day. All the same ... Desired output: Results show that each unit should generate approx. 31.5Wh per day. All the same ... My best try: while read line; do if [[ $line =~ \[\[.*\]\] ]]; then equ=echo "$line" | sed "s|.*\[\[||" | sed "s|]].*||" res=echo "$equ" | bc -l | awk '{print $1+0}' new_line=echo $line | sed "s|\[\[.*]]|$res|" echo $new_line else echo $line fi done < $infile Output: Results show that each unit should generate approx. 31.5Wh per day. All the same ... But wonder if there would not be a simpler way (without the while loop). In addition, this will work if there is only one equation per line.
taalf (23 rep)
Jul 28, 2022, 07:14 AM • Last activity: Jul 29, 2022, 01:35 PM
2 votes
4 answers
1940 views
How can I feed a string to awk for the purpose of mathematical calculation?
#!/bin/sh -- expression_to_evaluate='12 + 3' printf '%s\n' "Arithemtic Expansion: $(( $expression_to_evaluate ))" printf '%s' 'bc: ' printf '%s\n' "$expression_to_evaluate" | bc printf '%s' 'awk: ' awk -v expression_to_evaluate="$expression_to_evaluate" -- 'BEGIN{printf "%d\n", expression_to_evaluat...
#!/bin/sh -- expression_to_evaluate='12 + 3' printf '%s\n' "Arithemtic Expansion: $(( $expression_to_evaluate ))" printf '%s' 'bc: ' printf '%s\n' "$expression_to_evaluate" | bc printf '%s' 'awk: ' awk -v expression_to_evaluate="$expression_to_evaluate" -- 'BEGIN{printf "%d\n", expression_to_evaluate}' Output: Arithemtic Expansion: 15 bc: 15 awk: 12 awk is returning 12 instead of 15. How can I feed a string to awk for the purpose of mathematical calculation?
Harold Fischer (1974 rep)
Mar 1, 2019, 05:44 AM • Last activity: May 20, 2022, 07:33 PM
Showing page 1 of 20 total questions