7.13.2010

Maxima Function for Rounding off

If you know a built-in Maxima function for this, let me know.
And it's not round()!

See the code here: http://docs.google.com/document/pub?id=18PhgfcZXo_rjaCvVj2Q2tcYoFQ3cOYSEXcOo0NXE4rg

The Maxima built-in function round() does the following:

round(x)

"When x is a real number, returns the closest integer to x. Multiples of 1/2 are rounded to the nearest even integer. Evaluation of x is similar to floor( ) and ceiling( )."

It means that round(1.5) will, as we have been taught,
correctly result to 2, but round(0.5) will result to 0.
(Try round(0.51); the result is 1.)

And with this function you cannot specify how many decimal places to retain since it rounds off to an integer and the parameters of the function itself does NOT allow that to happen, either.

So, after tweaking with it, I came up with the function posted in the link above.

It's messy, I know. This is how it works:

roundoff(base, place)

where:
base is the value you want to round-off
place is the number of decimal places you wish to retain

There are a LOT of ideas in that one-liner of a Maxima function, but I choose to explain the stuff on a later post.
(Yeah, for example: Why the hell would I use 4.999999999985448 instead of 5 ?)

Anyway, see the screenshot below(click it to full view). If you got any other code from the Web, please tell me in the comments. Thanks.