Arithmetic on a Float in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Arithmetic on a Float in bash
# 8  
Old 10-24-2015
Note: the $[ ... ] is a non-standard, bash-specific legacy construct that has been deprecated since bash 2.0 and is now no longer in the man pages, so it should be regarded as an undocumented feature, which could be removed from future versions..

Instead, $(( .. )) should be used, which is also used in any other POSIX compliant shell.
# 9  
Old 10-24-2015
Quote:
Originally Posted by wisecracker
.
.
.
Important note both methods do not work with the number 80 but do if converted to 80.0 float equivalent.
As this little trick doesn't really do arithmetics, it needs the dot to lock in on.
# 10  
Old 10-24-2015
Quote:
Originally Posted by Scrutinizer
Note: the $[ ... ] is a non-standard, bash-specific legacy construct that has been deprecated since bash 2.0 and is now no longer in the man pages, so it should be regarded as an undocumented feature, which could be removed from future versions..

Instead, $(( .. )) should be used, which is also used in any other POSIX compliant shell.
Thanks...

I was/am aware, but the OP is using bash.
Sadly, I use(d) this method in AudioScope.sh from day one so that code is lumbered with this legacy.
However apart from my important note I did like RudiC's approach so I decided to reduce the number of characters, (ignoring spaces).
I will refrain from using this method again but will have to continue using it in the above project to keep the coding style the same...

Once again thanks and I will use the parentheses method in any future help...
# 11  
Old 10-24-2015
Save another char?echo $((${number%?.*}-2))0
This User Gave Thanks to RudiC For This Post:
# 12  
Old 10-24-2015
Quote:
Originally Posted by RudiC
Save another char?echo $((${number%?.*}-2))0
Terrific stuff, that is cool...

I just love your lateral thinking head...

Dare I use square brackets and reduce it by another two characters?
Only joking... ;oD
# 13  
Old 10-24-2015
Quote:
Originally Posted by RudiC
Save another char?echo $((${number%?.*}-2))0
Nice, a tiny nitpick would be that number=28.152 results in 00...
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Arithmetic with bash

I need to divide the number of white spaces by total number of characters in a file using bash. I am able to get the number of white spaces correctly using: tr -cd < afile | wc -c I am also able to get the total number of characters using: wc -c afile How do I divide the first... (2 Replies)
Discussion started by: ngabrani
2 Replies

2. Shell Programming and Scripting

Bash arithmetic issue

I found the following issue by simply increasing a variable. The ((A++)) expression returns an error, the other expressions both return 0. Does anyone know why? script.sh: #! /bin/bash A=0 B=0 C=0 ((A++)) ; echo "${?}" ((B=B+1)) ; echo "${?}" ((C+=1))... (8 Replies)
Discussion started by: elbrand
8 Replies

3. Shell Programming and Scripting

Arithmetic calculations in bash file

I have 2 numbers xmin = 0.369000018 xmax = 0.569000006 and want to calculate (xmax- xmin) / 5.0 I have tried using $(( )) but is always giving an error (8 Replies)
Discussion started by: kristinu
8 Replies

4. Red Hat

What is float IP?

:confused:We have two servers one active and one stand by as follows Active 202.61.9.9 Stand by 202.61.9.10 Float IP 202.61.9.8 What is use of this float IP? How it is configured? (1 Reply)
Discussion started by: manalisharmabe
1 Replies

5. Shell Programming and Scripting

Random float numbers in BASH

Hi people :) I'm learning shell scripting using bash and I want to generate 4 floating point number with 5 decimal places and write them to a file and a variable. I've done all this except the $RAMDOM enviroment variable does not generate a float number but a integrer. I hope you could... (3 Replies)
Discussion started by: pharaoh
3 Replies

6. Shell Programming and Scripting

float to normal

I have a file with 2 columns . One of the line looks like the following. Is it possible to convert every float no in column 2 to integer. input NM_032881 6.03787973608527e-05 output 0.0000603787..... (2 Replies)
Discussion started by: quincyjones
2 Replies

7. Shell Programming and Scripting

Arithmetic operations in bash,ksh,sh

Guys, The below expression is valid in which shells (sh,ksh,bash,csh)? VAR1=2 VAR2=$(($VAR1 -2)) Thanks (1 Reply)
Discussion started by: rprajendran
1 Replies

8. Shell Programming and Scripting

Need help is manipulating a file with some arithmetic operations using bash script

Friends, I have a file with contents like: interface Serial0/4/0/0/1/1/1/1:0 encapsulation mfr multilink group 101 Now I need to manipulate the file in such a way that to all the numbers less than 163, 63 gets added and to all numbers greater than 163, 63 gets deducted.(The numbers... (2 Replies)
Discussion started by: shrijith1
2 Replies

9. Shell Programming and Scripting

float input

how to input float data type in bash shell programming in linux? I am new to it so unaware to use the commands plz help me out. thank you. (6 Replies)
Discussion started by: purva
6 Replies

10. Programming

math.h: float ceilf(float x)

Good morning, I'm testing the use of ceilf: /*Filename: str.c*/ #include <stdio.h> #include <math.h> int main (void) { float ceilf(float x); int dev=3, result=0; float tmp = 3.444f; printf("Result: %f\n",ceilf(tmp)); return 0; } (1 Reply)
Discussion started by: jonas.gabriel
1 Replies
Login or Register to Ask a Question