Progressively remove decimal digits

Feb 7, 2013 at 8:51pm

Progressively remove decimal digits

How to remove accuracy of a number by one decimal digit each time, the decimal length not being fixed?

Any easy way to do it, or 7 sprintf objects + counter or sth like that…

Feb 7, 2013 at 11:23pm

Okay, like this, but i need to put the numbers back together, no idea how.

– Pasted Max Patch, click to expand. –
Feb 7, 2013 at 11:28pm

eh… just message “num decimal places”… easy

Feb 7, 2013 at 11:48pm

ah no.. that rounds them, it doesn’t truncate them

Feb 8, 2013 at 12:12am

Ugh, now regexp will add invisible zeroes at the end…

Feb 8, 2013 at 8:26am


This is very laborious to implement because each time your “message” is interpreted as a number, the floating-point behaviour destroys your work. What is for ?

Feb 8, 2013 at 9:05am

The only effective way to do this is using symbols, making sure that the symbols are *NEVER* converted to numeric values.

Once you’re in the numeric domain, anything along the line of what you’re trying to do will be squashed on by the laws of IEEE 754. If you insist on trying, go read the IEEE 754 documentation. At which point you will come back and beg to do it all with symbols.-)

Trust me. I’m the Doctor.

Feb 8, 2013 at 9:35am

Rule no.1: The Doctor LIES!

But listen to Peter ;)

Feb 9, 2013 at 12:53pm

[tosymbol] is limited to 4 decimal places. I really have to multiply it by 100 to get the accuracy i wish?

seems there should be a simpler way around this.

edit: Wow, i can’t believe I have so much trouble getting around something that seems as mundane as this?

Feb 9, 2013 at 2:36pm


“Wow, i can’t believe I have so much trouble getting around something that seems as mundane as this?”

Because it is not so easy ; This topic comes again and again in this forum and i’m surprised that “the Doctor” still wakes up for that ;-)

You have to think about : why do you want such precision : you really need to compute things with error less than 0.0001 ? Or is it just for cosmetic GUI ?

You can force MaxMSP to bring your number as a string and not a IEEE 754 with that kind of trick :

– Pasted Max Patch, click to expand. –
Feb 9, 2013 at 3:11pm

this is for fixed decimal places.

The output of an external outputs accuracy in terms of “1. == accurate, everything below is less accurate” so I wanted to create a failsafe that kickstarts the patcher into the next phase in case the accuracy of the result is insufficient.

I could just multiply the output of the external by 1’000’000 I guess. I’ll give it a shot that way, since I don’t really care whether it operates with floats or integers. Should’ve think of that before. Still, seems “unclean”… :)

Feb 9, 2013 at 5:54pm

Here’s an alternate way of doing it, using math instead of string formatting. This will give you the precision to n decimal places, but only if it’s needed. It won’t zero pad, but if that’s okay, then game on; I think that’s much easier to read, especially since the extra zeros aren’t providing you with useful information.

You can also display arbitrary precision with live.number, btw. Check out the units/unit format in the attached code.

– Pasted Max Patch, click to expand. –
Feb 9, 2013 at 10:34pm

This is awesome right there.


Feb 10, 2013 at 4:05am

similar but using js:

– Pasted Max Patch, click to expand. –
Feb 11, 2013 at 10:19am

Arithmetic approaches rely on the assumption that floating-point multiplication and division is carried out in decimal. It’s not; floating-point on computers is in binary (which is why I pointed people to IEEE 754, apparently an exercise in futility).

Both of the last two patches will give unexpected results (in other words, break) for the right (or, depending on your point of view, wrong) input values. I found a combination of values that broke Liubo’s patch in about one minute (on another machine right now, sorry, otherwise I would upload a confirmatory screen shot). I daresay there are values out there for which Peter’s patch will break.

To paraphrase John von Neuman: anyone attempting to round decimal values using binary arithmetic is, of course, in a state of sin.

At this stage, feeling more like the Magician’s Nephew than The Doctor.

Feb 11, 2013 at 10:53am

“Wow, i can’t believe I have so much trouble getting around something that seems as mundane as this?”

Feb 11, 2013 at 1:11pm

As promised. It turns out the same input (0.99805, 5 decimal places) breaks on both patches.

If you take the time to look at 0.99805 in binary floating-point and do the arithmetic in binary, you will see why the result of 0.98804 is inevitable.

Don't they teach this stuff in schools any more ?-)


  1. ScreenShot20130211at13.57.48.png
Feb 11, 2013 at 2:59pm

I’m aware of the precision issues, just had a feeling that this post had more to do with appearance than with some really high level of precision… (OP was truncating, after all)

If you care about decimal places and want to do it via math, do integer math (or if that’s too small, do in some other language such as Java with BigDecimal or Long–though in that case, the regexp stuff is not so bad IF you do it inside js/mxj. It’s a pain in Max because Max is always going to strip off leading zeros, etc.).

Feb 12, 2013 at 5:21pm

Peter, it depends on what you went to school for! :)

Feb 13, 2013 at 4:56pm

Thanks for the insight guys… Appreciated.

It’s not actually for *cosmetic* purposes but such accuracy isn’t mandatory either – so it’s fine. Thanks again

nicolas danet: Yeah.. But it seems so simple with pencil and rubber. :)


You must be logged in to reply to this topic.