Bit of a math question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bit of a math question
# 8  
Old 09-05-2014
Sorry for the delay, I went to the store and came back to find allot of very nice posts. After thinking about it, if the first number happens to end in *99,

FIRST_NUMBER='199'

then I would want 299 for my second number and not a repeat of 199.

So I guess this code will do what I need,

SECOND_NUMBER=$((((FIRST_NUMBER+1)/100+1)*100-1))

I plugged this into my script and I am getting the behavior I expect.

Is there some reason why my thought of just replacing the last two chars is ill conceived? The code I posted didn't work and I was getting a substring expression < 0 error.

I guess what I would have done here would have been to test $FIRST_NUMBER,
Code:
if [ "$FIRST_NUMBER" == "$SECOND_NUMBER" ]; then
   let "SECOND_NUMBER=$FIRST_NUMBER+100"
fi

to make sure I didn't end up in the same place, but it is nice to do this in one step.

LMHmedchem
# 9  
Old 09-05-2014
The negative value is only possible in bash 4. Probably your bash is too old..
Code:
bash3$ num=1478; echo ${num:0:-2}99
-bash: -2: substring expression < 0

But even with bash 4 it would be a problem if FIRST_NUMBER is a single digit..
Code:
bash4-4.2$ num=1478; echo ${num:0:-2}99
1499
bash4-4.2$ num=147; echo ${num:0:-2}99
199
bash4-4.2$ num=14; echo ${num:0:-2}99
99
bash4-4.2$ num=1; echo ${num:0:-2}99
bash4: -2: substring expression < 0

Of course the value would still need to be corrected by one..

Last edited by Scrutinizer; 09-05-2014 at 06:13 PM..
# 10  
Old 09-05-2014
Hi Scrutinizer...

Then pad it with a space...
Code:
Last login: Fri Sep  5 22:52:07 on ttys000
AMIGA:barrywalker~> x=" 9"
AMIGA:barrywalker~> y=99
AMIGA:barrywalker~> z=${x:0:$((${#x}-2))}$y
AMIGA:barrywalker~> echo $z
99
AMIGA:barrywalker~> x=" 29"
AMIGA:barrywalker~> y=99
AMIGA:barrywalker~> z=${x:0:$((${#x}-2))}$y
AMIGA:barrywalker~> echo $z
99
AMIGA:barrywalker~> x=" 219"
AMIGA:barrywalker~> z=${x:0:$((${#x}-2))}$y
AMIGA:barrywalker~> y=99
AMIGA:barrywalker~> echo $z
299
AMIGA:barrywalker~> x="-9"
AMIGA:barrywalker~> y=99
AMIGA:barrywalker~> z=${x:0:$((${#x}-2))}$y
AMIGA:barrywalker~> echo $z
99
AMIGA:barrywalker~> _

# 11  
Old 09-05-2014
All of the responses so far are assuming that the OLD_NUMBER is non-negative. If OLD_NUMBER can be less than -99, it is a little more complicated, but I think this works:
Code:
NEW_NUMBER=$((((OLD_NUMBER<=-100)*(OLD_NUMBER/100*100+1))+((OLD_NUMBER>-100)*((((OLD_NUMBER+1)/100)+1)*100-1))))

When put into a loop like this:
Code:
for OLD_NUMBER in "$@"
do
	NEW_NUMBER=$((((OLD_NUMBER<=-100)*(OLD_NUMBER/100*100+1))+((OLD_NUMBER>-100)*((((OLD_NUMBER+1)/100)+1)*100-1))))
	printf '%s -> %d\n' "$OLD_NUMBER" "$NEW_NUMBER"
done

and invoked as:
Code:
next99 1298 1299 1300 -1298 -1299 -1300 -99 99 0 -100

with either bash or ksh, it produces:
Code:
1298 -> 1299
1299 -> 1399
1300 -> 1399
-1298 -> -1199
-1299 -> -1199
-1300 -> -1299
-99 -> 99
99 -> 199
0 -> 99
-100 -> -99

# 12  
Old 09-06-2014
For non-negative numbers, try
Code:
echo $((NUM+100-(NUM+1)%100))

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

Which version of Windows Vista to install with a product key? 32-bit or 64-bit?

Hello everyone. I bought a dell laptop (XPS M1330) online which came without a hard drive. There is a Windows Vista Ultimate OEMAct sticker with product key at the bottom case. I checked dell website (here) for this model and it says this model supports both 32 and 64-bit version of Windows... (4 Replies)
Discussion started by: milhan
4 Replies

2. Shell Programming and Scripting

Shell script for solving the math question

Can such Puzzle solve through UNIX script? if yes, what could be the code? This has been solve in C language. we were trying to solve this through shell but could not because of not able to pass 1st argument with multiple value. we are not expert in unix scripting. Below is the puzzle John is a... (4 Replies)
Discussion started by: anshu ranjan
4 Replies

3. Shell Programming and Scripting

How to handle 64 bit arithmetic operation at 32 bit compiled perl interpreter?H

Hi, Here is the issue. From the program snippet I have Base: 0x1800000000, Size: 0x3FFE7FFFFFFFF which are of 40 and 56 bits. SO I used use bignum to do the math but summing them up I always failed having correct result. perl interpreter info, perl, v5.8.8 built for... (0 Replies)
Discussion started by: rrd1986
0 Replies

4. Solaris

immutable bit question

Hi! All Just wondering if anyone has a idea about setting the immutable bit on a Solaris 10 ZFS file I tried this chmod S+ci toto.txt and got that :-( chmod: ERROR: invalid mode (0 Replies)
Discussion started by: Ex-Capsa
0 Replies

5. UNIX for Dummies Questions & Answers

Question regarding permision and seguid bit (sticky bit)

Hi , I am having file permision as drwxrwsr_x I kwo for deleting a file in the diretory i need w permsion as well .. Say if i am having the permsion as drwxrwsrwx - wil any one can delete the files in the directory .. And one more question what is the s doing there ..... (2 Replies)
Discussion started by: arunkumar_mca
2 Replies

6. UNIX for Dummies Questions & Answers

Simple Math question...

Hi All , I'm trying to do a simple math expression ...but unsuccessfull :-( Anyone can help me? days=23 amount=`expr ${days} / 30 \* -125` echo $amount but as result i got 0 when i expect 95.833333 Another question...how i can limit only to two or three decimal fields? Thanks in... (1 Reply)
Discussion started by: EDBGSK
1 Replies

7. Programming

copying or concatinating string from 1st bit, leaving 0th bit

Hello, If i have 2 strings str1 and str2, i would like to copy/concatenate str2 to str1, from 1st bit leaving the 0th bit. How do i do it? (2 Replies)
Discussion started by: jazz
2 Replies

8. Shell Programming and Scripting

a math related question

hello: First I know the rules on Homework related questions, I wrote my script, but I cannot seem to figure out how to do one math problem. How do I take a zip code and seperate the idvidual digits? I used the modulus expression and divided the number by 10 ^ n but that only worked... (9 Replies)
Discussion started by: jahjah
9 Replies
Login or Register to Ask a Question