Bug 187650 - Number.toPrecision returns a wrong value
Summary: Number.toPrecision returns a wrong value
Status: RESOLVED WONTFIX
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: Safari 11
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-07-13 11:16 PDT by isol2
Modified: 2021-05-06 17:46 PDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description isol2 2018-07-13 11:16:43 PDT
Version: JavaScriptCore v233804
OS: Ubuntu 16.04 x64

Hi everyone,
I found an issue on a test case using Number.toPrecision that I don’t know if it is considered as an incompatibility issue between engines or a real bug example, but according with ES6 specs some values are described as "approximately" maybe this is a case of it. 
V8, JSC and SpiderMonkey returns the wrong value if param > 17, only Chakra works as expected.

Steps to reproduce:
print(123.56.toPrecision(18) === "123.560000000000000");
print(123.56.toPrecision(18))

Actual results:
False
123.560000000000002

Expected results:
True
123.560000000000000
Comment 1 Alexey Shvayka 2021-05-06 17:46:26 PDT
(In reply to isol2 from comment #0)
> I found an issue on a test case using Number.toPrecision that I don’t know
> if it is considered as an incompatibility issue between engines or a real
> bug example, but according with ES6 specs some values are described as
> "approximately" maybe this is a case of it. 

Thank you for report!

This is most certainly a IEEE 754 precision issue like 0.1 + 0.2 != 0.3.
The spec prose is worded to highlight the inaccuracy: please see step 10.a of https://tc39.es/ecma262/#sec-number.prototype.toprecision.