EVOLUTION-MANAGER
Edit File: setNumericRounding.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Change or turn off numeric rounding</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for setNumericRounding {data.table}"><tr><td>setNumericRounding {data.table}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2> Change or turn off numeric rounding </h2> <h3>Description</h3> <p>Change rounding to 0, 1 or 2 bytes when joining, grouping or ordering numeric (i.e. double, POSIXct) columns. </p> <h3>Usage</h3> <pre> setNumericRounding(x) getNumericRounding() </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p> integer or numeric vector: 0 (default), 1 or 2 byte rounding </p> </td></tr> </table> <h3>Details</h3> <p>Computers cannot represent some floating point numbers (such as 0.6) precisely, using base 2. This leads to unexpected behaviour when joining or grouping columns of type 'numeric'; i.e. 'double', see example below. In cases where this is undesirable, data.table allows rounding such data up to approximately 11 s.f. which is plenty of digits for many cases. This is achieved by rounding the last 2 bytes off the significand. Other possible values are 1 byte rounding, or no rounding (full precision, default). </p> <p>It is bytes rather than bits because it is tied in with the radix sort algorithm for sorting numerics which sorts byte by byte. With the default rounding of 0 bytes, at most 8 passes are needed. With rounding of 2 bytes, at most 6 passes are needed (and therefore might be a tad faster). </p> <p>For large numbers (integers > 2^31), we recommend using <code>bit64::integer64</code>, even though the default is to round off 0 bytes (full precision). </p> <h3>Value</h3> <p><code>setNumericRounding</code> returns no value; the new value is applied. <code>getNumericRounding</code> returns the current value: 0, 1 or 2. </p> <h3>See Also</h3> <p><code><a href="datatable-optimize.html">datatable-optimize</a></code><br /> <a href="https://en.wikipedia.org/wiki/Double-precision_floating-point_format">https://en.wikipedia.org/wiki/Double-precision_floating-point_format</a><br /> <a href="https://en.wikipedia.org/wiki/Floating_point">https://en.wikipedia.org/wiki/Floating_point</a><br /> <a href="https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html">https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html</a> </p> <h3>Examples</h3> <pre> DT = data.table(a=seq(0,1,by=0.2),b=1:2, key="a") DT setNumericRounding(0) # By default, rounding is turned off DT[.(0.4)] # works DT[.(0.6)] # no match, can be confusing since 0.6 is clearly there in DT # happens due to floating point representation limitations setNumericRounding(2) # round off last 2 bytes DT[.(0.6)] # works # using type 'numeric' for integers > 2^31 (typically ids) DT = data.table(id = c(1234567890123, 1234567890124, 1234567890125), val=1:3) print(DT, digits=15) DT[,.N,by=id] # 1 row, (last 2 bytes rounded) setNumericRounding(0) DT[,.N,by=id] # 3 rows, (no rounding, default) # better to use bit64::integer64 for such ids </pre> <hr /><div style="text-align: center;">[Package <em>data.table</em> version 1.14.4 <a href="00Index.html">Index</a>]</div> </body></html>