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

hashCode for doubles should be better. #2884

Closed
DartBot opened this issue May 3, 2012 · 8 comments
Closed

hashCode for doubles should be better. #2884

DartBot opened this issue May 3, 2012 · 8 comments
Assignees
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. core-a P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented May 3, 2012

This issue was originally filed by @mdakin


What steps will reproduce the problem?

  1. Run http://try.dartlang.org/s/P6E_
  2. It seems hashCode for double numbers is only the integer part of the number.so hashCode of 0.1 and 0.9 are both 0.

What is the expected output? What do you see instead?
hashCode for different double numbers should be different.

@anders-sandholm
Copy link
Contributor

cc @floitschG.
Removed Type-Defect label.
Added Type-Enhancement, Area-Library, Triaged labels.

@anders-sandholm
Copy link
Contributor

Issue #2859 has been merged into this issue.

@DartBot DartBot added Type-Enhancement area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. labels May 8, 2012
@sethladd
Copy link
Contributor

sethladd commented Jun 4, 2015

This is still the case.

main() {
  print(0.2345.hashCode);  // 0
  print(1.9999.hashCode);  // 1
  print(1.00000.hashCode);  // 1
  print(123.456.hashCode);  // 123
}

@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed triaged labels Feb 29, 2016
@lrhn lrhn added the core-m label Aug 11, 2017
@floitschG floitschG added core-a and removed core-m labels Aug 31, 2017
@Hixie
Copy link
Contributor

Hixie commented Oct 17, 2017

This seems like it might cause problems in Flutter.
Many of our objects have hashCode getters that derive their hashCode from the hashCodes of their properties. Many of our objects have double properties. Many of them in particular have a double in the range 0.0..1.0, with no other changes.

For example, if you use FlutterLogoDecoration.lerp to fade in a logo, you'll get back a series of objects that are identical except for their opacity, which will vary from 0.0 to 1.0. Say you want to fade something in over 10 seconds. If you stick all of these objects in a Map for efficiency, you'll get hash collisions on all values except the last one, so you'll have one bucket with 599 objects and one bucket with 1 object.

@kasperl kasperl self-assigned this Oct 18, 2017
@kasperl
Copy link

kasperl commented Oct 18, 2017

Let me take a stab at this!

@kasperl
Copy link

kasperl commented Oct 19, 2017

Started working on this: https://dart-review.googlesource.com/c/sdk/+/15140.

main() {
  print(0.2345.hashCode);  // 4597616773615499378
  print(1.9999.hashCode);  // 4611685567917960216
  print(1.00000.hashCode);  // 1
  print(123.456.hashCode);  // 26701843266626392
}

@Hixie
Copy link
Contributor

Hixie commented Oct 19, 2017

I am so curious to see what impact this has on our benchmarks!

whesse pushed a commit that referenced this issue Oct 23, 2017
Preserves the property that doubles and ints that are equal
share the same hash code, but gives different doubles a much
higher chance of having different hash codes.

BUG=#2884

Change-Id: Ia97164087db568f26ffbbd6854ed9c19f81e9611
Reviewed-on: https://dart-review.googlesource.com/15140
Commit-Queue: Kasper Lund <[email protected]>
Reviewed-by: Vyacheslav Egorov <[email protected]>
@kasperl
Copy link

kasperl commented Oct 23, 2017

Fixed in 1db8615.

@kasperl kasperl closed this as completed Oct 23, 2017
dart-bot pushed a commit that referenced this issue Mar 23, 2021
New commits included:
```
> git log --format="%C(auto) %h %s" 0e657414a472e74ca5dd76ae0db50cc060251dec..255a3091fc278b04be74d246a3bec8743ef4d0b7
 255a3091 Vendor package:tar and package:chunked_stream (#2932)
 86bf8b20 Handle relative git-url-paths correctly when --directory (#2919)
 3716a681 Let `pub add` fail if extra arguments are passed (#2927)
 a03ac729 Minor cleanup to reduce risk of using path.current (#2924)
 e87b7b66 Added null check for name in UserInfo class (#2918)
 056a8c9a pub deps --json (#2896)
 53a69e27 Fix .packages entries of relative path deps when using --directory (#2916)
 d6308efc pub upgrade command shows count of discontinued packages (#2908)
 51744805 Upgrade to the null safe versions of all dependencies (#2913)
 e0d538c7 Introduce .pubignore (#2787)
 79f3a8b9 pub outdated: added clear message when no outdated packages. (#2898)
 22463872 `cache clean` (#2904)
 11e7b2ce `publish --dry-run` informs that the server might do more checks (#2883)
 b6977d50 Remove untrue assert (#2884)
 35841f8d Merge branch 'cherry_picks_for_2_12'
 0db3255b Don't fail on failed status listing (#2877)
 53e8ecca Don't allow outdated taking arguments (#2872)
 e83a1dc1 Enable asserts when testing pub (#2754)
 178f2edb Add --directory option (#2876)
 5aadb70e Don't fail on failed status listing (#2877)
 4bf8a927 Remove unused field (#2878)
 73ad5426 Don't allow outdated taking arguments (#2872)
 9a70949e Use Dart library to read and write tar files (#2817)
 2f74230c Do not recommend decativating packages (#2871)
 b1697a27 Use full error message string in CommandResolutionFailedException (#2870)
 16a6210d Upgrade `downgrade --help`: `downgrade` actually updates `pubspec.lock` (#2859)
 6e240ea9 Use cached version listings as heuristic when prefetching (#2851)
 58152f7c Allow trailing slash in PUB_HOSTED_URL (#2856)
 b1bf9a33 Handle poor package-listing responses robustly. (#2847)
 d941bd24 Fix request metadata when overriding dependencyType (#2848)
 ```

Change-Id: Id6ed5698330fc0cdb507e024eabc34f925ca9208
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/192304
Commit-Queue: Sigurd Meldgaard <[email protected]>
Reviewed-by: Jonas Jensen <[email protected]>
dart-bot pushed a commit that referenced this issue Mar 24, 2021
This reverts commit 633d4ac.

Reason for revert: breaks dependency resolution in some cases.

Original change's description:
> Bump pub
>
> New commits included:
> ```
> > git log --format="%C(auto) %h %s" 0e657414a472e74ca5dd76ae0db50cc060251dec..255a3091fc278b04be74d246a3bec8743ef4d0b7
>  255a3091 Vendor package:tar and package:chunked_stream (#2932)
>  86bf8b20 Handle relative git-url-paths correctly when --directory (#2919)
>  3716a681 Let `pub add` fail if extra arguments are passed (#2927)
>  a03ac729 Minor cleanup to reduce risk of using path.current (#2924)
>  e87b7b66 Added null check for name in UserInfo class (#2918)
>  056a8c9a pub deps --json (#2896)
>  53a69e27 Fix .packages entries of relative path deps when using --directory (#2916)
>  d6308efc pub upgrade command shows count of discontinued packages (#2908)
>  51744805 Upgrade to the null safe versions of all dependencies (#2913)
>  e0d538c7 Introduce .pubignore (#2787)
>  79f3a8b9 pub outdated: added clear message when no outdated packages. (#2898)
>  22463872 `cache clean` (#2904)
>  11e7b2ce `publish --dry-run` informs that the server might do more checks (#2883)
>  b6977d50 Remove untrue assert (#2884)
>  35841f8d Merge branch 'cherry_picks_for_2_12'
>  0db3255b Don't fail on failed status listing (#2877)
>  53e8ecca Don't allow outdated taking arguments (#2872)
>  e83a1dc1 Enable asserts when testing pub (#2754)
>  178f2edb Add --directory option (#2876)
>  5aadb70e Don't fail on failed status listing (#2877)
>  4bf8a927 Remove unused field (#2878)
>  73ad5426 Don't allow outdated taking arguments (#2872)
>  9a70949e Use Dart library to read and write tar files (#2817)
>  2f74230c Do not recommend decativating packages (#2871)
>  b1697a27 Use full error message string in CommandResolutionFailedException (#2870)
>  16a6210d Upgrade `downgrade --help`: `downgrade` actually updates `pubspec.lock` (#2859)
>  6e240ea9 Use cached version listings as heuristic when prefetching (#2851)
>  58152f7c Allow trailing slash in PUB_HOSTED_URL (#2856)
>  b1bf9a33 Handle poor package-listing responses robustly. (#2847)
>  d941bd24 Fix request metadata when overriding dependencyType (#2848)
>  ```
>
> Change-Id: Id6ed5698330fc0cdb507e024eabc34f925ca9208
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/192304
> Commit-Queue: Sigurd Meldgaard <[email protected]>
> Reviewed-by: Jonas Jensen <[email protected]>

[email protected],[email protected]

Change-Id: I364a26468a36438bbbc887e613241030decef4d3
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/192748
Reviewed-by: Alexander Thomas <[email protected]>
Commit-Queue: Alexander Thomas <[email protected]>
dart-bot pushed a commit that referenced this issue May 11, 2021
New commits include:
```
$ git log --format="%C(auto) %h %s" 0e657414a472e74ca5dd76ae0db50cc060251dec..00c00e8adf9706bebe8f94483b7663c5f36f59d2
 00c00e8a Vendor tar (#2987)
 291705ca Being gradual migration to null-safety (#2988)
 c5f52a37 Fix CI (#2989)
 74040a45 Update to analyzer 1.5.0, migrate from deprecated AnalysisSession.getParsedUnit() (#2975)
 ce951d70 Fix dry-run tests for publishing  by ensuring there is a server to reject requests (#2978)
 018c9650 Update LICENSE (#2944)
 2614f15c Revert "Vendor package:tar and package:chunked_stream (#2932)" (#2940)
 12d9f457 Handle package:tar cancellations
 255a3091 Vendor package:tar and package:chunked_stream (#2932)
 86bf8b20 Handle relative git-url-paths correctly when --directory (#2919)
 3716a681 Let `pub add` fail if extra arguments are passed (#2927)
 a03ac729 Minor cleanup to reduce risk of using path.current (#2924)
 e87b7b66 Added null check for name in UserInfo class (#2918)
 056a8c9a pub deps --json (#2896)
 53a69e27 Fix .packages entries of relative path deps when using --directory (#2916)
 d6308efc pub upgrade command shows count of discontinued packages (#2908)
 51744805 Upgrade to the null safe versions of all dependencies (#2913)
 e0d538c7 Introduce .pubignore (#2787)
 79f3a8b9 pub outdated: added clear message when no outdated packages. (#2898)
 22463872 `cache clean` (#2904)
 11e7b2ce `publish --dry-run` informs that the server might do more checks (#2883)
 b6977d50 Remove untrue assert (#2884)
 35841f8d Merge branch 'cherry_picks_for_2_12'
 0db3255b Don't fail on failed status listing (#2877)
 53e8ecca Don't allow outdated taking arguments (#2872)
 e83a1dc1 Enable asserts when testing pub (#2754)
 178f2edb Add --directory option (#2876)
 5aadb70e Don't fail on failed status listing (#2877)
 4bf8a927 Remove unused field (#2878)
 73ad5426 Don't allow outdated taking arguments (#2872)
 9a70949e Use Dart library to read and write tar files (#2817)
 2f74230c Do not recommend decativating packages (#2871)
 b1697a27 Use full error message string in CommandResolutionFailedException (#2870)
 16a6210d Upgrade `downgrade --help`: `downgrade` actually updates `pubspec.lock` (#2859)
 6e240ea9 Use cached version listings as heuristic when prefetching (#2851)
 58152f7c Allow trailing slash in PUB_HOSTED_URL (#2856)
 b1bf9a33 Handle poor package-listing responses robustly. (#2847)
 d941bd24 Fix request metadata when overriding dependencyType (#2848)
```

Change-Id: Id7cc4c09e74c02a92bcafe1a9d9bab9431900540
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/199040
Reviewed-by: Alexander Thomas <[email protected]>
Commit-Queue: Jonas Jensen <[email protected]>
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. core-a P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

8 participants