I want to calculate the difference between two negative floating numbers and subsequently check if the result is below a certain threshold. The numbers are stored in the file.dat, since this calculation will be done many times the calculation has to rely on getting the values directly from a file without manual formatting. The file.dat looks for example like this.
-.63354975E+03 -.63354982E+03
Now since bash can't handle floats I've tried bc in the following way:
paste -sd- file.dat | bc >> file.dat
The paste command turns the input into
-.63354975E+03--.63354982E+03
bc should subract them, however I get the error message (standard_in) 1: syntax error. I could use sed to change one number to positive and then add them, but is there a way to do it directly with bc?
A different problem occurs when I use paste -sd+ file.dat | bc >> file.dat. This should add the numbers, the result is however 4.932900412, way off the expected 1267.09957. When I type in the calculation with the numbers formatted as -633.54975+-633.54982 I get the expected result. So it seems bc does not process numbers with exponents properly. Is there a way to resolve this?
paste -sd-bypr -T -S' - ' -2(prallows string delimiters, whereas AFAIKpasteonly allows a single character)file.datboth as input argument and for output redirection.sedto modify the exponent, then you may as well use it for inserting the operator as well e.g.sed -e '$!N;s/\n/ - /' -e '...'