Float operation with bc?BC—automatic full precision multiplicationRound/truncate digit in string in zsh (or...
Sleeping solo in a double sleeping bag
Do predators tend to have vertical slit pupils versus horizontal for prey animals?
Using は before 欲しい instead が
Count the frequency of items in an array
Land Registry Clause
Is it appropriate for a business to ask me for my credit report?
My two team members in a remote location don't get along with each other; how can I improve working relations?
Is there such a thing as too inconvenient?
Convert HTML color to OLE
Is this kind of description not recommended?
Can others monetize my project with GPLv3?
Are required indicators necessary for radio buttons?
Sort, slice and rebuild new object with array data
Does git delete empty folders?
Are there reliable, formulaic ways to form chords on the guitar?
Was Switzerland really impossible to invade during WW2?
I think my coworker went through my notebook and took my project ideas
Don't teach Dhamma to those who can't appreciate it or aren't interested
Why doesn't the Falcon-9 first stage use three legs to land?
Alchemist potion on Undead
Can a Beast Master ranger choose a swarm as an animal companion?
How do slats reduce stall speed?
But though we be the children of technology
What professions does medieval village with a population of 100 need?
Float operation with bc?
BC—automatic full precision multiplicationRound/truncate digit in string in zsh (or with external tool)Syntax error comparing floats with bcRound/truncate digit in string in zsh (or with external tool)How to round or convert a float value to int with bc? getting: “(standard_in) 1: syntax error”Understand “ibase” and “obase” in case of conversions with bc?Float operations with bc not precise?How do I get bc to start decimal fractions with a leading zeroUnix command to return maximum float value out of two after subtractionBash as float calculatorlinux + how to calculate float numbers with bashCalculate difference in negative floating numbers with bc
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
It looks like bc doesn't support float operations, when I do echo 1/8 | bc
it get me a zero.
I checked bc (1)
, but it doesn't even mention float
, so I wonder if it's supported?
bc
add a comment |
It looks like bc doesn't support float operations, when I do echo 1/8 | bc
it get me a zero.
I checked bc (1)
, but it doesn't even mention float
, so I wonder if it's supported?
bc
8
echo "scale=4; 1/8" | bc
– LiuYan 刘研
Mar 4 '13 at 2:20
This question and in particular the accepted answer might be interesting to you.
– Emanuel Berg
Mar 6 '13 at 1:21
add a comment |
It looks like bc doesn't support float operations, when I do echo 1/8 | bc
it get me a zero.
I checked bc (1)
, but it doesn't even mention float
, so I wonder if it's supported?
bc
It looks like bc doesn't support float operations, when I do echo 1/8 | bc
it get me a zero.
I checked bc (1)
, but it doesn't even mention float
, so I wonder if it's supported?
bc
bc
asked Mar 4 '13 at 2:07
daisydaisy
30.5k53 gold badges182 silver badges312 bronze badges
30.5k53 gold badges182 silver badges312 bronze badges
8
echo "scale=4; 1/8" | bc
– LiuYan 刘研
Mar 4 '13 at 2:20
This question and in particular the accepted answer might be interesting to you.
– Emanuel Berg
Mar 6 '13 at 1:21
add a comment |
8
echo "scale=4; 1/8" | bc
– LiuYan 刘研
Mar 4 '13 at 2:20
This question and in particular the accepted answer might be interesting to you.
– Emanuel Berg
Mar 6 '13 at 1:21
8
8
echo "scale=4; 1/8" | bc
– LiuYan 刘研
Mar 4 '13 at 2:20
echo "scale=4; 1/8" | bc
– LiuYan 刘研
Mar 4 '13 at 2:20
This question and in particular the accepted answer might be interesting to you.
– Emanuel Berg
Mar 6 '13 at 1:21
This question and in particular the accepted answer might be interesting to you.
– Emanuel Berg
Mar 6 '13 at 1:21
add a comment |
3 Answers
3
active
oldest
votes
bc
doesn't do floating point but it does do fixed precision decimal numbers. The -l
flag Hauke mentions loads a math library for eg. trig functions but it also means
[...] the default scale is 20
scale
is one of a number of "special variables" mentioned in the man page. You can set it:
scale=4
Anytime you want (whether -l
was used or not). It refers to the number of significant digits used in a decimal. In other words, subsequent solutions will be rounded to that number of digits after the decimal scale (== fixed precision).
The default scale sans -l
is 0, meaning rounded to whole numbers.
add a comment |
man page says:
If bc is invoked with the -l option, a math library is preloaded [...]
The comprehensibility of that could be improved, indeed...
14
Report it upstream as a bug...
– vonbrand
Mar 4 '13 at 3:18
add a comment |
1) Numbers in bc have a scale. The scale of a number should not be confused with the scale factor. The same world 'scale' is used as a function to query the scale of a number or as a parameter to set the scale factor.
echo "scale=scale(1.1);11/10" | bc will return 1.1
2) The scale factor determines how many digits are kept to the right of the decimal point when doing operations. If s is the current scale factor, sa is the scale of the first operand a, sb is the scale of the second operand b, results are truncated as follow :
scale of result
a+b max(sa,sb)
a-b max(sa,sb)
a*b min(sa+sb , max(s,sa,sb))
a/b s
a%b so that a = b*quotient + remainder; remainder has sign of a
a^b min(sa×|b|, max(s,sa)); b must be integer
sqrt(a) max(s,sa)
3) At the origin bc was a preprocessor of dc. Now, on many systems, bc is a standalone program.
Source : [https://plan9.io/magic/man2html/1/dc]
New contributor
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f66766%2ffloat-operation-with-bc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
bc
doesn't do floating point but it does do fixed precision decimal numbers. The -l
flag Hauke mentions loads a math library for eg. trig functions but it also means
[...] the default scale is 20
scale
is one of a number of "special variables" mentioned in the man page. You can set it:
scale=4
Anytime you want (whether -l
was used or not). It refers to the number of significant digits used in a decimal. In other words, subsequent solutions will be rounded to that number of digits after the decimal scale (== fixed precision).
The default scale sans -l
is 0, meaning rounded to whole numbers.
add a comment |
bc
doesn't do floating point but it does do fixed precision decimal numbers. The -l
flag Hauke mentions loads a math library for eg. trig functions but it also means
[...] the default scale is 20
scale
is one of a number of "special variables" mentioned in the man page. You can set it:
scale=4
Anytime you want (whether -l
was used or not). It refers to the number of significant digits used in a decimal. In other words, subsequent solutions will be rounded to that number of digits after the decimal scale (== fixed precision).
The default scale sans -l
is 0, meaning rounded to whole numbers.
add a comment |
bc
doesn't do floating point but it does do fixed precision decimal numbers. The -l
flag Hauke mentions loads a math library for eg. trig functions but it also means
[...] the default scale is 20
scale
is one of a number of "special variables" mentioned in the man page. You can set it:
scale=4
Anytime you want (whether -l
was used or not). It refers to the number of significant digits used in a decimal. In other words, subsequent solutions will be rounded to that number of digits after the decimal scale (== fixed precision).
The default scale sans -l
is 0, meaning rounded to whole numbers.
bc
doesn't do floating point but it does do fixed precision decimal numbers. The -l
flag Hauke mentions loads a math library for eg. trig functions but it also means
[...] the default scale is 20
scale
is one of a number of "special variables" mentioned in the man page. You can set it:
scale=4
Anytime you want (whether -l
was used or not). It refers to the number of significant digits used in a decimal. In other words, subsequent solutions will be rounded to that number of digits after the decimal scale (== fixed precision).
The default scale sans -l
is 0, meaning rounded to whole numbers.
answered Mar 4 '13 at 2:25
goldilocksgoldilocks
65.5k18 gold badges165 silver badges218 bronze badges
65.5k18 gold badges165 silver badges218 bronze badges
add a comment |
add a comment |
man page says:
If bc is invoked with the -l option, a math library is preloaded [...]
The comprehensibility of that could be improved, indeed...
14
Report it upstream as a bug...
– vonbrand
Mar 4 '13 at 3:18
add a comment |
man page says:
If bc is invoked with the -l option, a math library is preloaded [...]
The comprehensibility of that could be improved, indeed...
14
Report it upstream as a bug...
– vonbrand
Mar 4 '13 at 3:18
add a comment |
man page says:
If bc is invoked with the -l option, a math library is preloaded [...]
The comprehensibility of that could be improved, indeed...
man page says:
If bc is invoked with the -l option, a math library is preloaded [...]
The comprehensibility of that could be improved, indeed...
answered Mar 4 '13 at 2:17
Hauke LagingHauke Laging
59.8k12 gold badges93 silver badges140 bronze badges
59.8k12 gold badges93 silver badges140 bronze badges
14
Report it upstream as a bug...
– vonbrand
Mar 4 '13 at 3:18
add a comment |
14
Report it upstream as a bug...
– vonbrand
Mar 4 '13 at 3:18
14
14
Report it upstream as a bug...
– vonbrand
Mar 4 '13 at 3:18
Report it upstream as a bug...
– vonbrand
Mar 4 '13 at 3:18
add a comment |
1) Numbers in bc have a scale. The scale of a number should not be confused with the scale factor. The same world 'scale' is used as a function to query the scale of a number or as a parameter to set the scale factor.
echo "scale=scale(1.1);11/10" | bc will return 1.1
2) The scale factor determines how many digits are kept to the right of the decimal point when doing operations. If s is the current scale factor, sa is the scale of the first operand a, sb is the scale of the second operand b, results are truncated as follow :
scale of result
a+b max(sa,sb)
a-b max(sa,sb)
a*b min(sa+sb , max(s,sa,sb))
a/b s
a%b so that a = b*quotient + remainder; remainder has sign of a
a^b min(sa×|b|, max(s,sa)); b must be integer
sqrt(a) max(s,sa)
3) At the origin bc was a preprocessor of dc. Now, on many systems, bc is a standalone program.
Source : [https://plan9.io/magic/man2html/1/dc]
New contributor
add a comment |
1) Numbers in bc have a scale. The scale of a number should not be confused with the scale factor. The same world 'scale' is used as a function to query the scale of a number or as a parameter to set the scale factor.
echo "scale=scale(1.1);11/10" | bc will return 1.1
2) The scale factor determines how many digits are kept to the right of the decimal point when doing operations. If s is the current scale factor, sa is the scale of the first operand a, sb is the scale of the second operand b, results are truncated as follow :
scale of result
a+b max(sa,sb)
a-b max(sa,sb)
a*b min(sa+sb , max(s,sa,sb))
a/b s
a%b so that a = b*quotient + remainder; remainder has sign of a
a^b min(sa×|b|, max(s,sa)); b must be integer
sqrt(a) max(s,sa)
3) At the origin bc was a preprocessor of dc. Now, on many systems, bc is a standalone program.
Source : [https://plan9.io/magic/man2html/1/dc]
New contributor
add a comment |
1) Numbers in bc have a scale. The scale of a number should not be confused with the scale factor. The same world 'scale' is used as a function to query the scale of a number or as a parameter to set the scale factor.
echo "scale=scale(1.1);11/10" | bc will return 1.1
2) The scale factor determines how many digits are kept to the right of the decimal point when doing operations. If s is the current scale factor, sa is the scale of the first operand a, sb is the scale of the second operand b, results are truncated as follow :
scale of result
a+b max(sa,sb)
a-b max(sa,sb)
a*b min(sa+sb , max(s,sa,sb))
a/b s
a%b so that a = b*quotient + remainder; remainder has sign of a
a^b min(sa×|b|, max(s,sa)); b must be integer
sqrt(a) max(s,sa)
3) At the origin bc was a preprocessor of dc. Now, on many systems, bc is a standalone program.
Source : [https://plan9.io/magic/man2html/1/dc]
New contributor
1) Numbers in bc have a scale. The scale of a number should not be confused with the scale factor. The same world 'scale' is used as a function to query the scale of a number or as a parameter to set the scale factor.
echo "scale=scale(1.1);11/10" | bc will return 1.1
2) The scale factor determines how many digits are kept to the right of the decimal point when doing operations. If s is the current scale factor, sa is the scale of the first operand a, sb is the scale of the second operand b, results are truncated as follow :
scale of result
a+b max(sa,sb)
a-b max(sa,sb)
a*b min(sa+sb , max(s,sa,sb))
a/b s
a%b so that a = b*quotient + remainder; remainder has sign of a
a^b min(sa×|b|, max(s,sa)); b must be integer
sqrt(a) max(s,sa)
3) At the origin bc was a preprocessor of dc. Now, on many systems, bc is a standalone program.
Source : [https://plan9.io/magic/man2html/1/dc]
New contributor
New contributor
answered 2 days ago
cd9cd9
11 bronze badge
11 bronze badge
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f66766%2ffloat-operation-with-bc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
8
echo "scale=4; 1/8" | bc
– LiuYan 刘研
Mar 4 '13 at 2:20
This question and in particular the accepted answer might be interesting to you.
– Emanuel Berg
Mar 6 '13 at 1:21