Truncate Floating Point

PCPtech

Member
Join Date
Nov 2009
Location
California
Posts
7
I would like to truncate an F8
Example (2.528195) to 2.5

I have searched the forums but haven't had any luck. I am fairly new to programming and would appreciate any assistance.

Thank You
 
Multiply by 10 and store in an intermediate integer file such as N7:0, then divide by 10 again.

Edit: I believe my solution will actually round the value, 2.58 would become 2.6. Do you want to truncate, round or don't care?
 
Hi,

What would happen if you divided by 1 and then got the value from the most significant word of the math register? S:14 maybe

Just a SWAG.

Mark
 
Multiply by 10 and store in an intermediate integer file such as N7:0, then divide by 10 again.

Edit: I believe my solution will actually round the value, 2.58 would become 2.6. Do you want to truncate, round or don't care?


To compensate for rounding, subtract .5.
MUL F8:0 10.0 F8:0
SUB F8:0 .5 N7:0
MUL N7:0 0.1 F8:0


2.528195 * 10 = 25.28195
25.28195 - .5 = 24.78195 ---> rounds to 25 when moved to integer
25 * .1 = 2.5

2.5999 * 10 = 25.999
25.999 - .5 = 25.4999 ---> rounds to 25 when moved to integer
25 * .1 = 2.5


OK, now that the technique that works on paper is out of the way, lets talk about floating point numbers in computers. Computers (PLCs included) use something called IEEE754 format to represent floating point numbers in binary. There are lots of numbers that cannot be represented exactly in IEEE 754 floating point format. One such number is any decimal 1/10^n, ie, .1, .01, .001, etc. In other words, no matter how many times you try, you are not going to truncate to 1.1 or 2.1 or 3.1, etc. to a single decimal place because the number cannot be represented in IEEE754, instead you get 0.499999940395355 and thats a close as you're going to get, no matter how hard you try. You will run into the same problem with any #.2, #.4, #.7, #.8, and an infinite number of other combinations.
There are a number of threads on the forum on the subject - search IEEE 754. It is generally counterproductive to truncate your float. The routines that display floating point numbers on your computer screen have sophisticated and complex algorithms that display the number to a specific decimal place, and that know to display .1 on the computer screen when they see 0.499999940395355 but they usually don't try and truncate the actual float. See http://babbage.cs.qc.edu/IEEE-754/Decimal.html and http://en.wikipedia.org/wiki/IEEE_754-1985 to see how some numbers are actually stored.

Give us some more information about what you are trying to accomplish and maybe we can help.
 
Last edited:
First thank you all for your input. I am not a high level programmer at all.

What I am doing:
I have a SLC 5/04 w/ HSCE2
Connected to the HSCE2 is a 500 ppr pulse generator (pulses per revolution)
I am calculating the speed of a conveyor belt to compare with a speed setpoint to decide to speed up, slow down, or hold.
I am not too concerned about the rounding. It actually helps keep the process from bouncing between speeding up and slowing down and that level of accuracy is way better then the current application.

I have implemented the suggestions and it seems to be working well.
 
Glad its working for you. 9 times out of 10 the inability to represent certain numbers exactly is not an issue. But it is because of that 1 time in 10 that it becomes a problem that I asked about the application. There are lots of things that can be done for that 1 in 10, but knowing what the app is is the first step. You are definitely in the 9 out of 10 where it is not an issue. Good luck with the rest of the app.
 

Similar Topics

Hey fellas, I've got a 4-20 analog signal coming from a distance measuring laser into a Flex IO analog input module. I'm locating the position...
Replies
2
Views
2,074
Hi, Can anyone tell me if there is an equivalent 'truncate' function in Mitsubishi GX Works 2. Ive worked with Rockwell and Siemens for years...
Replies
23
Views
4,675
I'm currently working on a project that requires me to use a compute. I'm trying to truncate the first part of the equation and then divide by...
Replies
10
Views
13,969
I have a question on AB SLC 5/05 RSLogix 500. I need to take a floating point number and break it into two integer words, the lower word for...
Replies
4
Views
4,523
How is floating web accumulator works ? I have a project where I will control a floating accumulator without speed trim. Input and Output rolls...
Replies
14
Views
3,590
Back
Top Bottom