Difference between revisions of "Bc"

From Teknologisk videncenter
Jump to: navigation, search
m
m (Links)
 
(3 intermediate revisions by the same user not shown)
Line 100: Line 100:
 
340282366920938463463374607431768211456
 
340282366920938463463374607431768211456
 
[heth@mars ~]$
 
[heth@mars ~]$
 +
</source>
 +
= Factorial in bc=
 +
<source lang=cli>
 +
<input>define f(x) {
 +
  if (x>1) {
 +
    return (x * f (x-1))
 +
  }
 +
  return (1)
 +
 +
}
 +
f(3)</input>
 +
6
 +
<input>f(128)</input>
 +
38562048236258042173567706592346364061749310959022359027882840327637\
 +
34025751655435606861685885073615340300518330589163475921729322624988\
 +
57766114955245039357760034644709279247692495585280000000000000000000\
 +
000000000000
 
</source>
 
</source>
 
{{Source cli}}
 
{{Source cli}}
[[category:UNIX]][[Category:linux]]
+
=Links=
 +
*[http://phodd.net/gnu-bc/ Mange moduler til bc]
 +
[[category:UNIX]][[Category:linux]][[Category:Linux Command]]
 +
=Større tal=
 +
**[http://acme.com/software/bigint/ bigint] Even larger numbers than bc

Latest revision as of 09:00, 19 May 2013

Leg med bc

bc programmet er en avanceret regneprogram, som faktisk også har indbygget et programmeringssprog og som samtidig er forholdsvis nem at betjene.

Lidt leg med bc for at lære den at kende

Store tal

Hvor mange mulige IP addresser er der i IPv6 i forhold til IPv4.

  • IPv4 - 32 bits adresser
  • IPv6 - 128 bits adresser
[heth@mars ~]$ <input>bc</input>
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
<input>2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2</input>
4294967296

Puha det var mange gange jeg skulle skrive 2*2*2... 32 gange. Heldigvis kan vi opløfte i potenser

[heth@mars ~]$ <input>bc</input>
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
<input>2^32</input>
4294967296

For IPv6

[heth@mars ~]$ <input>bc</input>
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'.
<input>2^128</input>
340282366920938463463374607431768211456

Hvor mange mulige kombinationer af nøgler har 4096 bits kryptering

[heth@mars ~]$ <input>bc</input>
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
<input>2^4096</input>
10443888814131525066917527107166243825799642490473837803842334832839\
53907971557456848826811934997558340890106714439262837987573438185793\
60726323608785136527794595697654370999834036159013438371831442807001\
18559462263763188393977127456723346843445866174968079087058037040712\
84048740118609114467977783598029006686938976881787785946905630190260\
94059957945343282346930302669644305902501597239986771421554169383555\
98852914863182379144344967340878118726394964751001890413490084170616\
75093668333850551032972088269550769983616369411933015213796825837188\
09183365675122131849284636812555022599830041234478486259567449219461\
70238065059132456108257318353800876086221028342701976982023131690176\
78006675195485079921636419370285375124784014907159135459982790513399\
61155179427110683113409058427288427979155484978295432353451706522326\
90613949059876930021229633956877828789484406160074129456749198230505\
71642377154816321380631045902916136926708342856440730447899971901781\
46576347322385026725305989979599609079946920177462481771844986745565\
92501783290704731194331655508075682218465717463732968849128195203174\
57002440926616910874148385078411929804522981857338977648103126085903\
00130241346718972667321649151113160292078173803343609024380470834040\
3154190336

Det er jo et stort tal. Bemærk backslashen i linerne betyder at linien fortsætter.

Funktioner

bc kan anvendes til meget komplicerede beregninger. I eksemplet herunder beregnes PI med 10.000 decimaler. Tager lidt tid.

[heth@mars ~]$ <input>bc -l -q
scale = 10000
(12*a(1/49)+32*a(1/57)-5*a(1/239)+12*a(1/110443))*4</input>
3.141592653589793238462643383279502884197169399375105820974944592307\
81640628620899862803482534211706798214808651328230664709384460955058\
22317253594081284811174502841027019385211055596446229489549303819644\
28810975665933446128475648233786783165271201909145648566923460348610\
45432664821339360726024914127372458700660631558817488152092096282925\
<notice>De næste 150 linier er udeladt</notice>


Andre talsystemer

bc kan anvende ethvert talsystem. For at lave et netværksfilter havde jeg brug for at omsætte min MAC-adresse som er hexadecimal til binært

[heth@mars ~]$ <input>bc</input>
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
<input>obase=2
ibase=16
0021869FC355</input>
10000110000110100111111100001101010101
<input>quit</input>
[heth@mars ~]$

Anvendelse af bc i scripts

Piping til bc

Fra script kunne hexadecimal til binær omregning som i sidste eksempel udføres som (Husk \n er nylinie tegnet)

[root@mars log]#<input>echo -e "obase=2\nibase=16\n0021869FC355" | bc</input>
10000110000110100111111100001101010101


[heth@mars ~]$ <input>echo "2^128" | bc</input>
340282366920938463463374607431768211456
[heth@mars ~]$

Factorial in bc

<input>define f(x) {
  if (x>1) {
    return (x * f (x-1))
  }
  return (1)

}
f(3)</input>
6
<input>f(128)</input>
38562048236258042173567706592346364061749310959022359027882840327637\
34025751655435606861685885073615340300518330589163475921729322624988\
57766114955245039357760034644709279247692495585280000000000000000000\
000000000000

Links

Større tal

    • bigint Even larger numbers than bc