[comp.unix.questions] Floating-Point Arithmetic in a Csh Script

jjr@ut-emx.UUCP (Jeff Rodriguez) (04/17/88)

I am trying to do floating-point arithmetic in a csh script for 4.3 BSD.
Since "@ x = 2.3 + 4.5" won't work, I'm using bc.
But I can't find a nice way to get bc's output into a shell variable
without using a temporary file.

This method does not work:
     #!/bin/csh
     set x = `bc << END`
     scale = 5
     $1 + $2
     END

This is the best that I've come up with:
     #!/bin/csh
     # This csh script computes x = $1 + $2,
     # where $1 and $2 are floating-point numbers.
     bc << END > temp
     scale = 5
     $1 + $2
     END
     set x = `cat temp`

Can anybody think of a better way?

			Jeff Rodriguez
			jjr@emx.utexas.edu

koblas@mips.COM (David Koblas) (04/18/88)

In article <1748@ut-emx.UUCP> you write:
>I am trying to do floating-point arithmetic in a csh script for 4.3 BSD.
>Since "@ x = 2.3 + 4.5" won't work, I'm using bc.
>But I can't find a nice way to get bc's output into a shell variable
>without using a temporary file.
>
>This method does not work:
>     #!/bin/csh
>     set x = `bc << END`
>     scale = 5
>     $1 + $2
>     END
>

You were very close, what you needed to do was something more like this:
	#!/bin/csh
	set x = `bc << END  	\
	scale = 5  		\
	$1 + $2  		\
	END`
	echo $x

-- 
name : David Koblas         place: MIPS Computers Systems                
phone: 408-991-0287         uucp : {ames,decwrl,pyramid,prls}!mips!koblas 

jjr@ut-emx.UUCP (Jeff Rodriguez) (04/19/88)

Apparently, my article did not get sent from my host,
so I'm resubmitting it.

I am trying to do floating-point arithmetic in a csh script for 4.3 BSD.
Since "@ x = 2.3 + 4.5" won't work, I'm using bc.
But I can't find a nice way to get bc's output into a shell variable
without using a temporary file.

This method does not work:
     #!/bin/csh
     set x = `bc << END`
     scale = 5
     $1 + $2
     END

This is the best that I've come up with:
     #!/bin/csh
     # This csh script computes x = $1 + $2,
     # where $1 and $2 are floating-point numbers.
     bc << END > temp
     scale = 5
     $1 + $2
     END
     set x = `cat temp`

Can anybody think of a better way?

			Jeff Rodriguez
			jjr@emx.utexas.edu

richl@penguin.USS.TEK.COM (Rick Lindsley) (04/26/88)

In article <1748@ut-emx.UUCP> jjr@ut-emx.UUCP (Jeff Rodriguez) writes:
    
    This is the best that I've come up with:
         #!/bin/csh
         # This csh script computes x = $1 + $2,
         # where $1 and $2 are floating-point numbers.
         bc << END > temp
         scale = 5
         $1 + $2
         END
         set x = `cat temp`
    
    Can anybody think of a better way?

How about:

    set x = `echo "scale=5; $1 + $2" | bc`

Same effect, less space.

Rick