Questions tagged [floating-point]

The tag has no usage guidance.

5
votes
1answer
165 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 ...
0
votes
0answers
45 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" ' {...
0
votes
0answers
21 views

Use objdump to show data as floating point

I have a ELF executable a.out that uses some floating point constants. After some investigation I found the address of these constants and then I do, $ objdump -s a.out | grep 4a9940 which gives me ...
1
vote
4answers
567 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 ...
2
votes
1answer
51 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 ...
1
vote
1answer
2k 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,...
1
vote
3answers
1k 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 ...
2
votes
1answer
330 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; ...
3
votes
3answers
315 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 ...
0
votes
1answer
149 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 ...
7
votes
2answers
3k 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?
0
votes
3answers
320 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 "^...
1
vote
1answer
482 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 < $...
3
votes
3answers
1k 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 ...
1
vote
3answers
168 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); ...
2
votes
1answer
158 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 ...
1
vote
4answers
3k 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 $...
2
votes
2answers
2k 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 ...
5
votes
1answer
608 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, ...
0
votes
1answer
616 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 ...
0
votes
1answer
288 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 ...
0
votes
2answers
330 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;...
1
vote
1answer
1k 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 ...
4
votes
2answers
712 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 ...
0
votes
1answer
447 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=...
1
vote
1answer
203 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
2
votes
2answers
380 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....
1
vote
1answer
1k 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' "$...
14
votes
3answers
27k 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 ...
2
votes
1answer
10k 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. ...
1
vote
1answer
1k 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 ...
2
votes
2answers
837 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 ...
1
vote
1answer
148 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, ...
1
vote
3answers
696 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 ...
7
votes
4answers
4k 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 ...
4
votes
1answer
690 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 [ $...
1
vote
1answer
796 views

How to do float operation in Shell script? [duplicate]

time_value=$(($large / 1000)) $large could be 60 or 57. I'm expecting 57/1000=0.057. But I'm getting 0. So, is there any way to do this?
3
votes
4answers
703 views

Convert floating point numbers to decimal numbers

I have several 'ascii' tables in a directory, some of them have numbers expressed in decimal and some others ones in floating point, as follow: 1 1 1423 1 2 1589 1 3 0.85e 5 1 4 0.89e 4 1 5 8796 ... ...
11
votes
4answers
9k views

bash loop with 0.02 increment

I want to make a for loop in bash with 0.02 as increments I tried for ((i=4.00;i<5.42;i+=0.02)) do commands done but it didn't work.
0
votes
1answer
56 views

sum over floating points of the form 3.016e-06 using awk

I want to sum over floats of the form: 3.016e-06 The floats are saved in a files called S_3x3.txt S_4x4.txt ... S_8x8.txt My code sums only the first integer. Instead of 3.016e-06 + 4.022e-06 it ...
1
vote
2answers
1k views

Copy Single Value from a file to a variable and perform arithmetic over it? [duplicate]

Suppose we have a file like 'abc.txt' which has a single value 123456 and file 'xyz.txt' which also has a single value like 654321. I want to store these values form the file to some shell variable ...
3
votes
1answer
284 views

Approximating a floating point number with correct rounding off

How can I approximate a floating point number using shell-script (say, up to 4 digits after the decimal point)? I have two examples: A=1.2345678 and B=1.2345678E-05. So I would like to get A=1....
2
votes
2answers
121 views

Add a number in a huge ASCII file

Background: (1) Here is a screen capture of a part of my ascii file (over 600Mb): (1.1) Here is a part of the code: 0, 0, 0, 0, 0, 0, 0, 0, 3.043678e-05, 3.661498e-05, 2.070347e-05, 2.47175e-...
3
votes
3answers
669 views

Shell script to move files if certain percentage of lines contain a certain string

I am working on a shell script to move files if certain percentage of the lines contain a certain string. I've got ~2000 files in a directory that each contain a single column of data. The number of ...
1
vote
1answer
157 views

How to force decimal addition (#10) when adding numbers with a carry (octal issue)

How can I get 10#1$result to work correctly? When creating a calculator for adding two numbers I am adding together by transversing the digits from small to large. e.g. for adding 51 plus 51 I am ...
1
vote
1answer
799 views

How do you derive a decimal from a math operation involving a variable?

I'm trying to find derive SCALEFACTOR which is basically 10000/(sum of 4th column in a file). How do I get a decimal from the output? Appreciate any help in advance. #!/bin/bash FILES=/path/to/files/...
0
votes
1answer
1k views

integer number division to decimal number in bash script [duplicate]

in bash script i have to change the value of $R , so i have to division to decimal number but it's impossible R=$((R/(15.6))) #for convert move in millimeter to second i try to R=$((R/(156/10))) #...
3
votes
1answer
3k views

Simple floating point benchmark that can be compiled from source?

I am interested in benchmarking some ARM platforms; can anyone suggest a simple benchmark with source code that can be used to benchmark floating point performance? A package such as a .deb file ...
6
votes
2answers
6k views

How do determine whether Linux board is using hardware FPU or not?

I am using an embedded Linux board based on a fairly old Freescale ARM chip. How do I determine whether the Python interpreter there is using the hardware FPU on the chip or not? My /proc/cpuinfo is:...
1
vote
1answer
8k views

How can subtract 2 floats which have been extracted from 2 other files with BASH [duplicate]

I'm trying to get the difference between two numbers which I have taken from two files. I think my code will make sense: I've tried to make it work by two different methods, didn't work. What I get ...