Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decide if we want to generate DivisionByZeroException errors #26

Closed
DartBot opened this issue Oct 10, 2011 · 19 comments
Closed

Decide if we want to generate DivisionByZeroException errors #26

DartBot opened this issue Oct 10, 2011 · 19 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-as-intended Closed as the reported issue is expected behavior

Comments

@DartBot
Copy link

DartBot commented Oct 10, 2011

This issue was originally filed by [email protected]


Dart should not report a number divided by zero as infinity. And if infinity is allowed, math using infinity should be correct.

What steps will reproduce the problem?

  1. Use the following code:

main() {
  print(1 / 0);
  print((1 / 0) / (1 / 0));
}

What is the expected output? What do you see instead?

It should report:

NaN
NaN

Instead it reports:

Infinity
NaN

What version of the product are you using? On what operating system?

Using the following on Oct 10, 2011 15:50 EDT

http://www.dartlang.org/docs/getting-started/

Please provide any additional information below.

@DartBot
Copy link
Author

DartBot commented Oct 10, 2011

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.

@DartBot
Copy link
Author

DartBot commented Oct 10, 2011

This comment was originally written by [email protected]


(IEE ==> IEEE)

@DartBot
Copy link
Author

DartBot commented Oct 10, 2011

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!

@DartBot
Copy link
Author

DartBot commented Oct 10, 2011

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.

@DartBot
Copy link
Author

DartBot commented Oct 10, 2011

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.
 

@DartBot
Copy link
Author

DartBot commented Oct 10, 2011

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

@DartBot
Copy link
Author

DartBot commented Oct 10, 2011

This comment was originally written by [email protected]


You can close this if you want. Sorry to bother about it.

@DartBot
Copy link
Author

DartBot commented Oct 10, 2011

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.

@floitschG
Copy link
Contributor

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.

@DartBot
Copy link
Author

DartBot commented Oct 11, 2011

This comment was originally written by [email protected]


Added Area-Language, Triaged labels.

@gbracha
Copy link
Contributor

gbracha commented Oct 12, 2011

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

@DartBot
Copy link
Author

DartBot commented Oct 24, 2011

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.

@DartBot
Copy link
Author

DartBot commented Oct 25, 2011

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

@DartBot
Copy link
Author

DartBot commented Mar 28, 2012

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.

@anders-sandholm
Copy link
Contributor

Added Area-Dart2JS, Triaged labels.

@kasperl
Copy link

kasperl commented Apr 1, 2012

Changed the title to: "Make sure dart2js generates DivisionByZeroException errors".

@kasperl
Copy link

kasperl commented Apr 1, 2012

Update summary to reflect that we haven't decided yet.


Changed the title to: "Decide if we want to generate DivisionByZeroException errors".

@kasperl
Copy link

kasperl commented Sep 3, 2012

Added this to the Later milestone.

@kasperl
Copy link

kasperl commented Oct 17, 2012

Removed this from the Later milestone.

@DartBot DartBot added Type-Defect area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-as-intended Closed as the reported issue is expected behavior labels Oct 17, 2012
@DartBot DartBot added this to the Later milestone Oct 17, 2012
copybara-service bot pushed a commit that referenced this issue Jul 21, 2023
Here's a minimal repro that this CL fixes:

`ui.dart`

```dart
library dart.ui;

import 'dart:ffi';

part 'foo.dart';
```

`foo.dart`

```dart
part of dart.ui;

@Native<Void Function()>(symbol: 'foo_func', isLeaf: true)
external void foo_func();
```

When compiling with `compile_platform.dart` with `--target=dart2wasm`, the following error appears:


```
Unhandled exception:
Verification error: Target=wasm, VerificationStage.afterModularTransformations: Invalid location with target 'wasm' on FunctionNode() (FunctionNode): RangeError (offset): Invalid value: Not in inclusive range 0..56: 91
Context: 'foo_func_$import'.
Node: 'FunctionNode()'.
#0      VerificationErrorListener.reportError (package:kernel/verifier.dart:81:5)
#1      VerifyingVisitor.problem (package:kernel/verifier.dart:222:14)
#2      VerifyingVisitor._getLocation (package:kernel/verifier.dart:1361:7)
#3      VerifyingVisitor._hasLocation (package:kernel/verifier.dart:1370:26)
#4      VerifyingVisitor.getSameLibraryLastSeenTreeNode (package:kernel/verifier.dart:1342:28)
#5      VerifyingVisitor.localContext (package:kernel/verifier.dart:1382:24)
#6      VerifyingVisitor.defaultDartType (package:kernel/verifier.dart:1491:41)
#7      Visitor.visitVoidType (package:kernel/visitor.dart:1309:37)
#8      VoidType.accept (package:kernel/ast.dart:11190:42)
#9      FunctionNode.visitChildren (package:kernel/ast.dart:3919:16)
#10     VerifyingVisitor.visitChildren (package:kernel/verifier.dart:259:10)
#11     VerifyingVisitor.visitWithLocalScope (package:kernel/verifier.dart:266:5)
#12     VerifyingVisitor.visitFunctionNode (package:kernel/verifier.dart:721:5)
#13     FunctionNode.accept (package:kernel/ast.dart:3908:38)
#14     VerifyingVisitor.visitProcedure (package:kernel/verifier.dart:620:19)
#15     Procedure.accept (package:kernel/ast.dart:3311:40)
#16     visitList (package:kernel/ast.dart:14488:14)
#17     Library.visitChildren (package:kernel/ast.dart:591:5)
#18     VerifyingVisitor.visitChildren (package:kernel/verifier.dart:259:10)
#19     VerifyingVisitor.defaultTreeNode (package:kernel/verifier.dart:196:5)
#20     TreeVisitor.visitLibrary (package:kernel/visitor.dart:503:35)
#21     VerifyingVisitor.visitLibrary (package:kernel/verifier.dart:367:11)
#22     Library.accept (package:kernel/ast.dart:577:38)
#23     visitList (package:kernel/ast.dart:14488:14)
#24     Component.visitChildren (package:kernel/ast.dart:14320:5)
#25     VerifyingVisitor.visitChildren (package:kernel/verifier.dart:259:10)
#26     VerifyingVisitor.visitComponent (package:kernel/verifier.dart:342:7)
#27     Component.accept (package:kernel/ast.dart:14313:38)
#28     VerifyingVisitor.check (package:kernel/verifier.dart:171:15)
#29     verifyComponent (package:kernel/verifier.dart:69:20)
...
```

The issue seems to be that after doing this native transformation, the node's `fileUri` references the enclosing library (`ui.dart` above), but the `node.location` references the actual source file (`foo.dart` above) indirectly through `node.fileOffset`.

This ends up being an issue when compiling the platform dill in Google3,   but I didn't look into why `flutter build web --wasm` isn't broken.

Internal bug: b/292172146

Change-Id: I2b8d7d215b2c36354860257ce651d50168e9523d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/315360
Reviewed-by: Ömer Ağacan <[email protected]>
Commit-Queue: Jia Hao Goh <[email protected]>
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-as-intended Closed as the reported issue is expected behavior
Projects
None yet
Development

No branches or pull requests

5 participants