1

Running Math.PI.toString(10) in various JS engines seems to consistently output "3.141592653589793", which has 16 significant digits. Some other examples with 16 significant digits, tested in Chrome:

(Math.PI * 10).toString(10); // "31.41592653589793"
(Math.PI * 100).toString(10); // "314.1592653589793"
(Math.PI / 10000000).toString(10); // "3.141592653589793e-7"

However:

(Math.PI * 10000).toString(10); // "31415.926535897932"
(Math.PI / 100).toString(10); // "0.031415926535897934"

Is this standardized, or left to each engine to decide? What does the standard say?

last-child
  • 4,481
  • 1
  • 21
  • 19
  • Why you are using the paramater `10` on the `toString()` method ? i dont understand, i thaugh it is to specify the base. – Dahou Jun 05 '19 at 22:12
  • 1
    If you want to know what the spec says, it's not hard to find [toString Spec](https://www.ecma-international.org/ecma-262/6.0/#sec-tostring) – Chase Jun 05 '19 at 22:14
  • @dahou Because my question is explicitly about base 10. – last-child Jun 05 '19 at 22:15
  • I think chase led you to the right place - and the radix is not relevant here because 10 is the default anyway. – Randy Casburn Jun 05 '19 at 22:16
  • @Chase thank you. That is quite dense to read though, so it does not help me understand it. But at least it suggests that this should be reliable across different JS engines. – last-child Jun 05 '19 at 22:18
  • Unless you plan on reading the [V8 Source Code](https://chromium.googlesource.com/v8/v8.git/), I'm not sure what you were expecting. That spec is what the engine designers read when they build an engine. – Chase Jun 05 '19 at 22:30
  • 1
    @Chase: What they were expecting is a statement like “x is the smallest member of the set” instead of “x is the number such that for every y in the set, x ≤ y”. In other words, an intuitive description in natural language instead of a precise but technical specification in formal terminology and notation. – Eric Postpischil Jun 05 '19 at 22:44
  • @EricPostpischil thank you for the duplicate reference, it has an absolutely excellent answer (https://stackoverflow.com/a/49386744/) – last-child Sep 25 '19 at 09:02

0 Answers0