Replace toFixed to truncate rate blocks

toFixed required converting to a string and back, which had miserable
performance.  This avoids that cost.
master
Mike Gerwitz 2021-01-11 16:51:58 -05:00
parent d27cedc70c
commit 459a25e943
2 changed files with 22 additions and 21 deletions

View File

@ -216,11 +216,11 @@
<variable name="precision">
<choose>
<when test="@precision">
<value-of select="@precision" />
<value-of select="concat( '1e', @precision )" />
</when>
<otherwise>
<text>8</text>
<text>1e8</text>
</otherwise>
</choose>
</variable>
@ -275,7 +275,7 @@
<text>var result = </text>
<!-- if caller wants to yield a vector, don't cast -->
<sequence select="if ( not( $dim gt 0 ) ) then
'+(+( '
concat( 'precision(', $precision, ', +(+( ')
else
'(( '" />
<choose>
@ -296,8 +296,6 @@
<!-- if caller wants to yield a vector, don't truncate -->
<if test="not( $dim gt 0 )">
<text>.toFixed(</text>
<value-of select="$precision" />
<text>)</text>
</if>
@ -436,25 +434,25 @@
<choose>
<!-- if a precision was explicitly provided, then use that -->
<when test="@precision">
<value-of select="@precision" />
<value-of select="concat( '1e', @precision )" />
</when>
<!-- ECMAScript uses a default precision of 24; by reducing the
precision to 8 decimal places, we can drastically reduce the affect
of precision errors on the calculations -->
<otherwise>
<text>8</text>
<text>1e8</text>
</otherwise>
</choose>
</variable>
<text>Math.</text>
<value-of select="local-name()" />
<text>( +(</text>
<apply-templates select="./c:*" mode="compile" />
<text> ).toFixed( </text>
<text>(precision(</text>
<value-of select="$precision" />
<text> ) )</text>
<text>, +(</text>
<apply-templates select="./c:*" mode="compile" />
<text>)))</text>
</template>

View File

@ -1098,11 +1098,11 @@
<variable name="precision">
<choose>
<when test="@precision">
<value-of select="@precision" />
<value-of select="concat( '1e', @precision )" />
</when>
<otherwise>
<text>8</text>
<text>1e8</text>
</otherwise>
</choose>
</variable>
@ -1151,10 +1151,10 @@
<!-- store the premium -->
<value-of select="$store" />
<text> = </text>
<text> = precision(</text>
<value-of select="$precision" />
<!-- return the result of the calculation for this rate block -->
<text>(+( </text>
<text>, +(</text>
<!-- yield 0 if there are no calculations (rather than a syntax error!) -->
<if test="empty( c:* )">
<message>
@ -1169,11 +1169,7 @@
<!-- begin calculation generation (there should be only one calculation
node as a child, so only it will be considered) -->
<apply-templates select="./c:*[1]" mode="compile" />
<text> )).toFixed(</text>
<value-of select="$precision" />
<text>) * predmatch; }</text>
<text>; </text>
<text>)); }</text>
</template>
<template match="lv:rate" mode="compile-class-condition">
@ -1387,6 +1383,13 @@
};
function precision(p, x)
{
if (x % 1 === 0) return x;
return Math.round(x * p) / p;
}
/**
* Checks for matches against values for any param value
*