Data360 Analyze

 View Only
  • 1.  Floating-point division returning incorrect values

    Posted 09-19-2019 09:10

    I'm experiencing an issue when dividing decimal values where the returned value is a value that's very close to the intended value but not correct. For example, if you check the attached screenshot, the first 2 rows return values that end in trailing .9s when they should have clean answers (like the 3rd row).

    In my research I'm realizing this might be an issue with floating-point math as a whole. However there are usually solutions to issues like this by using a decimal type instead of the float type for true decimal values (which for my purposes, these numbers are. Is there a way to define these numbers with a decimal data type so these division calculations come out correctly? If not, is there another clean solution aside from needing to round every division calculation going forward?

    For added context, there are cases where divisions need to be done as a middle step to more significant calculations, as a result I'd strongly prefer not to round in the middle of a process if it isn't needed.



  • 2.  RE: Floating-point division returning incorrect values

    Employee
    Posted 09-24-2019 10:45

    Hi Dinesh,

    as you mentioned, it is not always possible to obtain an exact representation of a number using Floating Point arithmetic. Even simple calculations can result in inaccuracies in the result e.g. 

    Total = 0.1 + 0.1 + 0.1 - 0.3

    Which, from a school math perspective should result in a total of 0.0. 

     

    Data3Sixty Analyze does not provide native support for Decimal data types. However, for specific applications that require exact calculations you can leverage the Decimal package of the Python language. See the following link for details of the usage of the Decimal package:

    https://docs.python.org/2/library/decimal.html

    Attached is a data flow that provides some examples of how certain calculations can be implemented in custom nodes. In this case the examples include calculating: a running total; sum of values in a field, average of values in a field; x percent of a value; the percentage of two values.

    The Transform node provides a simple example to show the principles used and the Composite nodes show the use of further features of the module including how you can modify the precision and the rounding method employed (you probably want to use the 'ROUND_HALF_UP' Rounding type rather than the default). The Precision needs to be set appropriately (see hover text for the property).

    A key aspect of using Decimal type calculations is that you should acquire the numeric data as *an exact string representation of the numeric value* e.g. by casting the values to a string rather than outputting the data as a floating point (double) data type - else you may already have introduced an inexact value even before you have made any calculations. The intermediate data should also be passed from one node to another as the string representation of the values for the same reason.

    Regards,

    Adrian

     

    Attached files

    Decimal_Type_Math_Nodes_v1.0--share.lna