#πŸ”’ πŸ”’ zero rounding of float

68 messages Β· Page 1 of 1 (latest)

stray frost
#

rounding
a = round(-0.0001,2)
print(a)
-0.0

if there a way take advantage of this and test if it really is a round from a negative number with standard math or logical ? ....
maybe converting it in to string and analysing it...

mental hawkBOT
#

@stray frost

Python help channel opened

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.

deft granite
#

I don't really understand your question, what do you mean? Test if it's close to something?

grizzled drift
#

Not sure about what you are trying to achieve there, what are you trying to do exactly ?

#

Damn, mods are fast

stray frost
#

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 =

deft granite
#

So you want rounding, but negative zero turned to positive zero?

stray frost
#

? 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

deft granite
#

Ah, and you think you can't because 0.0 == -0.0

stray frost
#

and the 0.0 separatly

#

-0.0 < 0 False

grizzled drift
#

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 ?

stray frost
#

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

grizzled drift
#

In that case, is there anything preventing you from checking the number before it gets rounded then ?

stray frost
#

yes i need to rewrite alot of stuff for that

grizzled drift
#

Erm

stray frost
#

I think it is a python behavior not a float behavior ...

grizzled drift
#

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

stray frost
#

they define -0.0 and +0.0 ?

grizzled drift
#

Yup, you can read more on that wikipedia page

deft granite
#

Yes, IEEE 754 has a few quirks.

stray frost
#

but there is no way to test it

grizzled drift
#

As for your project I'd have advised for a huge rewrite

deft granite
#

str(number).startswith("-") :P

grizzled drift
#

But if you relly need to finish it, look into math.copysign

grizzled drift
radiant robin
#

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
deft granite
stray frost
#

in my present case I will hack to string ....

radiant robin
#

Or even:

def isnegzero(n):
    return n is -0.0
grizzled drift
#

isn't copysign able to detect negative zeros ?

deft granite
#

You can't reliably use is with floats.

radiant robin
stray frost
#

because I can get a bit more info by using the "sign

steady jolt
#

I'd do x==0 and math.copysign(1, x) == -1

deft granite
mental hawkBOT
steady jolt
#

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

deft granite
stray frost
#

print(math.copysign(1, -0.0))

#

gives -1

grizzled drift
#

Sounds good, now you can check with a standard inequality

stray frost
#

interesting kirky thing ....

#

... yeah it might not be for all implementation of python

#

thanks all πŸ™‚

grizzled drift
#

0.0 is the same as +0.0 for copysign, so you should only get 1(.0 ?) or -1(.0 ?) from it

stray frost
#

ya I will used copysign ....

grizzled drift
#

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

stray frost
#

there is a pep about this

#
#

is it release i didnt try ...

#

thanks all

mental hawkBOT
#
Python help channel closed

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

mental hawkBOT
#
Python help channel closed

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.