Re: [svgwg] the float values of polygon.points and polygon.getAttribute('points') are different (#1023)

`.getAttribute()` isn't returning floats, it's just returning a string, exactly the characters you typed in. There's no parsing, math, or numbers involved.

`.points()` is parsing the value and storing it as numbers internally, then returning it to you as JS Numbers. Numbers in computers are almost never stored as decimal numbers, like we write them; they're usually binary floating point; for JS, specifically, Numbers are 64-bit floats. However, if you parse a decimal number into a float then serialize it back out to a decimal string, you should always get the same value back. (The rules for how to print a float as a decimal are very carefully designed to make this true: when you parse "0.1", you get the floating point value *closest* to 0.1, and when you serialize that particular value back to decimal, it is guaranteed to serialize back to "0.1". The rules basically guarantee that a floating point will serialize to the *shortest possible decimal number* that it's closer to than any other floating point number is.)

What's probably happening here is that, internally, the browser isn't storing the points array as 64-bit floats. I'd guess it's instead using 32-bit floats, which can't represent as many values, so when it gets turned into a JS Number to return to you, it's *not* actually the 64-bit floating point value closest to 0.1; instead, it's a number a little larger. So when you ask it to serialize, it doesn't serialize as "0.1" (because there's *another* Number that serializes to that, instead), it instead serializes to that big long *almost 0.1* string you see, which is the most accurate representation it can make.

(The link provided by @longsonr isn't directly relevant to this question, as that's about why floating point *math* doesn't always match decimal math. But it does provide some useful background details about how floating point numbers work *in general*, which might help you understand what's going on here.)

-- 
GitHub Notification of comment by tabatkins
Please view or discuss this issue at https://github.com/w3c/svgwg/issues/1023#issuecomment-3403021278 using your GitHub account


-- 
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config

Received on Tuesday, 14 October 2025 18:00:26 UTC