BC


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BC
# 1  
Old 12-06-2011
BC

Spent ages on this and given up.....any bright ideas?

I would like an expression that if input -3 to -24, will give -27 to -48
and if input is -27 to -48, will give -3 to -24
i.e......
INPUT OUTPUT
-3 -27
-4 -28
-24 -48
-27 -3
-28 -4
-48 -24

Many thanks in advance
# 2  
Old 12-06-2011
Can you explain it again?

--ahamed
# 3  
Old 12-06-2011
Quote:
I would like an expression ...
If you are looking for program code, please mention what Programming Language or Shell. A sample goes a long way.

My interpretation of the question.
The table headed "INPUT OUTPUT" is a look-up table of some sort (not clear whether it is a file). Not clear where the input data comes from.
Using this table you can determine that inputting "-3" should provide the output "-27.
# 4  
Old 12-06-2011
This looks more like a mathematical puzzle.
Gareth, are you looking for a bc expression that subtracts 24 from your input value and returns the remainder of the division by -48 unless the remainder would be 0 in which case you'd like to return -48?
Such an expression could look like this:
Code:
( ( $usr_input - 24 ) % -48 ) - ( 48 * !( ( $usr_input - 24 ) % -48 ) )

This User Gave Thanks to cero For This Post:
# 5  
Old 12-06-2011
Building on the above brilliant ideas, I think I have got there:

Code:
for usr_input in -3 -4 -24  -27 -28 -48
do
       echo "((( $usr_input -23 ) % -48 ) -1 )"|bc
done

-27
-28
-48
-3
-4
-24

This User Gave Thanks to methyl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question