Skip to content

parseFloat drops powers of 10 for 17+ digit significands with exponents 23..37 #3060

Open

Description

@trackoor

Bug description

parseFloat, Number.parseFloat, and f64.parse are off by up to 10^15 when the parsed integer significand exceeds 2^53 - 1 (17+ significant digits) and the remaining decimal exponent is in 23..37. This also breaks parseFloat(x.toString()) round-tripping in that range.

In scientific, when exp > 22 && exp <= 22 + 15, significandf is scaled and exp is set to 22 before checking significand <= 9007199254740991, so the fallback scaleup(significand, exp) receives the unscaled integer significand with exp = 22.

Steps to reproduce

export function a(): f64 { return parseFloat("1.2345678901234567e+40"); }
export function b(): f64 { return parseFloat("12345678901234567e24"); }
export function c(): f64 { return f64.parse("4.9999999999999998e+39"); }
export function d(): f64 { return parseFloat("100000000000000000000000000000000000000000"); }

Actual return values:

  • a(): 1.2345678901234567e+38
  • b(): 1.2345678901234567e+38
  • c(): 5e+38
  • d(): 1e+40

Expected return values (matching JavaScript parseFloat):

  • a(): 1.2345678901234566e+40
  • b(): 1.2345678901234566e+40
  • c(): 4.9999999999999995e+39
  • d(): 1e+41

AssemblyScript version

v0.28.20

Credit: https://ucsbplse.github.io/

Playground

https://www.assemblyscript.org/editor.html#IyFvcHRpbWl6ZT1zcGVlZCZydW50aW1lPWluY3JlbWVudGFsCmV4cG9ydCBmdW5jdGlvbiBhKCk6IGY2NCB7IHJldHVybiBwYXJzZUZsb2F0KCIxLjIzNDU2Nzg5MDEyMzQ1NjdlKzQwIik7IH0KZXhwb3J0IGZ1bmN0aW9uIGIoKTogZjY0IHsgcmV0dXJuIHBhcnNlRmxvYXQoIjEyMzQ1Njc4OTAxMjM0NTY3ZTI0Iik7IH0KZXhwb3J0IGZ1bmN0aW9uIGMoKTogZjY0IHsgcmV0dXJuIGY2NC5wYXJzZSgiNC45OTk5OTk5OTk5OTk5OTk4ZSszOSIpOyB9CmV4cG9ydCBmdW5jdGlvbiBkKCk6IGY2NCB7IHJldHVybiBwYXJzZUZsb2F0KCIxMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAiKTsgfQojIWh0bWwKPHByZSBpZD0ib3V0Ij48L3ByZT4KPHNjcmlwdCB0eXBlPSJtb2R1bGUiPgpjb25zdCBlID0gYXdhaXQgaW5zdGFudGlhdGUoYXdhaXQgY29tcGlsZSgpLCB7fSk7CmRvY3VtZW50LmdldEVsZW1lbnRCeUlkKCJvdXQiKS50ZXh0Q29udGVudCA9IFsKICAiYSgpID0gIiArIGUuYSgpICsgIiAgKGV4cGVjdGVkIDEuMjM0NTY3ODkwMTIzNDU2NmUrNDApIiwKICAiYigpID0gIiArIGUuYigpICsgIiAgKGV4cGVjdGVkIDEuMjM0NTY3ODkwMTIzNDU2NmUrNDApIiwKICAiYygpID0gIiArIGUuYygpICsgIiAgKGV4cGVjdGVkIDQuOTk5OTk5OTk5OTk5OTk5NWUrMzkpIiwKICAiZCgpID0gIiArIGUuZCgpICsgIiAgKGV4cGVjdGVkIDFlKzQxKSIKXS5qb2luKCJcbiIpOwo8L3NjcmlwdD4=

Click Run: each line shows the actual value next to the expected one; all four are off by a power of 10.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions