MWorks expression feature request - low priority

Hi Chris,

It would be useful to have XML/STX functions for rounding to a set number of decimal places and a set number of significant digits. I’d be happy with a work-alike to matlab roundto.m and say the third-party chop.m:
http://www.liv.ac.uk/pjgiblin/maths/mfiles/chop.m

It’s possible to do this with int casting but makes for very complicated expressions and is difficult to get right.

Very low-priority - this doesn’t stop us from doing anything important.

Thanks!
Mark

Hi Mark,

Sorry for the delay in responding. I’ve opened a ticket for this and will try to get these functions in to the upcoming 0.5 release.

Cheers,
Chris

Hi Mark,

I’d be happy with a work-alike to matlab roundto.m

I assumed this was a MATLAB built-in, but it appears that it isn’t. Is this the function you’re referring to?

http://code.google.com/p/pmtk3/source/browse/trunk/matlabTools/stats/roundto.m

Thanks,
Chris

I don’t know what I was thinking in the earlier message, but I think what would be best is to add a
round() and a ceil() function that act like C’s - e.g. round(3) - Linux man page.
floor() is nice for symmetry but can be emulated by casting, right?

Then roundto() can be implemented in the XML by round(num*10x)/10x.

Thank you,
Mark

OK, that’s even easier. I’ll add round, ceil, and floor to the parser.

Thanks,
Chris

Hi Mark,

I’ve added ceil, floor, and round functions to the expression parser. They’ll be available in tonight’s nightly build.

Cheers,
Chris

Great! When you get a chance can you document how they work for negative numbers? (i.e. round/truncate toward zero or toward -Inf)

(I was recently having trouble figuring out how mod worked in MWorks for negative signed integers and eventually just gave up and worked around it.)

Thanks for all this work, Chris.

Mark

Hi Mark,

When you get a chance can you document how they work for negative numbers? (i.e. round/truncate toward zero or toward -Inf)

They’re simple wrappers around the corresponding C++ standard library functions, which return the following results:

  • ceil: nearest integer not less than the given value
  • floor: nearest integer not greater than the given value
  • round: nearest integer, rounding away from zero in halfway cases

(I was recently having trouble figuring out how mod worked in MWorks for negative signed integers and eventually just gave up and worked around it.)

The expression parser’s modulo operation uses the C++ modulo operator. In the case of negative operands, the C++98 standard leaves the sign of the result up to the implementation. (Or so says Wikipedia, at least.) However, both gcc and clang appear to follow the C++11 standard in this regard and give the result the same sign as the dividend.

I don’t know if that makes things any clearer, but there it is.

Chris

Thanks for putting this together, Chris, it’s very helpful. I appreciate you checking the current Mac OS behavior. Mark