Comments (19)
This comment was originally written by [email protected]
This is standard IEE 754 behavior: 1/0 = +Infinity, -1/0 = -Infinity, Infinity/Infinity = NaN. Dart isn't doing anything differently here than either Java or JavaScript.
from sdk.
This comment was originally written by [email protected]
(IEE ==> IEEE)
from sdk.
This comment was originally written by [email protected]
You mean just Javascript.
Even though the following does produce Infinity and NaN:
<html>
<body>
<script>
alert(1/0);
alert((1/0)/(1/0));
</script>
</body>
</html>
It is invalid in Java, e.g.:
public class Test {
public static void main(String args[]) {
System.out.println( "" + (1/0));
System.out.println( "" + ((1/0)/(1/0)));
}
}
results in:
$ java Test
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Test.main(Test.java:3)
Still, Dart is a new language. Couldn't it aspire to get math right instead of being just another Javascript?
Thanks!
from sdk.
This comment was originally written by [email protected]
Your Java example is dividing ints, not (IEEE 754) floats or doubles. Please replace 1 with 1.0 and 0 with 0.0 and give your code another try.
from sdk.
This comment was originally written by [email protected]
IEEE 754 is what hardware implements -- so adopting a different set of semantics for floating-point numbers means relying on software to do the heavy lifting, and puts code at a big disadvantage versus code that is designed to take advantage of what the floating-point hardware can do.
I thing there is a separate issue of how dividing ints should behave in Dart -- note that the / operator produces a num, not an int, even when dividing two ints. For example, 1/2 = 0.5, like JavaScript, not 0 as in Java. There is a ~/ operator that truncates to int. Currently, (1 ~/ 0) also evaluates to Infinity -- this seems like it would be worth taking up with the spec folks.
from sdk.
This comment was originally written by [email protected]
Yes, for Float and Double, but fails again with BigDecimal:
import java.math.BigDecimal;
public class Test {
public static void main(String args[]) {
System.out.println( "" + (1F/0F));
System.out.println( "" + ((1F/0F)/(1F/0F)));
System.out.println( "" + (1D/0D));
System.out.println( "" + ((1D/0D)/(1D/0D)));
System.out.println( "" + (new BigDecimal(1D).divide(new BigDecimal(0D), BigDecimal.ROUND_UNNECESSARY)));
System.out.println( "" + ((new BigDecimal(1D).divide(new BigDecimal(0D), BigDecimal.ROUND_UNNECESSARY)).divide(new BigDecimal(1D).divide(new BigDecimal(0D), BigDecimal.ROUND_UNNECESSARY), BigDecimal.ROUND_UNNECESSARY)));
}
}
Produces:
$ java Test
Infinity
NaN
Infinity
NaN
Exception in thread "main" java.lang.ArithmeticException: / by zero
at java.math.BigDecimal.divide(BigDecimal.java:1327)
at java.math.BigDecimal.divide(BigDecimal.java:1444)
at Test.main(Test.java:9)
But it looks like Ruby 1.8.7 still does what you are saying even with BigDecimal:
$ irb
1/0
ZeroDivisionError: divided by 0
from (irb):1:in `/'
from (irb):1
1.0/0.0
=> Infinity
(1.0/0.0)/(1.0/0.0)
=> NaN
require 'bigdecimal'
BigDecimal("1.0") / BigDecimal("0.0")
=> #<BigDecimal:100605f80,'Infinity',4(24)>
(BigDecimal("1.0") / BigDecimal("0.0")) / (BigDecimal("1.0") / BigDecimal("0.0"))
=> #<BigDecimal:1005fbff8,'NaN',4(56)>
Oh, well. It's a shame that we still settle for that. :)
from sdk.
This comment was originally written by [email protected]
You can close this if you want. Sorry to bother about it.
from sdk.
This comment was originally written by [email protected]
Oops, just saw your other comment. Yes, feel free to take up with the spec folks about ints being treated as floating points. Glad I could be of limited assistance.
from sdk.
DartC (the compiler from Dart to JavaScript) is not spec-compliant with regards to numbers. We compile dart numbers to JavaScript numbers (thus losing the distinction between ints and doubles).
dart: x / 0 throws a DivisionByZeroException in correct Dart. In dart->Js code it unfortunately becomes the equivalent of x / 0.0 which becomes infinity/nan.
from sdk.
This comment was originally written by [email protected]
Added Area-Language, Triaged labels.
from sdk.
The spec was carefully thought out and it describes the intended semantics of Dart. We make allowances for dartC, and maybe it is unreasonable to do the right thing and still be performant. But AFIK, this isn't a spec issue unless we want to enshrine special dispensation for dartC in the spec.
Removed Area-Language label.
Added Area-Compiler label.
Changed the title to: "dartC compiles both ints and doubles into Javascript numbers".
from sdk.
This comment was originally written by [email protected]
To me, it seems that JS is correct here. 1/0 is infinity. infinity / infinity is undefined, a.k.a. NaN.
from sdk.
This comment was originally written by [email protected]
As Florian said, the original target for the java-based dartc was to compile dart numbers to JS numbers.
The only thing that we can do here is to decide whether it is worth checking for division by zero in the current dartc or not -- I changed the description accordingly.
Changed the title to: "dartc does not generate DivisionByZeroException errors".
from sdk.
This comment was originally written by [email protected]
Unassigned from area=compiler. This bug is no longer valid for dartc, but I believe is still a general dart to Javsscript translation issue. If not, please close.
Removed Area-Compiler label.
Added New label.
from sdk.
Added Area-Dart2JS, Triaged labels.
from sdk.
Changed the title to: "Make sure dart2js generates DivisionByZeroException errors".
from sdk.
Update summary to reflect that we haven't decided yet.
Changed the title to: "Decide if we want to generate DivisionByZeroException errors".
from sdk.
Added this to the Later milestone.
from sdk.
Removed this from the Later milestone.
from sdk.
Related Issues (20)
- Inconsistent behavior with case-insensitive file imports HOT 5
- WASM - Faster Native Uint8List HOT 3
- Error while processing the analysis.setAnalysisRoots request HOT 1
- Analyzer Feedback from IntelliJ HOT 1
- The Dart VM does not report correct source of error/exception from the dart:io HOT 4
- Why isn't a WeakReference not garbage collected although its Retaining Path is just `ObjectPool` HOT 14
- Analysis server crash HOT 2
- A macro cannot augment the same class twice HOT 1
- Analyzer Feedback from IntelliJ HOT 1
- [CP] Fix crashes due to unhandled StructuralParameterTypes in UP in the CFE HOT 6
- [CP] [dart2wasm] Fix partial instantiation constants HOT 5
- "http/browser_client.dart" import causes tests to fail because of "http-1.2.2/lib/src/browser_client.dart:6:8: Error: Dart library 'dart:js_interop' is not available on this platform." HOT 3
- [Wildcard Variables] implement `THIS_ACCESS_FROM_FIELD_INITIALIZER` HOT 2
- dart2js: Internal Error: type 'InterfaceType' is not a subtype of type 'FunctionType' in type cast HOT 3
- DateTime.tryParse non consistent behavior across platforms HOT 1
- [Wildcard Variables][analyzer] Const wildcard variables have no errors when referenced HOT 2
- [Wildcard Variables][analyzer] Const wildcard functions have no errors when referenced HOT 1
- Internal error: Exception while using a AvoidRedundantArgumentValues to visit... HOT 2
- [Wildcard Variables][analyzer] Const wildcard initializer list variables should not accessed. HOT 1
- unintended_html_in_doc_comment triggers on some variants of allowed html tags in package:web 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 sdk.