News:

  • October 16, 2026, 04:13:33 PM

Login with username, password and session length

Author Topic: Precise MATH Instructions  (Read 23012 times)

Bolt

  • Hero Member
  • *****
  • Posts: 598
Precise MATH Instructions
« on: July 29, 2021, 06:46:31 PM »
I'm trying to do some calculations on a BRX, and am seeing about 8% error vs Excel.  When I simplify the numbers (in Excel and BRX), I get about 1/2% error, so it seems to be related to reals and rounding and what not.  Are there general rules of thumb to squeeze more precise math out of the MATH instructions?  This instruction contains SQRT and COS instructions.

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6177
  • Yes Pinky, Do-more will control the world!
Re: Precise MATH Instructions
« Reply #1 on: July 29, 2021, 11:12:06 PM »
Probably a single precision vs double precision issue. There are no simple rules, just an understanding of how each operation is adding error, and structuring the operations to minimize it.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

ADC Product Engineer

  • Hero Member
  • *****
  • Posts: 270
Re: Precise MATH Instructions
« Reply #2 on: July 30, 2021, 09:03:26 AM »
Also understanding promotion/demotion is a pretty big deal.

2 * 3.5 might not be the same as 2.0 * 3.5

The MATH help explains it better than I can. Topic: DMD0085

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6177
  • Yes Pinky, Do-more will control the world!
Re: Precise MATH Instructions
« Reply #3 on: July 30, 2021, 11:33:26 AM »
Also understanding promotion/demotion is a pretty big deal.

2 * 3.5 might not be the same as 2.0 * 3.5

The MATH help explains it better than I can. Topic: DMD0085

Absolutely correct, although that example isn't the best. A better example would be 7/2 + 3.0 = 6.0, whereas 7/2.0 + 3 = 6.5. Get the order of those operations wrong and your results can get very skewed. Excel uses double precision float everywhere I'm sure, and generally those issues disappear. While I would love to use double everywhere in the PLC, it simply isn't viable with that hardware.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

Garyhlucas

  • Hero Member
  • *****
  • Posts: 421
Re: Precise MATH Instructions
« Reply #4 on: July 30, 2021, 01:07:40 PM »
Isn?t this just MATH using Reals vs Integer?

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3850
    • Host Engineering
Re: Precise MATH Instructions
« Reply #5 on: July 30, 2021, 01:17:54 PM »
Isn?t this just MATH using Reals vs Integer?

Yes.  Integer math is precise (assuming you don't overflow +/- 2billion)

Real math is NOT precise (e.g. you can't represent 1.0/3.0 precisely using IEEE floating point - it's just an approximation), hence complex expression errors can get "relatively" large, depending upon what you are doing.  32 bit (single precision) IEEE has 24 bits of precision, but double precision floating point has 53 bits, even more than 32 bit integer.

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6177
  • Yes Pinky, Do-more will control the world!
Re: Precise MATH Instructions
« Reply #6 on: July 30, 2021, 03:13:36 PM »
Isn?t this just MATH using Reals vs Integer?

In the OPs Excel vs the PLC case it can easily be single precision vs double precision float. There are a large number of ways error can creep into math.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

Bolt

  • Hero Member
  • *****
  • Posts: 598
Re: Precise MATH Instructions
« Reply #7 on: July 30, 2021, 04:03:53 PM »
Also understanding promotion/demotion is a pretty big deal.

2 * 3.5 might not be the same as 2.0 * 3.5

The MATH help explains it better than I can. Topic: DMD0085
Yep, I'm aware of the implications of integer math, and the TOREAL/2.0 type solutions.

Isn?t this just MATH using Reals vs Integer?

In the OPs Excel vs the PLC case it can easily be single precision vs double precision float. There are a large number of ways error can creep into math.

Yes, I'm afraid that's what it boils down to is the lack of double precision.

Also understanding promotion/demotion is a pretty big deal.

2 * 3.5 might not be the same as 2.0 * 3.5

The MATH help explains it better than I can. Topic: DMD0085

Absolutely correct, although that example isn't the best. A better example would be 7/2 + 3.0 = 6.0, whereas 7/2.0 + 3 = 6.5. Get the order of those operations wrong and your results can get very skewed. Excel uses double precision float everywhere I'm sure, and generally those issues disappear. While I would love to use double everywhere in the PLC, it simply isn't viable with that hardware.

Are you sure you can't just add a little code to allow us to cast R's to a R:DP? :P

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3628
  • Darth Ladder
Re: Precise MATH Instructions
« Reply #8 on: July 30, 2021, 04:22:00 PM »
Excel uses double precision float everywhere I'm sure

Don't be too quick.   Maybe the BRX is actually correct, and his PC has the Approxium ((c) Intel)!
I retract my earlier statement that half of all politicians are crooks.  Half of all politicians are NOT crooks.  There.

Bolt

  • Hero Member
  • *****
  • Posts: 598
Re: Precise MATH Instructions
« Reply #9 on: July 30, 2021, 04:35:50 PM »
Excel uses double precision float everywhere I'm sure

Don't be too quick.   Maybe the BRX is actually correct, and his PC has the Approxium ((c) Intel)!

Nope. I've also checked it against online formulas to figure all the math out in the first place.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3850
    • Host Engineering
Re: Precise MATH Instructions
« Reply #10 on: July 30, 2021, 04:44:00 PM »
Could you post your MATH instruction expression (copy/paste)?

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3628
  • Darth Ladder
Re: Precise MATH Instructions
« Reply #11 on: July 30, 2021, 04:58:20 PM »
Nope. I've also checked it against online formulas to figure all the math out in the first place.

I was just kidding.
I retract my earlier statement that half of all politicians are crooks.  Half of all politicians are NOT crooks.  There.

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6177
  • Yes Pinky, Do-more will control the world!
Re: Precise MATH Instructions
« Reply #12 on: July 30, 2021, 05:27:19 PM »
Are you sure you can't just add a little code to allow us to cast R's to a R:DP? :P

Yeah...um. Sorry.

I actually fought with similar issues while developing the motion functions. Single precision wasn't accurate enough in a few of the axis calculations so I had to selectively add double. It was a little tricky getting the balance of accuracy and performance.
« Last Edit: July 30, 2021, 05:30:00 PM by BobO »
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

Bolt

  • Hero Member
  • *****
  • Posts: 598
Re: Precise MATH Instructions
« Reply #13 on: July 30, 2021, 07:53:22 PM »
Nope. I've also checked it against online formulas to figure all the math out in the first place.

I was just kidding.
Are you sure you can't just add a little code to allow us to cast R's to a R:DP? :P

Yeah...um. Sorry.

Man, the jokes are hard to pick up on here today!

Bolt

  • Hero Member
  • *****
  • Posts: 598
Re: Precise MATH Instructions
« Reply #14 on: July 30, 2021, 07:55:07 PM »
Could you post your MATH instruction expression (copy/paste)?

You asked for it!

SQRT(((((111.1321 - (0.56605 * COS(2 * ((R11 + R12) / 2)))) + (0.0012 * COS(2 * ((R11 + R12) / 2)))) * (R11 - R12)) ** 2) + (((((111.4151 * COS((R11 + R12) / 2)) - (0.09455 * COS(3 * ((R11 + R12) / 2)))) + (0.00012 * COS(5 * ((R11 + R12) / 2)))) * (R13 - R14)) ** 2))

I didn't fully expect it to "work", I was testing the capabilities, to see if a PLC would be suitable for the application.  Seems I might be striking out.