W3C home > Mailing lists > Public > public-html@w3.org > December 2008

Parsing floats

From: Philip Taylor <pjt47@cam.ac.uk>
Date: Mon, 29 Dec 2008 23:18:50 +0000
Message-ID: <49595ADA.5020004@cam.ac.uk>
To: HTML WG <public-html@w3.org>

The definition of "valid floating point number" does not allow numbers 
like "1e+10", and the "rules for parsing floating point number values" 
will not accept the "+" and will parse it into the number 1.

Many (most? all?) programming languages serialise large floating-point 
numbers using "+". So if I write something straightforward in Perl like:

   $total_disk_space = 2**50;
   $used_disk_space = 819845012984561;
   print qq{Disk usage: <meter min="0" max="$total_disk_space" 

then I'll get <meter min="0" max="1.12589990684262e+15" 
value="819845012984561">, which will work very badly since 'max' won't 
be parsed as expected. The same kind of problem can occur in Python, like:

   total_disk_space = 2.0**50
   used_disk_space = 819845012984561.0
   print 'Disk usage: <meter min="0" max="%s" value="%s"></meter>' % 
(total_disk_space, used_disk_space)

which gives <meter min="0" max="1.12589990684e+15" 

The same happens in JS (if the numbers are slightly larger). It also 
affects parsing of reflected attributes, so meter.setAttribute('value', 
meter.value) and meter.setAttribute('value', 
2*meter.getAttribute('value')) will break if the initial value is large, 
which is surprising.

To avoid the problem, I'd have to be aware that it's a problem in the 
first place (most likely by writing the code while unaware, and then 
hopefully discovering the bug sooner rather than later), and then I'd 
have to write a function that strips the "+" from the serialised string, 
which is ugly.

Thus, it would be better for authors if "+" was allowed in floating 
point number attributes.

Philip Taylor
Received on Monday, 29 December 2008 23:19:48 UTC

This archive was generated by hypermail 2.4.0 : Saturday, 9 October 2021 18:44:40 UTC