Error/Bug adding floating point numbers 2006-08-31 - By Timothy Jones
I feel your pain, Ambika, really I do...
This is the reason my output stylesheets are riddled with format-number().
<xsl:value-of select="format-number(number(@(protected)), '#,##0.00')" />
and
<xsl:variable name='roundedValue' select='format-number(number($rawValue), "0.00")' />
But in my case, I know I am working with dollars/cents, so I know what my output precision should be. I fully appreciate that in other cases, it may not be so clearly defined.
The same kind of behavior exists in languages that have 'float' and 'double' types (C, C++, Java, etc, probably Pascal's "real" type too). To combat this for currency values, I convert them to the biggest integer type and process them as cents: 32-bit long in "old C", 64-bit long in Java, and "long long" on modern C/C++ compilers. At least then you are in complete control of your math. Java also has java.math.BigDecimal which doesn't do the IEEE-754 behavior either, but it has the overhead of being a java.lang.Object subclass, whereas the primitive integers/longs work faster.
I have never understood how people can accept this rounding over the years, but these are my workarounds.
Timothy Jones
Syniverse Technologies
Work
(813) 637-5366
Sr. Systems Engineer
Cell
(813) 857-7650
Development, Tampa, FL
<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas -microsoft-com:office:word" xmlns:st1="urn:schemas-microsoft-com:office :smarttags" xmlns="http://www.w3.org/TR/REC-html40">
<head> <meta http-equiv=Content-Type content="text/html; charset=us-ascii"> <meta name=Generator content="Microsoft Word 11 (filtered medium)"> <o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags" name="State"/> <o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags" name="place"/> <o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags" name="City"/> <!--[if !mso]> <style> st1\:*{behavior:url(#default#ieooui) } </style> <![endif]--> <style> <!-- /* Font Definitions */ @(protected) {font-family:"Bradley Hand ITC"; panose-1:3 7 4 2 5 3 2 3 2 3;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0in; margin-bottom:.0001pt; font-size:12.0pt; font-family:"Times New Roman";} a:link, span.MsoHyperlink {color:blue; text-decoration:underline;} a:visited, span.MsoHyperlinkFollowed {color:blue; text-decoration:underline;} p {mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; font-size:12.0pt; font-family:"Times New Roman";} span.EmailStyle18 {mso-style-type:personal-reply; font-family:Arial; color:navy;} @(protected) Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in;} div.Section1 {page:Section1;} --> </style>
</head>
<body lang=EN-US link=blue vlink=blue>
<div class=Section1>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'>I feel your pain, Ambika, really I do...<o :p></o:p></span></font></p>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'>This is the reason my output stylesheets are riddled with format-number().<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'>   ;<xsl:value-of select="format-number(number(@(protected)), '#,##0.00')" /><o:p>< /o:p></span></font></p>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'>and<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'>   ;<xsl:variable name=’roundedValue’ select=’format-number(number($rawValue), “0.00”)’ /><o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'>But in my case, I know I am working with dollars/cents, so I know what my output precision should be. I fully appreciate that in other cases, it may not be so clearly defined.<o:p></o:p>< /span></font></p>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'>The same kind of behavior exists in languages that have ‘float’ and ‘double’ types (C, C++, Java, etc, probably Pascal’s “real” type too). To combat this for currency values, I convert them to the biggest integer type and process them as cents: 32-bit long in “old C”, 64-bit long in Java, and “long long” on modern C/C++ compilers. At least then you are in complete control of your math. Java also has java.math.BigDecimal which doesn’t do the IEEE-754 behavior either, but it has the overhead of being a java.lang.Object subclass, whereas the primitive integers/longs work faster. <o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'>I have never understood how people can accept this rounding over the years, but these are my workarounds.<o:p></o:p>< /span></font></p>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>
<div>
<table class=MsoTableGrid border=0 cellspacing=0 cellpadding=0 style='border-collapse:collapse'> <tr> <td width=180 colspan=2 valign=top style='width:134.85pt;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal align=center style='text-align:center'><strong><b><i><font size=3 color=teal face="Bradley Hand ITC"><span style='font-size:12.0pt; font-family:"Bradley Hand ITC";color:teal;font-style:italic'>Timothy Jones</span></font></i></b></strong><strong><b><i><font color=red face=Arial><span style='font-family:Arial;color:red;font-style: italic'><o:p></o:p></span></font></i></b></strong></p> </td> <td width=419 valign=top style='width:314.6pt;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal><strong><b><i><font size=2 color=red face=Arial><span style='font-size:10.0pt;font-family:Arial;color:red;font-style:italic' >Syniverse Technologies</span></font></i></b></strong><strong><b><i><font color=teal face="Bradley Hand ITC"><span style='font-family:"Bradley Hand ITC"; color:teal;font-style:italic'><o:p></o:p></span></font></i></b></strong></p> </td> </tr> <tr> <td width=60 valign=top style='width:44.85pt;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal align=right style='text-align:right'><font size=2 color=navy face=Arial><span style='font-size:10.0pt;font-family:Arial; color:navy'>Work</span></font><strong><b><i><font color=teal face="Bradley Hand ITC"><span style='font-family:"Bradley Hand ITC"; color:teal;font-style:italic'><o:p></o:p></span></font></i></b></strong></p> </td> <td width=120 valign=top style='width:1.25in;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'>(813) 637-5366<o:p></o:p></span></font>< /p> </td> <td width=419 valign=top style='width:314.6pt;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'>Sr. Systems Engineer </span>< /font><strong><b><font color=navy face=Arial><span style='font-family:Arial;color:navy;font-weight: normal'><o:p></o:p></span></font></b></strong></p> </td> </tr> <tr> <td width=60 valign=top style='width:44.85pt;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal align=right style='text-align:right'><font size=2 color=navy face=Arial><span style='font-size:10.0pt;font-family:Arial; color:navy'>Cell</span></font><strong><b><i><font color=teal face="Bradley Hand ITC"><span style='font-family:"Bradley Hand ITC"; color:teal;font-style:italic'><o:p></o:p></span></font></i></b></strong></p> </td> <td width=120 valign=top style='width:1.25in;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'>(813) 857-7650<o:p></o:p></span></font>< /p> </td> <td width=419 valign=top style='width:314.6pt;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size: 10.0pt;font-family:Arial;color:navy'>Development, <st1:place w:st="on"><st1 :City w:st="on">Tampa</st1:City>, <st1:State w:st="on">FL</st1:State></st1:place>< /span></font><strong><b><i><font color=teal face="Bradley Hand ITC"><span style='font-family:"Bradley Hand ITC "; color:teal;font-style:italic'><o:p></o:p></span></font></i></b></strong></p> </td> </tr> </table>
</div>
<div>
<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><o:p> </o:p></span></font></p>
</div>
</div>
</body>
</html>
|
|