Questions tagged [floating-point]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1 vote
2 answers
98 views

Replicating floating point arithmetic WITHOUT bc utility

I'm needing to write a script on some embedded Linux systems that don't have bc installed on them and won't be able to have it installed. The script I'm writing is essentially a monitoring script that ...
user avatar
  • 13
0 votes
3 answers
70 views

Using numbers with decimal points in if statements and operators

I'm trying to write a script that detects the Linux OS and version and through the use of conditions and operators execute different commands depending on the result. Detect OS if [ -f /etc/os-release ...
user avatar
0 votes
1 answer
114 views

Invalid arithmetic operator (error token is " 7.16.3 when passing float from read command

I am trying to automate software update with bash script. When I am passing version number e.g 7.16.3 I get following error: ") Syntax error Invalid arithmetic operator (error token is " I ...
user avatar
0 votes
1 answer
25 views

My calculation is getting converted to integer by default(KSH) [duplicate]

I am assigning a decimal value to a variable which is an output from an oracle command. when I echo the variable I see the decimal value. For example, echo $a is showing me .7214 but when I do ...
user avatar
  • 1
0 votes
4 answers
75 views

Floating point comparison in shell

I am comparing the floating point values in shell script based on this reference. Following is the script contents num1=50.960 num2=6.65E+07 echo "${num1} < ${num2}" | bc When I ran the ...
user avatar
  • 133
1 vote
2 answers
385 views

Convert a textual representation of a float to actual numeral using command line

I have a double printed as text. Is there a way to pass that source text number to some utility in order to get the actual numeral i.e. the nearest actual representable double value? Either in decimal/...
user avatar
  • 1,195
0 votes
1 answer
94 views

How to shorten decimal output in bash?

Right now I am working on an assignment that involves advanced piping, and one of the questions involves calculating a percent and shortening it to 3 decimal places. I've managed to calculate the ...
user avatar
0 votes
0 answers
91 views

Multi page, float16 image viewer

Looking for some utility to easily view multipage tiff files, which can display float16/uint16 data, and be able to easy to display next image in directory. On windows I was using Irfanview for it. I ...
user avatar
2 votes
1 answer
182 views

POSIX floating comparison

working with a legacy system, here I'm connecting with sqlplus to store the returned value in a variable and its worked fine with me but the problem with compearing with floating values a numeric/...
user avatar
2 votes
2 answers
354 views

How many decimal places could awk's rand() give you?

How many decimal places will rand() give? I assume rand() value can't be any completely arbitrary number from 0 up to excluding 1 and that it's limited to a certain amount of decimal places or ...
user avatar
  • 339
4 votes
1 answer
3k views

How to drop decimal place on numbers in a JSON array?

What shell command in a bash script could be issued to efficiently strip all the decimal places from the numbers in a JSON file such as this: [ { "IMSKU": "1000001", ...
user avatar
  • 4,883
0 votes
1 answer
549 views

Blender fails to launch - Floating Point Exception (Core Dumped)

I am new to Linux so excuse my noob. I haven't been able to find an answer for this anywhere nor have I even found anyone with a similar problem and no one on discord has an answer either.. I am ...
user avatar
0 votes
3 answers
41 views

Discard integer and keep floating point

I'm trying to remove an integer from a decimal value that I currently have. current syntax: h=$(echo "scale=2; (($e/$g/$g))" | bc) echo $h The following is used to convert seconds to minutes ...
user avatar
  • 25
2 votes
1 answer
123 views

Gnuplot Precission

Does anyone know whether there is a way to run gnuplot with quadruple arithmetic precision? E.g. When you want to plot a line and you want to subtract a value like "2....
user avatar
31 votes
2 answers
5k views

Which is the current decimal separator?

Say I have a POSIX shell script that needs to run on different systems/environments that I do not control, and needs to remove the decimal separator from a string that is emitted by a program that ...
user avatar
  • 1,095
6 votes
1 answer
3k views

seq decimal separator

Using a seq command with floating point numbers, my output comes with commas instead of dots as decimal separators, despite using dots in the input: seq 0.1 0.3 1.3 0,1 0,4 0,7 1,0 1,3 I assumed ...
user avatar
  • 10.6k
0 votes
0 answers
169 views

Restricting the awk output of df sum to 2 decimal points [duplicate]

I have a script to calculate the cumulative capacity in all the servers. for i in `cat servers` do ssh user@$i "df -t xfs --total | grep total"; done > capout.txt awk -vDT="Capacity" ' {...
user avatar
  • 255
1 vote
4 answers
6k views

Floating point numbers in bash [duplicate]

Let's say I have a variable and I want to print 5 significant digits after I multiplied it by 1000. zsh can do it: zsh$ x=2.8026407e+00 zsh$ printf "%.5g\n" "$(( 1000*${x} ))" zsh> 2802.6 Can ...
user avatar
  • 5,092
2 votes
1 answer
128 views

Understanding the od utility output in regards to floats 'od -t f'

I am making my own od utility based on the Single UNIX Specification and I am confused on how the representation of floats is handled e.g. od -t f. I have a perfectly functioning utility otherwise ...
user avatar
2 votes
1 answer
7k views

How to change regional settings in Linux, including decimal point

I was on Linux Mint, now I'm trying Ubuntu 18 main distro. At top of Gnome day of week/time is displayed. That's where I started, wanting to change it to English having set regional to Russian via GUI,...
user avatar
1 vote
3 answers
5k views

Rounding off to nearest number

I have a file with data like this vserver-1 vserver-1_root 0.95 0.0019043 0.948047 vserver-1 home 10.00 8.25 1.75 vserver-1 usr 95 45.65 39.35 vserver-1 file0 100 89.15 10.85 Desired formatted ...
user avatar
3 votes
1 answer
1k views

Why is LDBL_MAX 1.18973E+4932 and how is this possible?

If I write a C program to say the value of LDBL_MAX the largest value for a long double, #include <float.h> #include <stdio.h> int main(void) { const long double max = LDBL_MAX; ...
user avatar
  • 3,270
5 votes
2 answers
774 views

Why does $((0.1)) expand to 0.10000000000000001 in zsh?

In zsh: $ echo $((0.1)) 0.10000000000000001 While in other shells with floating point arithmetic expansion: $ ksh93 -c 'echo $((0.1))' 0.1 $ yash -c 'echo $((0.1))' 0.1 Or awk: $ awk 'BEGIN{print ...
user avatar
0 votes
1 answer
693 views

Setting Input field to 2 decimal points (Price)

I am currently trying to input prices into a .txt file. So i want it when the user input:1.1 Instead of 1.1, I want it to become 1.10 instead. As it would look better for the table I am trying to ...
user avatar
12 votes
2 answers
15k views

Can bash do floating-point arithmetic without using an external command? [duplicate]

I have read that bash can do integer arithmetic without using an external command, for example: echo "$((3 * (2 + 1)))" Can bash also do floating-point arithmetic without using an external command?
user avatar
0 votes
3 answers
734 views

Only allow floating points regex

How can I check if a specific string is a floating points? This are possible floating points: 12.245 +.0009 3.11e33 43.1E11 2e-14 This is what I tried: grep "^[+\-\.0-9]" grep "^[+-]*[0-9]" grep "^...
user avatar
  • 137
1 vote
1 answer
765 views

Checking that a decimal number is in a range in bc

I've written this if, that is obviously not working, and I still can't manage to get over it: #LASTEFFECTIVEHASH if (( $(echo "$LASTEFFECTIVEHASHMINVAL < $LASTEFFECTIVEHASH < $...
user avatar
  • 358
3 votes
3 answers
2k views

How to compare two float values in ksh?

To compare float values below command is working fine in bash, But not working in ksh on AIX. if (( $(bc <<< " 5.3 >= 5.3") ));then echo "hi";else echo "bye";fi`, I had tried in so many ...
user avatar
1 vote
3 answers
682 views

Adding zero in front of small decimal places [duplicate]

so when I'm coding Im trying to have the output print out 0.12 instead of .12. Here is my code below echo -n "What is the total cost? "; read cents cost=$(echo "scale =2;$cost_in_cents/100" | bc); ...
user avatar
2 votes
1 answer
663 views

Decimal to Hexadecimal conversion working in Linux but not in Unix

I want to convert a decimal number to hexadecimal number. I used the command : printf '%X\n' 053698180 This command is working perfectly fine on Linux server. But throwing the below error when ...
user avatar
  • 21
1 vote
4 answers
5k views

Regular expression to match floating point numbers in shell script

I'm using a regular expression to match floating point numbers: for f in $float_numbers ; do if [ $f =~ "[0-9]*\.?[0-9]*" ] ; then echo "****f is $f ****" fi done where $...
user avatar
  • 1,309
2 votes
2 answers
4k views

Convert exponential notation to decimal number in awk

I have space-separated file. This file contains text as well as numbers in exponential format. I want to convert exponential numbers to actual decimal values. Can someone suggest how I can achieve ...
user avatar
  • 23
5 votes
1 answer
874 views

Bash shell decimal test [duplicate]

My shell is bash and I have three variables x=5; y=7; z=7.5 I am trying to use the below statement to test the equality of variables $y and $z as shown below Command: test $z -eq $y; echo $? But, it ...
user avatar
  • 207
0 votes
1 answer
2k views

Unix command to return maximum float value out of two after subtraction

This is similar to question about getting the minimum/maximum of two numbers, but I'm interested to do the same with float numbers. So basically I'm trying to use shell arithmetic expansion using the ...
user avatar
  • 18.1k
0 votes
1 answer
473 views

Is clock_getres the proper function for obtaining the resolution for Ubuntu 16.04 clock_gettime()?

Is clock_getres the proper function for obtaining the resolution for Ubuntu 16.04 clock_gettime()? If it is , how do I use the result to do this calculation? 64 bit integer has 15 base 10 digits of ...
user avatar
  • 641
0 votes
2 answers
777 views

Convert bash loop index to floating-point

I need to use the index i in a loop such as for i in {0..10..5} in a floating-point calculation. What is the simplest way to convert i to floating-point? Variations on i=$(bc -l <<< "scale=7;...
user avatar
  • 33
1 vote
1 answer
2k views

Wrong output in comparing floats

I'm trying to compare two floats in bash and something is going wrong. Here is the code sample based on solution here num1=0.502E-01 num2=0.01 echo $num1'>'$num2 | bc -l echo $num2'>'$num1 | bc ...
user avatar
4 votes
2 answers
1k views

Float operations with bc not precise?

When I'm performing float operations in shell using bc, the result is not the same if I use a regular calculator. Am I doing something wrong? For example, I need to find a volume of a sphere. User ...
user avatar
  • 43
0 votes
1 answer
922 views

If else statement involving two numbers in scientific notation [duplicate]

I have two numbers in scientific notation and I want to do some comparison between them in an if statement in bash. While doing so, I am getting error like bwlow: E.g: bash-4.2$ a=10e-12 bash-4.2$ b=...
user avatar
  • 45
1 vote
1 answer
268 views

How to express floating point expressions in bash scripts

Why does a2=100. echo 1.1*$a2|bc work on the terminal, but the 3 variants in the following script don't: a2=100. echo " 1.1*$a2|bc $(1.1*$a2|bc) $((1.1*$a2|bc)) ">x.sh
user avatar
  • 11
2 votes
2 answers
1k views

Why do newer versions of awk print large integers instead of decimals?

I often use the awk (gawk) calculator: calc(){ awk "BEGIN{ print $* }" ;} In Ubuntu 14.04, it output big numbers in standard form, e.g. $ calc 56777654409823*4674990588446667952594759939814064128 2....
user avatar
  • 3,311
1 vote
1 answer
4k views

Bash script how to accept single or double digit number and make it 2 digit

I have a following simple extract from my script which is strangely stuck up for a particular inputs. echo -e "Enter the Day of the Month : \c " read input_day printf -v input_day '%02d' "$...
user avatar
21 votes
3 answers
45k views

How to format floating point number with exactly 2 significant digits in bash?

I want to print the floating point number with exactly two significant digits in bash (maybe using a common tool like awk, bc, dc, perl etc.). Examples: 76543 should be printed as 76000 0.0076543 ...
user avatar
  • 313
8 votes
1 answer
34k views

Is it possible to get a decimal output from doing division in Bash? [duplicate]

Basically in Bash, what I'm trying to do is take a input of seconds from a user and find the hours. So basically if the user enter 35 the output should be 0.00972222. But bash gives me is a zero. ...
user avatar
1 vote
1 answer
4k views

comparing decimal point values in if statement

Today I was trying to compare two values and surprised what I saw. I didn't find any good suggestion anywhere. Could any one please help? if [ 10.94 -gt 10 ] then echo True else echo False fi I ...
user avatar
  • 429
2 votes
2 answers
2k views

Display integral in scientific notation?

I have an integral value: cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq 2400000 I assign it to a shell variable: CPU_FREQ=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq) I ...
user avatar
1 vote
1 answer
215 views

Unix awk decimal export in pipe

I'm struggling to adapt a script, which loops through a large number of individual files (subj_id), and prints out the value of $4 in a specified line. Text file contains lines # Measure Brain, ICV, ...
user avatar
  • 11
2 votes
3 answers
990 views

Formatting from Decimal to Exponential

I have a data file abc.txt in the following format: BALT 1 54.500 -161.070 3.95863757 0.01691576 BARM 2 -9.200 67.120 4.07529868 0.01951653 BKSR 3 43.830 142.520 4.08919819 0.00587340 I ...
user avatar
  • 25
8 votes
4 answers
6k views

Weird float rounding behavior with printf

I've read some answers on this site and found the printf rounding desirable. However when I used it in practice, a subtle bug led me to the following behavior: $ echo 197.5 | xargs printf '%.0f' 198 ...
user avatar
  • 185
5 votes
1 answer
1k views

How to compare float values in a shell script?

I need to alert if my load average is more than 10. I could do it like this if my awk command returned an integer: declare -i MAX_LOAD=10 declare -i L1=`cat /proc/loadavg | awk {'print $1'}` if [ $...
user avatar
  • 909