Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Using the gnu debugger as a high-precision calculator UNIX
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)

Using the gnu debugger as a high-precision calculator | 15 comments | Create New Account
Click here to return to the 'Using the gnu debugger as a high-precision calculator' hint
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
bc - An arbitrary precision calculator language


[ Reply to This | # ]
Using the gnu debugger as a high-precision calculator
Authored by: rwmitchell on Oct 22, '04 11:58:40AM

Or dc

dc - an arbitrary precision calculator

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.

[ Reply to This | # ]
Authored by: rhowell on Oct 22, '04 01:30:03PM
Doh! I wish I knew about "bc" when Google put up its billboard ad.

>bc -l



[ Reply to This | # ]
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)

[ Reply to This | # ]
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...

[ Reply to This | # ]
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.



[ Reply to This | # ]
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

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

[ Reply to This | # ]
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: It compiles cleanly on Mac OS X, and is also an arbitrary precision calculator.

[ Reply to This | # ]
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's rather inferior brains? :-)

[ Reply to This | # ]

Replacing 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.

[ Reply to This | # ]

Replacing 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

[ Reply to This | # ]
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 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()
bad buffer in yy_scan_bytes()

[ Reply to This | # ]
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
% 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.


[ Reply to This | # ]
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.

[ Reply to This | # ]
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 ;)

Madness takes its toll.
Please have exact change.

[ Reply to This | # ]