#π π zero rounding of float
68 messages Β· Page 1 of 1 (latest)
@stray frost
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
I don't really understand your question, what do you mean? Test if it's close to something?
Not sure about what you are trying to achieve there, what are you trying to do exactly ?
Damn, mods are fast
in some case rouding from negative number give zerro doesnt have the same meaning from rounding a positive number to zero.
it seems float can give -0.0 from a negative number
but -0.0 == 0.0
if I used =
So you want rounding, but negative zero turned to positive zero?
? in some case rouding from negative number give zerro doesnt have the same meaning from rounding a positive number to zero.
I want yo detct the -0.0
Ah, and you think you can't because 0.0 == -0.0
The real question is why you would like to detect it thought, is it just curiosity ? Or what is the actual use case behind this ?
n some case rouding from negative number give zerro doesnt have the same meaning from rounding a positive number to zero.
on you thermometer, you see some time -0 so it might means the real value is -0.001
not 0 or +0.001
so it might freeze or not ...
In that case, is there anything preventing you from checking the number before it gets rounded then ?
yes i need to rewrite alot of stuff for that
Erm
I think it is a python behavior not a float behavior ...
It's a float behaviour in general, it's defined by a standard
Signed zero is zero with an associated sign. In ordinary arithmetic, the number 0 does not have a sign, so that β0, +0 and 0 are equivalent. However, in computing, some number representations allow for the existence of two zeros, often denoted by β0 (negative zero) and +0 (positive zero), regarded as equal by the numerical comparison operations ...
they define -0.0 and +0.0 ?
Yup, you can read more on that wikipedia page
Yes, IEEE 754 has a few quirks.
but there is no way to test it
As for your project I'd have advised for a huge rewrite
str(number).startswith("-") :P
But if you relly need to finish it, look into math.copysign
hacky but probably simpler haha
Just dug through the math module docs. It doesn't seem to offer a way to detect -0.0 vs 0.0. Though 0.0 == -0.0 and 0.0 is not -0.0
This relies on 0.0 and -0.0 being interned though.
Assuming that, try:
def isnegzero(n):
return n == 0.0 and n is not 0.0
They're not singletons though
in my present case I will hack to string ....
Or even:
def isnegzero(n):
return n is -0.0
isn't copysign able to detect negative zeros ?
You can't reliably use is with floats.
Can you demonstrate that? I'll try.
because I can get a bit more info by using the "sign
I'd do x==0 and math.copysign(1, x) == -1
!e
x = 0.
y = 3 / float("inf")
print(f"{x is y=}")
:white_check_mark: Your 3.12 eval job has completed with return code 0.
x is y=False
On platforms that support signed zeros, copysign(1.0, -0.0) returns -1.0.
Notes like these always scare and confuse me. What kind of platform can you run python on where signed zeros don't exist??
Thank you.
ints? π
None that CPython runs on, but Python-the-language doesn't require IEEE 754.
Sounds good, now you can check with a standard inequality
interesting kirky thing ....
... yeah it might not be for all implementation of python
thanks all π
0.0 is the same as +0.0 for copysign, so you should only get 1(.0 ?) or -1(.0 ?) from it
ya I will used copysign ....
I was wondering if you could somehow get "no sign" (i.e. 0 as an output) in this case, but it makes sense that it's not possible here
there is a pep about this
As the designated PEP-Delegate for John Belmonteβs PEP 682, Iβm hereby accepting PEP 682 for inclusion into Python, targeting Python 3.11. Iβd like to thank John for a well-written PEP, and for his patience with the PEP process. Iβd also like to thank the PEP editors for their invaluable help with the process. I look forward to a slightly more h...
is it release i didnt try ...
thanks all
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.
π zero rounding of float
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.