Mit Gnu bc teilen

2663
Boldewyn

Ich fange gerade mit Gnu an und bin ganz am Anfang (sehr entmutigend ...). Ich möchte zwei Zahlen teilen und als Ergebnis einen Float erhalten:

$bc bc 1.06.94 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'.   15/12 1 15.0/12.0 1 15.000000/12.000000 1 scale(15.00000) 5 

Die Manpage besagt, dass diese Division eine Zahl mit derselben Skala wie die Anfangswerte zurückgibt. Das stimmt natürlich entweder nicht oder mir fehlt etwas.

Googling hat keine neuen Erkenntnisse gebracht (außerdem kann 'BC' auch für 'British Columbia' stehen).

Sehen Sie meinen Fehler? Besser noch, kennen Sie gute Referenzen / Tutorials zu bc?

5

4 Antworten auf die Frage

7
Joey

Sie können die Skala mit einstellen

scale=2 

Dann funktioniert die Abteilung wie erwartet:

scale=2 15/12 1.25 

Um aus Wikipedia zu zitieren:

Alle Zahlen und Variableninhalte sind willkürliche Zahlen, deren Genauigkeit (in Dezimalstellen) von der globalen scaleVariablen bestimmt wird.

Ah, das ist der Kern des Pudels. Kann ich den Maßstab global in einer .bcrc-Datei festlegen? Boldewyn vor 14 Jahren 0
Fand es: `export BC_ENV_ARGS = ~ / .bcrc` Boldewyn vor 14 Jahren 0
Ich bezweifle, dass »Das auch Krieg des Pudels Kern« zuverlässig in anderen Sprachen funktioniert :-) Zumindest weiß ich nicht, wie bekannt Faust in anderen Sprachen ist :-) Joey vor 14 Jahren 0
Dies war der eigentliche Kern des Pudels, also ein reisender Gelehrter? Der Casus lenkt ab. Projekt Gutenberg ist dein Freund: http://www.gutenberg.org/files/14591/14591-h/14591-h.htm Boldewyn vor 14 Jahren 0
Aber ich muss dem zustimmen, und LEO.org schlägt "den Kern der Sache" vor ;-) Boldewyn vor 14 Jahren 0
7
jlbartos

Verwenden Sie bc -l, um die mathematische Bibliothek vorab zu laden. Die Standardskala ist auf 20 eingestellt.

$ bc -l 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'.  4/3 1.33333333333333333333 
1
Colin Daly

In case this helps anyone, I was trying to get scale=2 and quiet mode set by default and I couldn't seem to get it right.

If I set BC_ENV_VARS="-q", I got quiet mode and if I set BC_ENV_VARS="~/.bcrc", I could set scale=2 in this file but I couldn't turn on quiet mode (maybe there is a way but I couldn't find it).

In the end I used export BC_ENV_ARGS=~/.bcrc (with scale=2) and aliased bc to bc -q.

1
andkore

To expand on Colin_Daly, use BC_ENV_ARGS="-q $HOME/.bcrc", with scale=INT in .bcrc obviously. The environment variable is basically just stuck right after the bc command.

What's happening is that -q is just a normal command line option, and $HOME/.bcrc is expanded and fed into bc as the first file argument (there can be multiple file arguments). scale=INT is a line of bc, which is a programming language. It's functionally equivalent to running bc and entering scale=INT on the bc prompt. You have to use $HOME and not ~ because ~ is only recognized when it's not quoted (see here), but we need some sort of quoting so the variable is set to both words, not just up to the space.