1

I'm currently toying around with making scripts using tcsh and Ive noticed: If you do division it only outputs as many significant figures as the arguments going in

i.e. ( the numbers are the entered arguments):

2/4 outputs 0
4/2 outputs 2
22/2 outputs 11
1/8 outputs 0
9/2 outputs 4

So how does one alter this so 1/8= 0.125, 2/4 = 0.5, 9/2 = 4.5 etc??

admdrew
  • 3,790
  • 4
  • 27
  • 39
  • GNU/Linux shells can only perform integer division. There are other options, however: http://unix.stackexchange.com/questions/40786/how-to-do-integer-float-calculations-in-bash-or-other-languages-frameworks – admdrew Oct 08 '14 at 17:49
  • possible duplicate of [how can I get a float division in bash?](http://stackoverflow.com/questions/12722095/how-can-i-get-a-float-division-in-bash) – admdrew Oct 08 '14 at 17:50

2 Answers2

0

Use the arbitrary precision calculator bc and set scale to the precision you want:

 $ bc -e 'scale=10;1/8;4/5; scale=30; 1/31'
 .1250000000
 .8000000000
 .032258064516129032258064516129
Jens
  • 69,818
  • 15
  • 125
  • 179
-1

You have to provide the memory space that you need by requesting the DATA TYPE for your variable. If you set your answer to a variable of type char, there will be only space for one character. If you set your variable type to float, there will be memory prepared for a decimal value.