Comments (3)
@tonyofbyteball https://stackoverflow.com/questions/7223359/are-0-and-0-the-same
It's part of IEEE 754
. Most of the time you can ignore it because there is no difference in strict equality checks or addition/subtraction, etc. -0 === 0
and 1+ -0 === 1 + 0
etc.
The main difference in practical terms is when dividing or multiplying by zero as:
1/-0 === -Infinity
and 1/0 === Infinity
1 * -0; // -0
and 1 * 0; // 0
Of course, dividing anything by zero is problematic for other reasons anyway...
JS also returns false
for Object.is(-0,0)
.
The reason it is coming up at all is because JS doesn't actually have an explicit integer number type, as it uses doubles for everything. Even after ES6 introduced Number.isInteger
it still uses doubles for all "integers" - this breaks down a bit at 0
and outside Number.MIN_SAFE_INTEGER
and Number.MAX_SAFE_INTEGER
.
As a comparison, here is the output from my REPL in clojure (using the JVM which has both floats and integer number types):
boot.user=> (/ 1 0)
java.lang.ArithmeticException: Divide by zero
boot.user=> (/ 1 -0)
java.lang.ArithmeticException: Divide by zero
boot.user=> (/ 1 0.0)
Infinity
boot.user=> (/ 1 -0.0)
-Infinity
You can see there is a difference between signed 0 in doubles but dividing by integer 0 is always an exception as there isn't an integer negative zero AFAIK.
IMO the JVM behaviour of integers is usually what you want, dividing by the integer 0 is undefined in normal arithmetic and throwing an error is more helpful than returning +/-Infinity
to be propagated through the rest of your code :/
from ocore.
I didn't know about signed 0 until today. Where can we meet it?
from ocore.
So yeah... tbh it could go either way...
Argument for:
There is no difference between the integer represented by both -0
and 0
as there is only one integer 0
so therefore -0
is a negative double but not a negative integer, so therefore isNonnegativeInteger
should return true
.
Argument against:
JavaScript does not have any integers in reality, so following IEEE 754
there is a difference between -0
and 0
that will be apparent as soon as you multiply or divide anything by either of them. Even though we are willing to take ES6's lead on designating a subset of doubles as "integers" as per Number.isInteger
, and -0
is definitely one of these integers, it is also a negative value, so we cannot say that it is "non negative". Therefore isNonnegativeInteger
should return false
.
from ocore.
Related Issues (20)
- Question : what's the purpose of witness proof in catch up process ? HOT 1
- Catchup is slow when tps is more than 60 HOT 2
- "inputs and outputs do not balance" - composer and validator inconsistency HOT 2
- resend multi-sig wallet creation notification
- Question: Can user change a shared_address's definition by sending a definition_change unit ? HOT 1
- Question : Does 'send to address' make always new change address? HOT 1
- Error: Error: SQLITE_ERROR: too many SQL variables HOT 1
- "unstable unit not found: "+best_parent_unit HOT 1
- ParentFingerPrint argument is not a buffer HOT 2
- did not check whether the amount is real HOT 3
- iPhone & Apple watch notification
- Add method on the WS API to retrieve AA state vars? HOT 1
- backups not possible when node is only HOT 4
- Introduce logging framework HOT 10
- Introduce health check HOT 1
- Allow to externalize crypto operations to HSM HOT 4
- Fork byteball Tortik HOT 8
- 'wrong profile hash' message is shown incorrectly in live and testnet HOT 2
- Is there no way to set an alias for an asset? HOT 1
- ER_BAD_FIELD_ERROR: Unknown column 'getters' in 'field list' HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ocore.