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…
Okay, like this, but i need to put the numbers back together, no idea how.
----------begin_max5_patcher---------- 524.3ocwV1taaBCEF92vUgk+0lDEgMeu+safcCzUM4.dothXPXm1rV068gOF RxZyGtaILhBQ93ie4kGNbbdw2CuncCWgQeAcKxy6EeOOHjIf23XO7J1lpFlB RCuhqTrkbbfcNMeiFhGgJ28YZV45UBYCWCKkNF7msRsR7L2DiPBi1ka6Z8Tx jwnc8bEWpYZQq7G87Js0qoQlEhHkIlehFOgtabUVgz+piaSGi2NUGSWcuPtb OwRhKBySGTKADMqDjNeO8D0v8X6hGtIlh269PxVAWB7W6ErFz2X88sOgMy+p uu4TvkCqWNn9wvSbRgAHz3LGnC4pPGI+oA0eGbdtIT0Hp3+6jgdTxDbN5TZo yfzI4wYkIAn3Bas4gQDsXNQzMHxUrtQH0m9MqzbKJn6fCML8DvIelqeZ3x+m UOS.JCJYNCaxl2NOgkvg63I5x11IG5sSH.gRoVNcDzbhlxWv5kd9R9lNzmt8 6CG0284ChFxGAMomrxY+utfpw8urkTEGAUwtgJXs3Fg7s+8.35ah+m7S0ttu Zx8SWHDcqEp4JsPB6iuWRlcSQjsIcuntlCyOgqZghsngC9O5fOIc1OC8cQQm yOj4yOtvm2X5qpexcfOfelI9PcwOwyGexbwO4ymebp9I6uzO1W+YccOx6UiZ BVYnQ4Cs8lgYAvPgzNDTbnE4iho7K7Mp8p+uAfxIRWN -----------end_max5_patcher-----------
eh… just message "num decimal places"… easy
ah no.. that rounds them, it doesn’t truncate them
Ugh, now regexp will add invisible zeroes at the end…
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.
Rule no.1: The Doctor LIES!
But listen to Peter ;)
[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?
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"… :)
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.
----------begin_max5_patcher---------- 1128.3oc2Y10aaaCEF9Z6eEDBq.aEtthRxRV6tsta1sa.6l1h.JIZG1JQZPQ 4jrh9ee7CoX4NKI5lDZih.HaQQRe3y48b3gJeY9LuL183ZOvuBdOX1ruLe1L cSpFl0d+LuJz84knZc27xYUUXpvag4YB78Bc6+V4cnGpAEj5cknG.IfBbNoB UBj2lK+IZ6OsohPKwB8jAaabCeal5d+k9csvnhZx+h08J3wlkil0H5FdWq63 3ZoIgDDF8FNNWXVOQqUCDjjnt52dA7wtAgD42Rna6MfffUKWs.DEq6LzL7f9 ChTnWqrrO8lfDud1JEUgMXfSjq4+TfJI4dpm+04yUWVXIdKI6wKkKSUuZm+8 H9iS+Id7II5wfJnsUSShG1gMKXOuE.uMkLjD.ivEXjFgsbIIVSmUCfkXuGmH tzpEX9MXJJqD227pQ6wE2fDBNIqQfO7s5VzzxF0hurAy1z0bW68m9t0Sqb3n mUeKiKFgdG04RFcqs8sgRLr06UKS1LXODOXV3olmqECceblJigB7dGitGyEf JzV4uXSAFjgx+7EHdKzHSBSVYa.GbcndDwQZIUDbz.tUuHAbCg0+tISvQ4Bv FNqBnCR.BFfiqX6w.h3B.XXxZMtfImKfgoZ.a7LCB3HmB32gJyaJQBrjvRNK WpxelBxlMXNllKUwXwcXLsE8HZAfPkeJ8B2RpAUMkBxtRBl2MwkDJNm0PE8y 34RuSZnZ2ifTn0dmTi+Xs16DDnGSX3.dmPm5c9KbsjvW.QtZSXcVjyca6vTc Vjnw03ANkh+wktFHnI0Zju0ZxX3gAjNdEPPmxx+QUIvknLRidzZBlFe.fSri luyS3B1wtCyArM.n+KJKOQXZR6t7l5G8GEMvzWDzPw2Im8+GYdibYdRZDbNq c3PUYacE1s6SGFn2HYHzL1AO9cVYw2CXjlnbwLthvFFDLNCjG2HCQ2ZEKLoy Mm1XPVDMEKF4vH9OeBn2BfWGBHS59IDPAu.Bnq1HKy9Ys0XMAXftCLxyUH+i 7zi3BmLh6wuPnSKhT3wbN+nwi77cGqxXzB1SWEM5aAwa5szg9lc1SFiKo+Xm b1PhX80U9iAh0WGYle80PlYUPUv5oS.E+C9N6lbwPyQ6hSGS9rx0xmlprCuK gShIYSjJIJ6Ul6Sibp7wmA2rIpK75HpCe+Nt5nF+Lze4G9vheZC7Wd5JvACB sRz0criQUcmSA.5wqeqSey++DsQnZ+XtUyZ34clbWwqfClQAtVPn5Se1qSp2 mduNcKon.S6KAKH0JGXwvdPasmUVXNpZGAvQLmJRwNlTXW28JK80I7fgl2Mi olhitKzb.aUO6t6fQ7rs3zF9jqtHmAa0o3ri1WON+3I78OeVSpkViaXSrMtJ e2IcrvbV4LqI8HQw.vwcrwF3ni8biP9ajEiXO9tydlzcAcq8DXi83F+UnEtq TmYMqszZ9d7UlxWP61sGyqamRsgHKv6SLt513EyMk8ZtUOidb7dRW+WOWMae c9+gESF.w -----------end_max5_patcher-----------
This is awesome right there.
similar but using js:
----------begin_max5_patcher---------- 496.3oc2U0rTBCCD9b6SQlbBbPLofUza9.3S.xvj1FKAaS5jDTTFe2M+zhnV Fqf5Auzz7sa28a+5tIaBCfIh0TEDbEXJHHXSXPfCxBDTuO.VRVmVPTN2fohx RJWCG3sooq0N7JIMkoXBdiEVlCWjr7znnFv6DbMmTRcltVxHEasHySrnngnZ jJhNcAimO2DYsmh3KrlASbO8uGEMDAlU+I7UkLdAU6nJdmbpXO6xId71va7U rR23LxB9RXn8wfNJE2UHLAos5E800aKUWrqfvX+BpikWEQZRflJmS4jjBWpP emROpF0Coeph5ois9Hlez.XBgmCAyN.IxjoDprEIBO4fjn5FfZM57eOIxDEV o4m6Nd1QUiwOVMi9nQg97.F4dyvA.0lXN9X527KwegVF8cZov6SbNLEYuCZ3 QGQgGMxsb4+h4r16YVpzRVdNUBtE16FhdwPStDxdjonYfS.NjJwi8vnAjo3Y 8A8Am0F5svVD+3iQ68mtMdzE+fmg2wtNWlfEL9Gu3ywVK96EZkXkLsITMGuC divYTklwIZ6ceu4jclbGmVvxxn7c6YxXJaaT19u8oq7wdVZ23C9ugOi6.eh+ yjm3tnNiNP53alHUUOPkp5X5XhY9boPZ2FOHzegheqKhPI8AVi+SBsQ6kvWA GJXNT. -----------end_max5_patcher-----------
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.
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 ?-)
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.).
Peter, it depends on what you went to school for! :)
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. :)
Forums > MaxMSP