LAE

Welcome to the LAE community!  Please feel free to start a discussion in the discussion tab or join in a conversation.

Discussions

Members

Resources

Events

 View Only
  • 1.  Floating Point Calculations, Rounding

    Employee
    Posted 09-29-2009 14:02

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: rabbott

    There is a standard problem with computers making floating point calculations in decimal (base 10) because the computer generates and stores them in binary (base 2). Essentially, what you�re looking at is a single decimal representation of what could be one of several approximate binary values. For example:

    a = 2.905
    b = 2.9 + .005
    c = 2.9 + .004999999998
    d = 2.9 + .005000000000000001
    emit a,b,c,d
    emit a.round(-2) as ar
    emit b.round(-2) as br
    emit c.round(-2) as cr
    emit d.round(-2) as dr

    Will output:

    a:double | b:double | c:double | d:double | ar:double | br:double | cr:double | dr:double
    2.905 | 2.905 | 2.905 | 2.905 | 2.91 | 2.91 | 2.9 | 2.91

    Note that all four values show as 2.905, but the rounding of all four is not the same.

    The best way to deal with this is to convert the calculation to integer math. For example:

    round(2.905, -2)

    Gives you either 2.91 OR 2.90 randomly

    round (2.905, -2, 1)

    Gives you 2.91 consistently but does so for any value over 2.90

    round (2.905, -2, -1)

    Gives you 2.90 consistently for any value

    To ensure you get 2.91 for any value of 2.905 through 2.909, you would want to convert the calculation to integer math:

    round(int(1000 * 2.905), 1)/1000

    Attached is a graph containing the above example and solution.

    - Bob

    Attachments:
    RoundingFloats.brg


  • 2.  RE: Floating Point Calculations, Rounding

    Employee
    Posted 04-30-2013 10:28

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: Umbaugh

    I am having a challenge with a floating point calculation in an Agg node. I am attempting to do a sum([Double Variable]) in an Agg. The Double Variable has negative and positive numbers often canceling each other to zero. It works fine while sorted in alternating equal positive and negative values. At any time the input values do not alternate and then come back to zero, the value turns to an exponent instead of zero. I commented out the where lastInGroup to troubleshoot. Below is an example of this occurrence. The sum of this particular variable should balance to zero, however the exponent value is returning. How can this be corrected? I attempted the floating point calculations suggested to no avail. Thank you for any assistance.

    Double Variable Double Variable_Sum
    -0.04 -0.04
    0.04 0.0
    -0.04 -0.04
    -0.04 -0.08
    0.04 -0.04
    0.04 0.0
    0.04 0.04
    0.04 0.08
    0.04 0.12
    -0.04 0.08
    -0.04 0.04
    -0.04 -1.38777878078e-17
    -0.04 -0.04
    -0.04 -0.08
    0.04 -0.04
    0.04 -1.38777878078e-17
    0.04 0.04
    -0.04 -1.38777878078e-17


  • 3.  RE: Floating Point Calculations, Rounding

    Employee
    Posted 05-02-2013 04:12

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: ThomasT

    Much apreciated! Thank you!


  • 4.  RE: Floating Point Calculations, Rounding

    Employee
    Posted 05-02-2013 06:34

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: rboccuzzi

    Umbaugh, you are running into what Bob is talking about above; your value is close to, but not exactly 0 because of standard floating point variations, as described in our help. The exponent is there because you actually have a VERY small number, VERY close to 0. You should avoid comparing directly with a 0 for exactly this reason. In your case, you would do one of a few approaches I show in the attached example graph, either using the round function, or even better, to calculate equality with an explicit tolerance (epsilon).

    Cheers
    Rich
    Attachments:
    Floating Point Sum Example.brg


  • 5.  RE: Floating Point Calculations, Rounding

    Employee
    Posted 05-14-2013 08:18

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: Umbaugh

    Thank you Rich. I apologize for the delayed response. I did not receive an expected e-mail notification when the thread was updated by you. I received an e-mail for ThomasT's update, but not for yours.

    I was not able to see the use of the groupSum as we are still on version 4. I was able to remove the group and just use sum though. What is the difference between these? Is there any appreciable difference in my circumstance?

    I was able to successfully use the sum version with the round function. We had attempted to use the round, but had tried to combine it with the sum function in the same variable call. Still trying to get a hang of the Brainscript syntax.

    Thank you for you assistance.


  • 6.  RE: Floating Point Calculations, Rounding

    Employee
    Posted 05-14-2013 08:29

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: rboccuzzi

    Nope, sum was deprecated to the name groupSum, to match the other group operators, but it is the same. As for the notification, you will only get one notification from a thread before you revisit. Since the notification from ThomasT probably didn't cause you to follow through to the thread here, you wouldn't see any more notifications. I guess it's always worth following the notification to the thread if you want to be sure to get all notifications following.

    Cheers
    Rich