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

Floating point problem in duration #5851

Open
awebow opened this issue Mar 14, 2021 · 1 comment
Open

Floating point problem in duration #5851

awebow opened this issue Mar 14, 2021 · 1 comment

Comments

@awebow
Copy link

awebow commented Mar 14, 2021

Describe the bug
Floating point error occurs when call .asWeeks() after call .subtract() with particular values.

To Reproduce

console.log(moment.duration(604800000).asWeeks()); // 1
console.log(moment.duration(1036800000).subtract(5, 'days').asWeeks()); // 0.9999999999999999
console.log(moment.duration(1036800000).subtract(5, 'days').asDays()); // 7

Expected behavior

console.log(moment.duration(604800000).asWeeks()); // 1
console.log(moment.duration(1036800000).subtract(5, 'days').asWeeks()); // 1
console.log(moment.duration(1036800000).subtract(5, 'days').asDays()); // 7

Desktop (please complete the following information):

  • OS: Windows 10
  • Version Node.js v12.13.1

Moment-specific environment

  • The time zone setting of the machine the code is running on
  • The time and date at which the code was run
  • Other libraries in use (TypeScript, Immutable.js, etc)

Please run the following code in your environment and include the output:

console.log((new Date()).toString())
console.log((new Date()).toLocaleString())
console.log((new Date()).getTimezoneOffset())
console.log(moment.version)
Mon Mar 15 2021 02:34:14 GMT+0900 (GMT+09:00)
2021-3-15 2:34:14
-540
2.29.1
@L0c-Nar
Copy link

L0c-Nar commented Sep 22, 2021

I also stumbled across the problem. The conversion in milliseconds is obviously affected as well. (See also #5596):
console.log(moment.duration('PT0.002635S').asMilliseconds()); // 2.6350000000000002

Moment.js 2.29.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants