[issue41598] rnd() + rndup() in math

2020-08-22 Thread Vedran Čačić
Vedran Čačić added the comment: > use more digits to manage rounding in decimal base, not only one but more (i > should think better and experiment on how many) You don't have to. It's infinitely many. :-P Think, how many decimal digits would you need to accurately round numbers to a closest

[issue41598] rnd() + rndup() in math

2020-08-22 Thread marco_ocram
marco_ocram added the comment: i think in your long post you have underlined among others: 1. in binary implementation of floats one bit was reserved to rounding; 2. this one bit is not enough to manage a correct rounding in a converted decimal base; my considerations: 3. someone i think had e

[issue41598] rnd() + rndup() in math

2020-08-22 Thread Vedran Čačić
Vedran Čačić added the comment: I think you don't know what paraphrasing means. Or maybe I don't know it, but in any case, I'm well aware what Von Neumann said, and how it is usually misunderstood in today's society. "Living in the state of sin" is simply an emotionally powerful metaphor, and

[issue41598] rnd() + rndup() in math

2020-08-22 Thread marco_ocram
marco_ocram added the comment: @Vedran: I'm sorry about my "quick-and-dirty implementations". As i've already stated the problem's more deep i expect, despite the second half up rounding for my needs now (after your observation) work well. I've verified other languages have the same problem

[issue41598] rnd() + rndup() in math

2020-08-21 Thread Steven D'Aprano
Steven D'Aprano added the comment: Marco, it is better to give a description of the functionality required rather than a simplistic and incorrect implementation :-) (Not that I am likely to provide a better implementation without a lot of study.) Regardless of whether you or I agree with th

[issue41598] rnd() + rndup() in math

2020-08-21 Thread Steven D'Aprano
Steven D'Aprano added the comment: Vedran: you are quoting von Neumann out of context, he was talking about generating random numbers, not rounding, and in the seven decades since he made his famous witticism, we of course know that there is absolutely nothing wrong with generating random nu

[issue41598] rnd() + rndup() in math

2020-08-21 Thread Vedran Čačić
Vedran Čačić added the comment: Yes, and these functions are completely fine for your personal library, if you need such things. But they really have no place in math module, since (1) they aren't always correct, (2) it's incredibly difficult to characterize exactly when they are, and (3) eve

[issue41598] rnd() + rndup() in math

2020-08-21 Thread marco_ocram
marco_ocram added the comment: rndup is not correct and rnd work smooth only for numbers about < e16 until they have a fractional part. it's interesting but not simple. -- ___ Python tracker ___

[issue41598] rnd() + rndup() in math

2020-08-21 Thread marco_ocram
marco_ocram added the comment: thank you very much about the hints, i've improved the code as follows. def rnd(x, n=0): a = x*10**(n + 1) b = int(a) if abs(a - b) >= 0.5: b += 1 if x >= 0 else -1 a = b/10 b = int(a) if abs(a - b) >= 0.5: b += 1 if x >= 0 else -1 return b/1

[issue41598] rnd() + rndup() in math

2020-08-20 Thread Vedran Čačić
Vedran Čačić added the comment: Yes, we can do better than ms (whatever that means). And we do exactly that in the `decimal` module. Floats are not the right target for your algorithms, because they don't have decimal digits. It's as if you're trying to round words in English language. :-D (I

[issue41598] rnd() + rndup() in math

2020-08-20 Thread marco_ocram
marco_ocram added the comment: print(a := rnd(rnd(2.8-1.3, 15))) print(b := rnd(rnd(1.8-1.3, 15))) print(a == b) results ... 2.0 1.0 False it's the last significative digit that have problems, we could work on the code to correct this and experiment if someone is interested. by the way the

[issue41598] rnd() + rndup() in math

2020-08-20 Thread marco_ocram
marco_ocram added the comment: i want to be more clear, these could be useful. https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/round-function This VBA function returns something commonly referred to as bankers rounding. So be careful before using this functi

[issue41598] rnd() + rndup() in math

2020-08-20 Thread Vedran Čačić
Vedran Čačić added the comment: ROUND_HALF_UP is not chosen because it's popular, but because it's the best way to compensate for errors in representing decimal numbers by binary numbers. Thinking that floats are decimal numbers is going to bite you anyway sooner or later. For example, would

[issue41598] rnd() + rndup() in math

2020-08-20 Thread marco_ocram
marco_ocram added the comment: revision of "if you need to realize decimal.ROUND_DOWN function trunc is already fine." --> "if you need to realize decimal.ROUND_DOWN function int() or math.trunc() are already fine." -- ___ Python tracker

[issue41598] rnd() + rndup() in math

2020-08-20 Thread marco_ocram
marco_ocram added the comment: about floats ... https://docs.python.org/3/library/functions.html#round "if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2)" i'm sure lots of users don't need

[issue41598] rnd() + rndup() in math

2020-08-20 Thread Vedran Čačić
Vedran Čačić added the comment: If we want to proceed with this, much better way would be to add a rounding mode optional argument to `round` builtin. But really, anyone trying to precisely round a decimal representation of a binary floating point number is, to paraphrase von Neumann, living

[issue41598] rnd() + rndup() in math

2020-08-20 Thread Karthikeyan Singaravelan
Change by Karthikeyan Singaravelan : -- nosy: +mark.dickinson, rhettinger, stutzbach ___ Python tracker ___ ___ Python-bugs-list mai

[issue41598] rnd() + rndup() in math

2020-08-20 Thread Ronald Oussoren
Ronald Oussoren added the comment: Adding new functions that are extreme similar to existing functions requires a better rationale than your giving. In particular, both are similar to the builtin function "round" (https://docs.python.org/3/library/functions.html#round>). -- nosy: +r

[issue41598] rnd() + rndup() in math

2020-08-20 Thread marco_ocram
New submission from marco_ocram : hello, please insert following simple but useful roundings in new lib math. def rnd(x, n=0): a = x*10**n b = int(a) if abs(a - b) >= 0.5: b += 1 if x >= 0 else -1 return b/10**n def rndup(x, n=0): a = x*10**n b = int(a) if abs(a - b) > 0: