Emacs Calc: Matrix Mapping “Close Enough” Keyboard Macro

In Emacs Calculator, frequently I come up with results that that are algebraically supposed to be a whole number, but instead because of precision error they end up as some annoying value that is very close. For example, I’m expecting a 0 but get “3.69600000004e-11” which is an extremely small value close to zero. For matrices, I defined this shortcut which maps over the elements, rounding them if they are “close enough” to the whole number.

After defining the macro I inserted it into ~/.emacs.d/calc.el:

(fset 'calc-ce
   (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ([86 77 39 105 102 40 97 98 115 40 114 111 117 110 100 40 120 41 45 120 41 32 60 32 48 46 48 48 48 48 48 48 49 44 114 111 117 110 100 40 120 41 44 120 41 return return] 0 "%d")) arg)))

In calc, press ‘x c e’.

So, this


This is not the same thing as simply rounding as it only rounds if the value is very close (under 0.0000001 difference). The formula mapped over the matrix is

if(abs(round(x)-x) < 0.0000001,round(x),x)

A different approach I tried was to multiply, truncate, and reverse the multiplication, but this doesn’t work for values like 0.9999999999.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s