Parsing floats

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" 
value="$used_disk_space"></meter>};

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" 
value="8.19845012985e+14">.

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
pjt47@cam.ac.uk

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