marco_ocram added the comment:
"- What would you expect round(2.675, ndigits=2, mode=ROUND_HALF_UP) to give? I
strongly suspect that Marco would expect and want a result of 2.68. But if we
follow the existing rules for round, it's going to give 2.67."
Yes, that's right.
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
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 la
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
<https://bugs.python.org
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 el
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 wa
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
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."
--
___
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
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 -
10 matches
Mail list logo