Forums > MaxMSP

Progressively remove decimal digits

February 7, 2013 | 8:51 pm

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…


February 7, 2013 | 11:23 pm

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

– Pasted Max Patch, click to expand. –

February 7, 2013 | 11:28 pm

eh… just message "num decimal places"… easy


February 7, 2013 | 11:48 pm

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


February 8, 2013 | 12:12 am

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


February 8, 2013 | 9:05 am

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.



MIB
February 8, 2013 | 9:35 am

[OT]
Rule no.1: The Doctor LIES!

But listen to Peter ;)


February 9, 2013 | 12:53 pm

[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?


February 9, 2013 | 3:11 pm

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"… :)


February 9, 2013 | 5:54 pm

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

February 9, 2013 | 10:34 pm

This is awesome right there.

thanks


February 10, 2013 | 4:05 am

similar but using js:

– Pasted Max Patch, click to expand. –

February 11, 2013 | 10:19 am

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.


February 11, 2013 | 1:11 pm

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 ?-)

[attachment=214546,5015]

Attachments:
  1. ScreenShot20130211at13.57.48.png

February 11, 2013 | 2:59 pm

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


February 12, 2013 | 5:21 pm

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


February 13, 2013 | 4:56 pm

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. :)


Viewing 17 posts - 1 through 17 (of 17 total)