Questions tagged [floating-point]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0 votes
0 answers
21 views

Make bc command display 0.5 instead of .5 [duplicate]

When I expect to get a decimal such as 0.2 or 0.4, the bc command displays it as .2 and .4. Can the bc command display them as 0.2 and 0.4?
horsey_guy's user avatar
0 votes
0 answers
59 views

Rounding 2.555500000000000104805053 with printf -> 2.555 or 2.556? [duplicate]

I tried printf "%.3f\n" 2.555500000000000104805053 printf "%.3f\n" 2.555500000000000104805054 the result was 2.555 2.556 same with printf "%.3f\n" 3....
R 9000's user avatar
  • 167
1 vote
1 answer
175 views

Completed stumped by trying to use a decimal value in an array for Bash

Here is some simple test code. #!bin/bash cpm=(0 1 0.094) lv=1 attack=5 defense=9 stamina=16 echo $((cpm[lv])) mycpm=$((cpm[lv])) #mycpm=`echo "0.094" | bc -l` cq=`echo "$attack*$...
Tim50001's user avatar
2 votes
3 answers
958 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 ...
William's user avatar
  • 43
0 votes
3 answers
2k 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 ...
willowen100's user avatar
1 vote
1 answer
1k 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 ...
codehunter-py's user avatar
0 votes
1 answer
80 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 ...
RAJ's user avatar
  • 1
0 votes
4 answers
438 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 ...
Raja's user avatar
  • 133
0 votes
2 answers
721 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/...
Jim's user avatar
  • 1,391
0 votes
1 answer
174 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 ...
PCRevolt's user avatar
0 votes
0 answers
128 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 ...
tokariew's user avatar
2 votes
1 answer
312 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/...
yong shi's user avatar
1 vote
2 answers
474 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 ...
Margo Oka's user avatar
  • 341
4 votes
1 answer
6k 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", ...
ptrcao's user avatar
  • 5,575
0 votes
1 answer
825 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 ...
Richard Christopher's user avatar
0 votes
3 answers
50 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 ...
SSR's user avatar
  • 25
2 votes
1 answer
197 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....
Jannis Erhard's user avatar
33 votes
2 answers
8k 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 ...
gboffi's user avatar
  • 1,362
7 votes
1 answer
4k 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 ...
FelixJN's user avatar
  • 13.5k
0 votes
0 answers
171 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" ' {...
xrkr's user avatar
  • 269
1 vote
4 answers
9k 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 ...
pfnuesel's user avatar
  • 5,827
2 votes
1 answer
189 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 ...
Zeno of Elea's user avatar
5 votes
1 answer
9k 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,...
Alex Martian's user avatar
  • 1,005
2 votes
3 answers
6k 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 ...
user1400953's 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; ...
cat's user avatar
  • 3,458
6 votes
2 answers
1k 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 ...
Stéphane Chazelas's user avatar
0 votes
1 answer
2k 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 ...
pandora's user avatar
13 votes
2 answers
21k 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?
user267288's user avatar
0 votes
3 answers
997 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 "^...
O'Niel's user avatar
  • 157
1 vote
1 answer
1k 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 < $...
Marco's user avatar
  • 418
3 votes
3 answers
3k 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 ...
Sriram P's user avatar
1 vote
3 answers
1k 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); ...
user221083's user avatar
2 votes
1 answer
1k 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 ...
Akshata's user avatar
  • 21
1 vote
4 answers
7k 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 $...
SecureTech's user avatar
  • 1,389
2 votes
2 answers
5k 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 ...
John's user avatar
  • 23
5 votes
1 answer
1k 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 ...
AngiSen's 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 ...
kenorb's user avatar
  • 20.9k
0 votes
1 answer
540 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 ...
Frank's user avatar
  • 671
0 votes
2 answers
897 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;...
Friasco's user avatar
  • 33
1 vote
1 answer
3k 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 ...
rambalachandran's user avatar
4 votes
2 answers
2k 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 ...
Slado's user avatar
  • 43
0 votes
1 answer
1k 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=...
Pratap's user avatar
  • 45
1 vote
1 answer
294 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
FRB's user avatar
  • 11
2 votes
2 answers
2k 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....
EmmaV's user avatar
  • 4,057
1 vote
1 answer
5k 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' "$...
Sachin H's user avatar
  • 111
24 votes
3 answers
50k 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 ...
tafit3's user avatar
  • 343
9 votes
1 answer
39k 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. ...
shawn edward's user avatar
1 vote
1 answer
6k 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 ...
PriB's user avatar
  • 479
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
267 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, ...
julia's user avatar
  • 11