Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I read a billion of discussion about this but still have confused thoughts. I just need to use bash to calculate some parameters, and for this operation I need to get the integer part of a float operation. I tried many ways, but each time I get some errors. Here is the last one:

fkeypar "ex1.fef[1]" TSTARTI         #### fkeypar is an external command to get some values to assign to the subsequent variables
t0i="$(expr $(pget fkeypar value) - 11544)"                                          
fkeypar "ex1lc.fits[2]" TELAPSE
lengthini=`pget fkeypar value`
fkeypar "ex7lc.fits[2]" TSTOP
lengthfin=`pget fkeypar value`
fkeypar "ex1lc.fits[2]" TSTART      
ijd=`pget fkeypar value`

i=$(echo "scale=0; ($ijd - $t0i)/$period + 1" | bc)
ifin=$(echo "scale=0; ($lengthfin - $ijd)/$period + 1" | bc)

echo $i
for ((n=$i; n<=$ifin; n++))
do
    echo $n

where all the parameters in i and ifin are defined. This code is within a for cycle, and it returns me an error (many times):

./gti_user_tot: line 34: ((: n=: syntax error: operand expected (error token is "=")
bc: symbol lookup error: /science/heasoft-6.14/x86_64-unknown-linux-gnu-libc2.9/lib/libreadline.so.6: undefined symbol: PC
bc: symbol lookup error: /science/heasoft-6.14/x86_64-unknown-linux-gnu-libc2.9/lib/libreadline.so.6: undefined symbol: PC

./gti_user_tot: line 34: ((: n=: syntax error: operand expected (error token is "=")

The echo $i seems ignored, the for cycle over n has some problems. I could avoid bc but I can't find a stable way to workaround.

share|improve this question
    
Are you sure you're using bash to run your script? –  Greg Hewgill Jun 26 at 2:55
    
Yes XD #!/bin/bash –  Py-ser Jun 26 at 2:57
    
echo '1.1111 1.1111 +p' | dc | sed 's/\..*//' –  mikeserv Jun 26 at 2:59
    
@mikeserv, sorry what? –  Py-ser Jun 26 at 3:05
    
@Py-ser - did you try it? –  mikeserv Jun 26 at 3:44

1 Answer 1

For the operation (12.3-23.4)/34.5+1 this could be the first step.

A floating point subtraction as bash function:

    #!/bin/bash

    sub ()       # Arguments: minuend and subtrahend
    {
            m=$1
            s=$2
            mi=${m%.*}
            mf=${m#*.}
            si=${s%.*}
            sf=${s#*.}
            p='00000000'
            mfp=$mf${p:${#mf}}
            ml=$mi$mfp
            [[ $ml =~ ^0*(.*)$ ]] && ml=${BASH_REMATCH[1]}
            sfp=$sf${p:${#sf}}
            sl=$si$sfp
            [[ $sl =~ ^0*(.*)$ ]] && sl=${BASH_REMATCH[1]}
            rl=$(($ml-$sl))
            rlen=${#rl}; for((i=$rlen-1;i>=0;i--)); do r="$r${rl:$i:1}"; done
            r=${r:0:8}.${r:8:99}
            rlen=${#r}; for((i=$rlen-1;i>=0;i--)); do rx="$rx${r:$i:1}"; done
            [[ $rx =~ ^([0-9-]*.[0-9]*[1-9])0*$ ]] && result=${BASH_REMATCH[1]}
            echo $result
    }

    sub 12.3 23.4

Output:

-11.1

The next step (floating point division) could be an extension of my implementation (integer divison with floating point result) to calculate -11.1/34.5.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.