Progressively remove decimal digits

Ploki's icon

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

Ploki's icon
Max Patch
Copy patch and select New From Clipboard in Max.

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

Ploki's icon

eh... just message "num decimal places"... easy

Ploki's icon

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

Ploki's icon

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

Peter Castine's icon

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's icon

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

But listen to Peter ;)

Ploki's icon

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

Ploki's icon

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

Peter McCulloch's icon

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.

Max Patch
Copy patch and select New From Clipboard in Max.

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

Ploki's icon

This is awesome right there.

thanks

liubo's icon

similar but using js:

Max Patch
Copy patch and select New From Clipboard in Max.

Peter Castine's icon

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.

Peter Castine's icon

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

5015.ScreenShot20130211at13.57.48.png
png
Peter McCulloch's icon

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

Brian H.'s icon

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

Ploki's icon

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