14,000 hints and counting!

Using the gnu debugger as a high-precision calculator
This hint requires the Developer Tools, which, of course, everyone has :). See, my problem with Apple's brain-dead Calculator application is that floating point invariably gets converted to integer (it used to anyway, now this is fixed in 10.3.5). Well, okay ... you can use still use gdb as an unlimited (sorta) precision calculator! Gotta do it in the Terminal, though. Here's how:
``````% gdb
...
(gdb) p 3.141579 * 2
\$1 = 6.2831580000000002
(gdb) q
``````
The p character stands for "print," meaning print the results of the equation. Okay, so maybe the amount of precision is overkill, but it's better than no precision at all! You can also use previous results in further equations. See that rather archaic \$1 in the result there? Well, in a different equation (in the same gdb session, of course), you can type that \$1, and it will substitute the calculated value. For example:
``````% gdb
...
(gdb) p 3.141579 * 2
\$1 = 6.2831580000000002
(gdb) p \$1 / 2
\$2 = 3.1415790000000001
(gdb) q
``````
You can also use \$2 as result in another equation ... and so on. Note that a similar hint was provided using Applescript.
•
• Currently 2.50 / 5
You rated: 1 / 5 (4 votes cast)

[10,221 views]

## Hint Options

Using the gnu debugger as a high-precision calculator | 15 comments | Create New Account
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Using the gnu debugger as a high-precision calculator
Authored by: geohar on Oct 22, '04 11:25:49AM

Ah interesting. But what's wrong with bc?

>man bc
NAME
bc - An arbitrary precision calculator language
.....

;)

Using the gnu debugger as a high-precision calculator
Authored by: rwmitchell on Oct 22, '04 11:58:40AM

Or dc

NAME
dc - an arbitrary precision calculator

DESCRIPTION
Dc is a reverse-polish desk calculator which supports unlimited preci-
sion arithmetic. It also allows you to define and call macros.

And it uses reverse-polish notation to boot! Beats the heck out of that other way of doing math.

high-precision!
Authored by: rhowell on Oct 22, '04 01:30:03PM

>bc -l

>>scale=1000
>>e(1)

>>2.718281828459045235360287471352662497757247093699959574966967627724
07663035354759457138217852516642742746639193200305992181741359662904
35729003342952605956307381323286279434907632338298807531952510190115
73834187930702154089149934884167509244761460668082264800168477411853
74234544243710753907774499206955170276183860626133138458300075204493
38265602976067371132007093287091274437470472306969772093101416928368
19025515108657463772111252389784425056953696770785449969967946864454
90598793163688923009879312773617821542499922957635148220826989519366
80331825288693984964651058209392398294887933203625094431173012381970
68416140397019837679320683282376464804295311802328782509819455815301
75671736133206981125099618188159304169035159888851934580727386673858
94228792284998920868058257492796104841984443634632449684875602336248
27041978623209002160990235304369941849146314093431738143640546253152
09618369088870701676839642437814059271456354906130310720851038375051
01157477041718986106873969655212671546889570350354
high-precision!
Authored by: aranor on Oct 22, '04 06:33:49PM

Oh, yeah, that would have been useful. I ended up using Mathematica to calculate e to the appropriate number of places and then wrote a python script to solve the problem (later I re-wrote the python script in Mathematica itself)

high-precision!
Authored by: geohar on Oct 23, '04 04:24:52AM

I ended up kicking mathkernel invokations to do primality testing. ;) Perl script to hang it all together...

Using the gnu debugger as a high-precision calculator
Authored by: jonbauman on Oct 22, '04 02:38:25PM

personally, I think Python makes a pretty good desk calculator, and is generally more powerful than bc.

---

jon

Using the gnu debugger as a high-precision calculator
Authored by: johnblommers on Oct 22, '04 03:50:06PM

The gnu debugger is weak. Look at the trailing roundoff errors. We are better off with the bc -l (bc dash ell) command:
bc -l
scale=50
4*a(1)
3.14159265358979323846264338327950288419716939937508

Here a(1) returns the arctangent of 1 which is pi/4 so now we have REAL precision!

Using the gnu debugger as a high-precision calculator
Authored by: maartensneep on Oct 22, '04 06:03:14PM

While bc is installed by default, its syntax is a bit archaic.

I prefer calc: http://www.isthe.com/chongo/tech/comp/calc/index.html. It compiles cleanly on Mac OS X, and is also an arbitrary precision calculator.

Using the gnu debugger as a high-precision calculator
Authored by: recusant on Oct 22, '04 08:18:02PM

The question here really is why is any software company shipping a calculator desktop app that isn't using arbitrary precision algorithms.

Apple, you had an excuse when you were shipping 1MHz 6502 based machines. Any machine made in the last ten years can stand up to using an arbitrary precision engine in a calculator app.

So here's the question: Calculator has an executable called "CalcEngine" in the resource bundle. It appears to take algebraic calculations from stdin and write the answers to stdout. Anyone up to the challenge of figuring out the expected symbols and notation to creating a wrapper to bc or calc to replace Calculator.app's rather inferior brains? :-)

Replacing Calculator.app/CalcEngine with 'bc'
Authored by: recusant on Oct 22, '04 08:34:28PM

Ok, so I moved CalcEngine to CalcEngine.backup and created a symbolic link to bc called CalcEngine to see how badly it would break.

It mostly works in the Basic view, but many of the functions aren't recognized in the Advanced view. Looks like a job for sed, but it would be a lot easier with some info on the CalcEngine program itself. It also won't recognize decimals (sets the precision to 0)

Don't forget to put the original CalcEngine back if you do this experiment.

Replacing Calculator.app/CalcEngine with 'bc'
Authored by: fracai on Oct 25, '04 10:11:07AM

oh how I wish I had left my computer up when I went to work this morning.

make a script that receives stdin and writes it to a file
heck, just make a named pipe

then run tail -f on the output file or pipe
run some basic/advanced calculations and you've got the format

when I get home I'll set this up and see what I get.

---
i am jack's amusing sig file

Using the gnu debugger as a high-precision calculator
Authored by: Carnildo on Oct 25, '04 05:45:50PM

CalcEngine seems to be a more powerful calculator than the Calculator.app frontend -- it has support for such things as variables and full infix notation. Also, using "strings" on it gives some interesting results:

yacc stack overflow
fatal flex scanner internal error--no action found
fatal flex scanner internal error--end of buffer missed
fatal error - scanner input buffer overflow
flex scanner push-back overflow
out of dynamic memory in yy_create_buffer()
out of dynamic memory in yy_scan_buffer()
out of dynamic memory in yy_scan_bytes()

a quick tcsh alias for expressions
Authored by: bdm on Oct 22, '04 09:36:23PM

Here is a simple tcsh alias for quick calculations on the command line. Someone will tell us how to define it in bash too...

`% alias calc 'echo "END{print \!:* }" | awk -f /dev/stdin' `

Now you can calculate anything that awk can do in a single expression, or a list of expressions separated by commas:

``` % calc sin(2.3)*2^4.5 16.8734 % calc log(512)/log(2), log(513)/log(2) 9 9.00282 % calc x=3,y=4,x^3/y 3 4 6.75 ```

Do `man awk` to see what is possible. Note that you don't have to escape special characters, which is why the alias looks more complex than you'd expect. Make sure your bash version has that property too.

Brendan.

Using the gnu debugger as a high-precision calculator
Authored by: russellh on Oct 24, '04 10:26:57PM

Don't forget about dc ("desk calculator"), the arbitrary precision RPN calculator.

Was that supposed to be pi?
Authored by: dzurn on Oct 26, '04 12:55:47AM

I hope the original hinter wasn't trying to use pi in his example. He's got his last two digits wrong.

And yes, I knew that before I saw pi to 50 places by another poster ;)

---