bc

How to get a decimal number when dividing in bc?

吃可爱长大的小学妹 提交于 2019-12-04 22:17:56
I need 'bc' to divide a number and give me not only the floor but also the remainder. For instance 'bc' gives me '2' if I do '5/2'. I'd really want something like '2.5' Maybe this isn't even possible? scale = 20 It sets the number of decimal places. $ bc bc 1.06 Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 355/113 3 scale=20 355/113 3.14159292035398230088 quit $ 来源: https://stackoverflow.com/questions/10324157/how-to-get-a-decimal-number-when-dividing-in-bc

bc is ignoring scale option

时光怂恿深爱的人放手 提交于 2019-12-03 11:00:45
问题 I can't figure out why bc tool sometimes ignores the scale option. Here is an example: > echo 'scale=2; 2.777 - 1.4744' | bc 1.3026 Expected result is: 1.30 Additional information: > bash --version GNU bash, version 2.05b.0(1)-release (x86_64-suse-linux) Copyright (C) 2002 Free Software Foundation, Inc. > bc --version bc 1.06 Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc. 回答1: as Carl pointed out, if you check man page, you can find that line. it is about expression

bc and its ibase/obase options:

瘦欲@ 提交于 2019-12-03 07:19:00
问题 I stumbled over a curious bug, I think: I tried to read "512" as a number to base 6, and output it as base 16: echo "ibase=6;obase=16;512" | bc 161 As you can see, the output is 161, but it should be bc (sic!). I tried with base 10: echo "ibase=6;obase=10;512" | bc 512 The value is unchanged. Curious! Default obase is 10. If I omit it: echo "ibase=6;512" | bc 188 Well, that seems right. In a two step process, it works: echo "obase=16;"$(echo "ibase=6;512" | bc) | bc BC So I made a script for

bc and its ibase/obase options:

本小妞迷上赌 提交于 2019-12-02 19:50:57
I stumbled over a curious bug, I think: I tried to read "512" as a number to base 6, and output it as base 16: echo "ibase=6;obase=16;512" | bc 161 As you can see, the output is 161, but it should be bc (sic!). I tried with base 10: echo "ibase=6;obase=10;512" | bc 512 The value is unchanged. Curious! Default obase is 10. If I omit it: echo "ibase=6;512" | bc 188 Well, that seems right. In a two step process, it works: echo "obase=16;"$(echo "ibase=6;512" | bc) | bc BC So I made a script for different bases, but it keeps me puzzled: for ib in {6,8,10,16}; do echo $ib; for ob in {10,16}; do

Rounding Numbers with bc in Bash

不问归期 提交于 2019-11-30 20:17:21
I want to compute an average with 3 decimal figures, rounded to nearest, using bc . For example: average of 3, 3 and 5 should yield 3.667 and average of 3, 3 and 4 should yield 3.333 I tried: echo "scale=3; $sum/$n+0.0005" | bc but scale doesn't behave as I expect. What can I do to solve my problem? Your trick to add 0.0005 is not a bad idea. Though, it doesn't quite work that way. scale is used internally when bc performs some operations (like divisions). In your case, it would be better to perform the division first, maybe using a large scale or the -l switch to bc 1 (if your version

How do I calculate the log of a number using bc?

半城伤御伤魂 提交于 2019-11-30 12:25:23
问题 This is the first time I am using bc. I want to calculate the log (base 10) of a number. How do I this? 回答1: Invoke bc with the -l option (to enable the math library) like so $ echo 'l(100)/l(10)' | bc -l 2.00000000000000000000 Use the l function which is the natural log. Take the log of the number you are interested in then divide by the natural log of 10. 回答2: the logarithm of x in respect to base b can be computed given any logarithm function to an arbitrary base k -- that's actually

bash, bc modulo does not work with -l flag

ぃ、小莉子 提交于 2019-11-30 04:25:19
问题 So I am trying to use bc to calculate some logarithms but I also need to use it to calculate the modulus for something. Whilst making my script, I launched bc to test it. Without any flags, bc <<< "3%5" of course returns 3 . But with bc -l (loads math library so I can compute logarithms) any calculation of a%b returns 0 where a and b can be any number but 0 . What's happening? 回答1: That's because, from the manual: expr % expr The result of the expression is the "remainder" and it is com‐

Rounding Numbers with bc in Bash

余生颓废 提交于 2019-11-30 03:13:30
问题 I want to compute an average with 3 decimal figures, rounded to nearest, using bc . For example: average of 3, 3 and 5 should yield 3.667 and average of 3, 3 and 4 should yield 3.333 I tried: echo "scale=3; $sum/$n+0.0005" | bc but scale doesn't behave as I expect. What can I do to solve my problem? 回答1: Your trick to add 0.0005 is not a bad idea. Though, it doesn't quite work that way. scale is used internally when bc performs some operations (like divisions). In your case, it would be

How do I calculate the log of a number using bc?

烈酒焚心 提交于 2019-11-30 03:07:31
This is the first time I am using bc. I want to calculate the log (base 10) of a number. How do I this? Ray Toal Invoke bc with the -l option (to enable the math library) like so $ echo 'l(100)/l(10)' | bc -l 2.00000000000000000000 Use the l function which is the natural log. Take the log of the number you are interested in then divide by the natural log of 10. the logarithm of x in respect to base b can be computed given any logarithm function to an arbitrary base k -- that's actually pretty cool! log_b(x) = log_k(x) / log_k(b) e.g. log_b(x) = ln(x) / ln(b) if b=10: log_10(x) = ln(x) / ln(10)

How to get bc to handle numbers in scientific (aka exponential) notation?

a 夏天 提交于 2019-11-28 07:12:51
bc doesn't like numbers expressed in scientific notation (aka exponential notation). $ echo "3.1e1*2" | bc -l (standard_in) 1: parse error but I need to use it to handle a few records that are expressed in this notation. Is there a way to get bc to understand exponential notation? If not, what can I do to translate them into a format that bc will understand? Ferdinando Randisi Unfortunately, bc doesn't support scientific notation. However, it can be translated into a format that bc can handle, using extended regex as per POSIX in sed: sed -E 's/([+-]?[0-9.]+)[eE]\+?(-?)([0-9]+)/(\1*10^\2\3)/g'