Author Topic: Managing IEEE Floating Point Numbers  (Read 39468 times)

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3177
    • View Profile
    • Internet Programmable PLCs
Managing IEEE Floating Point Numbers
« on: December 06, 2011, 10:45:48 PM »
Although the Nano-10, FMD and F-series as well as the older T100M+ PLCs do not directly support floating point computation, there may be situation when it become necessary to deal with numbers that it obtains that is formatted as a floating point number.  For example the PLC could interface to a Modbus power meter that only presents data in floating point format via Modbus registers. The retrieved floating point data needs to be converted into fixed point integer for computation.

In other occasions, the PLC may need to present its result as a floating point number for other devices that only accepts data in floating point format. This enables the PLC to be programmed as an instrument, a smart sensor gateway, or act as a data concentrator, data translator or protocol converters between incompatible industrial devices.

We have written and tested two TBASIC custom functions to enable translation of floating-point to fixed-point data and vice versa. The .PC6 files containing the two functions can be downloaded from the following link. These two functions are written to be easily copied into your application programs. Please refer to the comments in the functions for details.

http://www.tri-plc.com/trilogi/Float2Integer-RevA.zip

If you wish to check the conversion result of the function, the following website is a good calculator:

http://www.h-schmidt.net/FloatApplet/IEEE754.html

More information about IEEE Single Precision Floating point format can be found at:

http://en.wikipedia.org/wiki/Single-precision_floating-point_format
« Last Edit: December 07, 2011, 10:30:30 AM by support »
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3177
    • View Profile
    • Internet Programmable PLCs
Re:Managing IEEE Floating Point Numbers
« Reply #1 on: October 29, 2014, 08:55:23 AM »
The Fx1616-BA, Fx2424 and SmartTILE-Fx board now all natively support floating point numbers and it is very easy for these PLCs to read from MODBUS registers that contain numbers in floating point format.

The i-TRiLOGI version 7 supports the commands:

A = FLOAT2BITS (x#)   ' convert floating point number into IEEE 32-bit single precision hex number
X# = BITS2FLOAT(n)   ' convert a 32-bit number in IEEE single precision format into a floating point number.

Hence you can use the standard READMB2 command to read two consecutive Modbus registers and then convert the 32-bit number into internal floating point numbers for computation.
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

BC SYSTEMS

  • Full Member
  • Posts: 146
    • View Profile
Re:Managing IEEE Floating Point Numbers
« Reply #2 on: January 16, 2015, 12:50:31 PM »
Hi,

I'm using a fx PLC and reading data from a power meter in the IEEE format. When I look at the variable A# in the online viewer the value is correct "240.5". If I copy this to the LCD I get "+240.50000" but really all I want is "240.50".

I have copied the data from A# to A, multiplied by 10 then used the MOD function to get " 240.50" but this seems a little long winded and uses up the 32 bit variable.

I was wondering if I could truncate the A# string?   Have tried A# = MID$(A#),2,6) but this fails to compile  :'(
« Last Edit: January 16, 2015, 02:44:43 PM by BC SYSTEMS »

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3177
    • View Profile
    • Internet Programmable PLCs
Re:Managing IEEE Floating Point Numbers
« Reply #3 on: January 16, 2015, 08:45:47 PM »
try this:

SETLCD 1,1, STR$(A#, -2)
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

BC SYSTEMS

  • Full Member
  • Posts: 146
    • View Profile
Re:Managing IEEE Floating Point Numbers
« Reply #4 on: January 17, 2015, 12:07:55 AM »
Hello,

Thanks that helped but you still have the signed bit representing a + or - depending on your value.   Can I remove this also without moving the data into the 16/32 bit memory?  Appreciate IEEE is for both positive and negative number representation but the value I'm reading will always be positive.

Cheers

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3177
    • View Profile
    • Internet Programmable PLCs
Re:Managing IEEE Floating Point Numbers
« Reply #5 on: January 17, 2015, 03:23:45 PM »
Sorry, but you will have to use MID$ to remove the sign.


A# = 240.50000
A$ = STR$(A#,-2)
SETLCD 1,1, A$

A$ = MID$(A$, 2, LEN(A$)-1)
SETLCD 2,1, A$


Email: support@triplc.com
Tel: 1-877-TRI-PLCS

BC SYSTEMS

  • Full Member
  • Posts: 146
    • View Profile
Re:Managing IEEE Floating Point Numbers
« Reply #6 on: January 17, 2015, 04:01:06 PM »
Ok Thanks

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:Managing IEEE Floating Point Numbers
« Reply #7 on: January 19, 2015, 10:52:21 AM »
Marcus,

I was having problems formatting negative floating point values with str$(x#,n).

This code shows a similar usage of mid$ to swap a "+" for a "-".


   ' print float with 2 decimal points of accuracy
   '
   if (i# < 0.0)
      A$ = str$(-i#,-2)
      A$ = "-" + mid$(A$,2,len(A$)-1)  
      print #2 A$
   else
      print #2 str$(i#,-2)
  endif
[/font][/color]
Best regards,

Gary d

BC SYSTEMS

  • Full Member
  • Posts: 146
    • View Profile
Re:Managing IEEE Floating Point Numbers
« Reply #8 on: January 19, 2015, 11:03:04 AM »
Thanks Gary,

I'll give that a try!

Cheers

Marcus