User Tools

Site Tools


calculating

If you want to send us your comments, please do so. Thanks
More on comments


Calculation and numbers

Calculating with bc

Quotes

There are different double quotes. Beware the you use the right ones This does not work due to the wrong type of quotes

$ echo57/43.1234567| bc
(standard_in) 1: illegal character: \342
(standard_in) 1: illegal character: \200
(standard_in) 1: illegal character: \234
(standard_in) 1: illegal character: \342
(standard_in) 1: illegal character: \200
(standard_in) 1: illegal character: \235

The second quote double, one wrong

$ echo "57/43.1234567"| bc
(standard_in) 1: illegal character: \342
(standard_in) 1: illegal character: \200
(standard_in) 1: illegal character: \235

The second quote wrong

$ echo "57/43.1234567” | bc
>

An other example with wrong quotes

$ echoscale=2;57/43.1234567| bcscale=2
bash: 57/43.1234567”: No such file or directory

Single and double quotes can be used
With the right quotes

$ echo "57/43.1234567" | bc
1
$ echo '57/43.1234567' | bc
1

Floating point calculations

With echo

echo "scale=2;57/43.1234567" | bc		# 1.32
echo 'scale=2;57/43.1234567' | bc		# 1.32

scale only works with divide, not with multiplication

echo 'scale=2;57*43.1234567' | bc		# 2458.0370319	( should be 2458.04 )

This can be resolved, apart from the correct rounding, can be acieved by deviding the result by 1

echo 'scale=2;57*43.1234567/1' | bc		# 2458.03

Without echo

bc <<< 'scale=4;1216.00*31.1034768'		# 37821.8277888
bc <<< 'scale=4;1216.00*31.1034768/1'		# 37821.8277
bc <<< '365 * 24 * 60 * 60'			# 31536000
bc <<< $((4+5))*5                               # 45

Correct rounding with scale as a variable
It might be safe to give the initial variable scale a different name

scale=3;echo "scale=$scale;(((10^$scale)*160*24.90385449058/1)+0.5)/(10^$scale)" | bc	# 3984.617
scale=3;bc <<< "scale=$scale;(((10^$scale)*160*24.90385449058/1)+0.5)/(10^$scale)"	# 3984.617

Number ranges

echo $(seq 10)1 2 3 4 5 6 7 8 9 10

Pi

a(x)The arctangent of x, arctangent returns radians
pi=$(echo "scale=10; 4*a(1)" | bc --mathlib)
bc <<< 15*$pi
# Result: 47.1238897980
 
bc <<< '4*a(1)'
# Result: 3.14159265358979323844

Main subjects on this wiki: Linux, Debian, HTML, Microcontrollers, Privacy

RSS
Disclaimer
Privacy statement
Bugs statement
Cookies
Copyright © : 2014 - 2024 Webevaluation.nl and the authors
Changes reserved.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
calculating.txt · Last modified: 25-12-2022 19:53 by wim