You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pig.apache.org by "Philip (flip) Kromer (JIRA)" <ji...@apache.org> on 2014/05/11 00:04:18 UTC

[jira] [Reopened] (PIG-3926) ROUND_TO function: rounds double/float to fixed number of decimal places

     [ https://issues.apache.org/jira/browse/PIG-3926?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Philip (flip) Kromer reopened PIG-3926:
---------------------------------------


* ROUND_TO returns NULL given a NULL value (rather than failing)
* It now by default rounds to the nearest neighbor, choosing the _even_ digit for ties: ROUND_TO(-2.5,1) == -2.0, ROUND_TO(-1.5,1) == -2.0. See http://docs.oracle.com/javase/7/docs/api/java/math/RoundingMode.html#HALF_EVEN: "This is the rounding mode that statistically minimizes cumulative error when applied repeatedly over a sequence of calculations [... and] is analogous to the rounding policy used for float and double arithmetic in Java" 
* You can specify the rounding mode using an optional third parameter, supplying the integer value of one of the rounding modes: http://docs.oracle.com/javase/7/docs/api/constant-values.html#java.math
* Added more test cases.

> ROUND_TO function: rounds double/float to fixed number of decimal places
> ------------------------------------------------------------------------
>
>                 Key: PIG-3926
>                 URL: https://issues.apache.org/jira/browse/PIG-3926
>             Project: Pig
>          Issue Type: New Feature
>    Affects Versions: 0.12.0
>            Reporter: Philip (flip) Kromer
>            Assignee: Philip (flip) Kromer
>            Priority: Minor
>              Labels: builtin, function, math
>             Fix For: 0.13.0
>
>         Attachments: 0001-ROUND_TO-handles-nulls-accepts-rounding-mode.patch, 0001-Unit-tests-for-ROUND.patch, 0002-ROUND_TO-UDF-safely-rounds-values-to-given-precision.patch
>
>
> The too-often used trick of rounding to a fixed number of decimal places by writing '1000 * Math.round(num / 1000.0)' is not only unsightly, it's numerically imprecise (http://stackoverflow.com/a/12684082/41857). The attached patch adds a function to do this.
> ROUND_TO(val, digits) accepts a single float or double value, along with an integer number of digits, and returns the value to that number of decimal places 
> Examples:  
> {code}
> ROUND_TO(3.14159f, 3) -- returns a float 3.142
> ROUND_TO(3.14159d, 0) -- returns a double 3.0.
> {code}
> I also added unit tests for ROUND -- there were none before that I could find.
> I don't know how to write a unit test that the schema correctly routes to FloatRoundTo and DoubleRoundTo -- if the ones I provided are insufficient please point me to a simple example to emulate.



--
This message was sent by Atlassian JIRA
(v6.2#6252)