Announcement

Collapse
No announcement yet.

Floating Point Multiplication

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Floating Point Multiplication

    I'm wondering if someone can shed some light on the floating point functions...

    I'm using flt_MUL() and it yields a slightly different number than when I use a calculator.

    In this example it's doing 261 x 6.8615 and getting 1790.851440, rather than the correct answer of 1790.8515.


    Code:
    #constant RATIO_TARGET := "6.8615"
    
        var float1[2], float2[2], float3[2];
        var valWtBrei := 261;
    
                            flt_UITOF(float1, valWtBrei);
                            flt_VAL(float2, RATIO_TARGET);
                            flt_MUL(float3, float1, float2);
                            gfx_MoveTo(10,600);
                            flt_PRINT(float1, "%.6f");
                            putstr(" x ");
                            flt_PRINT(float2, "%.6f");
                            putstr(" = ");
                            flt_PRINT(float3, "%.6f");
    This yields:

    Code:
    261.000000 x 6.861500 = 1790.851440


    I haven't yet determined if this is a significant problem for me, but was curious as to why the answer differs...
    Thanks,
    -Brian
    Last edited by BitBuilder; 10th September 2014, 05:02 AM.

  • #2
    The floating point is done using Single precision as described here http://en.wikipedia.org/wiki/Single-...g-point_format

    The 'errors' that can creep in using that format are described, but you may find it a bit difficult to follow using your example above.

    So try this 'converter' http://www.h-schmidt.net/FloatConverter/IEEE754.html

    If you put in 261, you get 261.

    When you put in 6.8615 you get 6.861499786376953

    Using those numbers in your calculator will give 1790.85144424438, but we aren't finished yet.

    Now put 1790.85144424438 into the converter and it will give you 1790.8514404296875.

    Which is the number you will get If you increase the number of decimals in your 4DGL program far enough.

    So, is the difference between your calculator and a computer using IEEE754 math significant?

    It's up to you, (and the actual accuracy of the input numbers) but try this, consider that 261 could be 260.5 rounded up, or 261.49999 rounded down. Have a look at the 'range' of answers your calculators gives for that.

    Perhaps also read here http://en.wikipedia.org/wiki/Significant_figures and where it says "For multiplication and division, the result should have as many significant figures as the measured number with the smallest number of significant figures."

    So for 261 * 6.8615 (assuming all significance is 'shown') the 'strict' answer is 1791 (or maybe even 1790, I'm not exactly sure on that last bit)

    Hope that makes sense
    Mark

    Comment


    • #3
      Thanks for the detailed explanation, Mark. Now that I see what's going on, I'm sure in the case for this application it will be of no significance...

      -Brian

      Comment

      Working...
      X