News:

  • June 10, 2026, 07:59:16 AM

Login with username, password and session length

Author Topic: Floats and ints and bitwise operators...oh my!  (Read 34920 times)

Mike Nash

  • Hero Member
  • *****
  • Posts: 652
Floats and ints and bitwise operators...oh my!
« on: October 17, 2014, 02:26:15 PM »
Here's an example of a case where I'd like some formatting in MATH box expressions:

Code: [Select]
((Drum[V0] | IF(MHR7:0 & C201, RelayWDCfg0:SD, 0)) & IF(C103, -1, RelayPanicCfg0:SD) &
 IF(ST4, ~RelayFlashCfg0:SD, -1) & ~RelayTypeTOD0:SD) | (RelayTypeTOD0:SD & TODOutputs0:SD)

Actually doesn't look too bad on screen but hard to follow scrunched up in a little MATH edit box or at runtime.

I have no idea what that is supposed to do. It did make me look into the & and | a little and I get what I think are strange results.

I am also foggy on how to format this so the instruction is a MATH box with R0 as Result

Code: [Select]
IF(X0, 1.1, 2.2) | IF(X1, 4.4, 8.8)
X0  X1    R0
 0     0    10.0
 0     1     6.0
 1     0     9.0
 1     1     5.0
So why the truncation and why the addition?
« Last Edit: October 22, 2014, 10:43:29 AM by BobO »

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3607
  • Darth Ladder
Floats and ints and bitwise operators...oh my!
« Reply #1 on: October 17, 2014, 02:28:28 PM »
It's all boolean.  It replaces 32 rungs of ladder with 12 contacts per in one MATH box.  The answer gets sent to Y32:SD.

| and & are bitwise OR and AND, so they don't have any meaning in a floating point context.  I'm using them on bitpack DINTs.
« Last Edit: October 17, 2014, 02:34:19 PM by Controls Guy »
I retract my earlier statement that half of all politicians are crooks.  Half of all politicians are NOT crooks.  There.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3827
    • Host Engineering
Floats and ints and bitwise operators...oh my!
« Reply #2 on: October 17, 2014, 02:38:27 PM »
X0  X1    R0
 0     0    10.0
 0     1     6.0
 1     0     9.0
 1     1     5.0
should be
IF(X0, IF(X1, 5.0, 9.0), IF(X1, 6.0, 10.0))

Mike Nash

  • Hero Member
  • *****
  • Posts: 652
Floats and ints and bitwise operators...oh my!
« Reply #3 on: October 17, 2014, 03:05:04 PM »
X0  X1    R0
 0     0    10.0
 0     1     6.0
 1     0     9.0
 1     1     5.0
should be
IF(X0, IF(X1, 5.0, 9.0), IF(X1, 6.0, 10.0))

But my question is more "why is the truncation and summing even happening?" I am not trying to get particular results, I just don't see where the results are coming from. In other words, I want to know what types of things I can do and what I had better avoid due to unexpected (by me) results.

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3607
  • Darth Ladder
Floats and ints and bitwise operators...oh my!
« Reply #4 on: October 17, 2014, 03:08:11 PM »
It's taking the IEEE 32-bit representation of the floating point values and ORing them bitwise.  Like I said, it's meaningless to try to do bit ops on floats, unless you're actually working on the value knowing the format (mantissa, exponent, etc.)

Here's the Wikipedia article on how 32-bit floats are encoded:  https://en.wikipedia.org/wiki/Single-precision_floating-point_format
« Last Edit: October 17, 2014, 03:14:23 PM by Controls Guy »
I retract my earlier statement that half of all politicians are crooks.  Half of all politicians are NOT crooks.  There.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3827
    • Host Engineering
Floats and ints and bitwise operators...oh my!
« Reply #5 on: October 17, 2014, 03:09:21 PM »
But my question is more "why is the truncation and summing even happening?"
In your version, you are bit-wise OR-ing floating point numbers.  That is not a good idea.  Only do bit-wise operations on integer types.

plcnut

  • Hero Member
  • *****
  • Posts: 814
    • premiersi.com
Floats and ints and bitwise operators...oh my!
« Reply #6 on: October 17, 2014, 03:13:10 PM »
"why is the truncation and summing even happening?"

You are combining BITwise instructions with real numbers. When you take a Real value of 5.0 and view is as bits, and then take the Real value of 2.0 and view it as bits and then OR the bits, you will get a series of bits that make sense as bits, but when you view those same bits as Real numbers, they *may* not resemble anything related to 5.0 and 2.0.
Circumstances don't determine who we are, they only reveal it.

~Jason Wolthuis
Premier Systems Integration, LLC
http://premiersi.com

Mike Nash

  • Hero Member
  • *****
  • Posts: 652
Floats and ints and bitwise operators...oh my!
« Reply #7 on: October 17, 2014, 03:16:08 PM »
AH! I get the bitwise ORing part now. I was using the 1,2,4 and 8s just so I would know where the results were coming from without realizing I was setting myself up!

b_carlton

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 606
    • thePLCguy
Floats and ints and bitwise operators...oh my!
« Reply #8 on: October 17, 2014, 04:50:44 PM »
Just out of curiosity I had to try it. The bitwise OR of 5.0 and 2.0 is ... 5.0   Big disappointment.
An output is a PLC's way of getting its inputs to change.

plcnut

  • Hero Member
  • *****
  • Posts: 814
    • premiersi.com
Floats and ints and bitwise operators...oh my!
« Reply #9 on: October 17, 2014, 05:04:49 PM »
Big disappointment.

I didn't pick very good numbers did I...
Hopefully it communicated a point though... about... Floating points ;D
Circumstances don't determine who we are, they only reveal it.

~Jason Wolthuis
Premier Systems Integration, LLC
http://premiersi.com

Mike Nash

  • Hero Member
  • *****
  • Posts: 652
Floats and ints and bitwise operators...oh my!
« Reply #10 on: October 17, 2014, 06:20:14 PM »
Just out of curiosity I had to try it. The bitwise OR of 5.0 and 2.0 is ... 5.0   Big disappointment.

Well now I am confused again. The simulator shows MATH R125 5.0 | 2.0 = 7.0 which is what I would expect per my new understanding.

b_carlton

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 606
    • thePLCguy
Floats and ints and bitwise operators...oh my!
« Reply #11 on: October 17, 2014, 07:35:13 PM »
I used the floating point calculator at http://www.h-schmidt.net/FloatConverter/IEEE754.html

5.0 gives a hex representation of 40A0 0000
2.0 gives a hex representation of 4000 0000

which bitwise ORed should give    40A0 0000 or 5.0

But the instruction help information clearly notes that it is not meant for Real values. This is probably why.
An output is a PLC's way of getting its inputs to change.

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3607
  • Darth Ladder
Floats and ints and bitwise operators...oh my!
« Reply #12 on: October 21, 2014, 10:56:40 AM »
Byte extraction casts!  I was hoping they were there and when I checked sure enough they were! Why do we not have a thumbs-up emoticon??

Feature request:  Restore our ability when viewing numeric memory locations in ASCII mode in a Data View to specify how many registers (or characters) to show on a line (like DS5 behavior).  Sometimes we still have to store text in numeric memory even though we have a string type now, and it also helps with Ctrl-Enter'ing multiple blocks at a time.

Thanks!
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: 6158
  • Yes Pinky, Do-more will control the world!
Floats and ints and bitwise operators...oh my!
« Reply #13 on: October 21, 2014, 11:27:24 AM »
Byte extraction casts!  I was hoping they were there and when I checked sure enough they were! Why do we not have a thumbs-up emoticon??

Because we are better PLC developers than forum managers. ;)
"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

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3827
    • Host Engineering
Floats and ints and bitwise operators...oh my!
« Reply #14 on: October 21, 2014, 11:40:39 AM »
Feature request:  Restore our ability when viewing numeric memory locations in ASCII mode in a Data View to specify how many registers (or characters) to show on a line (like DS5 behavior).
That's what STRINGs are for.  You get a lot more ASCII display capabilities using STRINGs.

For raw numeric, you can still see the ASCII data, just one numeric element at a time.  There currently is no good way to specify the variable "length" in Data View.  Hence, you can still get as many as you want, up to 99 in one Data View.