Arithmetic on a Float in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Arithmetic on a Float in bash
# 1  
Old 10-23-2015
Arithmetic on a Float in bash

I am using bash
I have a script that takes a number, i.e. 85.152, which is always a non integer and essentially tries to get that number to be a multiple of 10. My code is as follows:

Code:
number=85.152
A=${number%.*} #Converts float to integer
typeset -i B=$(((A-20)/10)) #subtracting 20 is desired, dividing by 10 is done to get the value less than 10 (only works for numbers less than 100)
typeset -i C=${B%.*} #converts float to integer (redundant I know, but necessary)
typeset -i answer=$((C * 10)) #Gives me a multiple of 10

Explanation:
1. I convert from a float to integer first because I cannot simply divide 85.152 by 10 using bash. It does not like dividing floats.
2. I subtract 20 because this is desired by design (it can be ignored). The division by 10 gets me to a number below 10 which is just a trick because any integer less than 10 which is multiplied by 10 will simply be a multiple of 10.
3. I convert from float to integer again just in case the number I divided, (to get less than 10), ends up dividing out to be another float (i.e. 85/10=8.5, then truncate to 8).
4. Finally I multiply by 10, 8*10=80 which is a multiple of 10.


I did what I could as a newbie to make it work but now I would like to simplify this process. I simply want to take 85.152 and make it a multiple of 10 that is 20 less than 85.152. So answer = (85.125-20) truncated to a multiple of 10. Should be 60. Any suggestions?
# 2  
Old 10-23-2015
Not sure what you mean. Something like this maybe?
Code:
answer=$(((${number%.*}-20)/10*10))

or
Code:
answer=$(((${number%.*}/10-2)*10))

# 3  
Old 10-23-2015
Code:
$ V=85.5
$ echo $(( ${V%.*}-(${V%.*}%10)-20 ))

60

$

# 4  
Old 10-24-2015
OSX 10.7.5, default bash terminal...
How about:-
Code:
Last login: Sat Oct 24 08:38:01 on ttys000
AMIGA:barrywalker~> x=87.1173
AMIGA:barrywalker~> y=20
AMIGA:barrywalker~> echo $[${x%.*}-${x%.*}%10-$y]
60
AMIGA:barrywalker~> _

# 5  
Old 10-24-2015
Code:
x=85.152
y=20
$ echo "(($x-$y)/10)*10"|bc 
60

# 6  
Old 10-24-2015
How about
Code:
number=85.152
echo $((${number%?.*}0 - 20 ))
60
number=895.152
echo $((${number%?.*}0 - 20 ))
870

This User Gave Thanks to RudiC For This Post:
# 7  
Old 10-24-2015
Hi RudiC...
Neat!
Contracting on your method, OSX 10.7.5, default bash terminal:-
Code:
Last login: Sat Oct 24 17:08:09 on ttys000
AMIGA:barrywalker~> number=87.11234
AMIGA:barrywalker~> echo $[${number%?.*}0-20]
60
AMIGA:barrywalker~> number=897.1357
AMIGA:barrywalker~> echo $[${number%?.*}0-20]
870
AMIGA:barrywalker~> _

EDIT:-
Important note both methods do not work with the number 80 but do if converted to 80.0 float equivalent.

Last edited by wisecracker; 10-24-2015 at 01:22 PM.. Reason: See above...
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