truncating floating point

jimcav

Member
Join Date
Dec 2004
Location
new jersey
Posts
229
good morning guys using a compact logic l32e i have a real number that i wish to truncate to x.xxx before i do any math to the value. is there an easy way to do this.

Jim Cav
 
If you really want to truncate at 3 decimal places, multiply x 1000, put result in a TRN block, then divide x 1000. Truncate block does NO rounding at all, just truncates.
 
No, there is not a clean way to do it that will guarantee you three significant digits.

IEEE754 floats have limits of resolution. Lets say you multiply 1.00125 by 1000. Then use the TRN instruction on 1001.25 to remove the fraction, giving you 1001. Then divide by 1000, to get 1.001 and store it as a float. The number you will store in memory for your computation is 1.0009998.

If you have a specific reason for having exactly three significant digits then convert everything to milli-<whatevers>, put the values in DINTS, and use 32 bit integer math.
 
That final divide by 1000 may undo the truncation. Floating point does not do PRECISE decimal values as well as it would seem by just writing them out. Better to leave it after the TRN and do all your other calculations knowing you will have to divide by 1000 klater for a final result.
 
good morning guys using a compact logic l32e i have a real number that i wish to truncate to x.xxx before i do any math to the value. is there an easy way to do this.

Jim Cav

Are you sure Truncation is the operation you want to perform ?

1.123999 truncated to 3 decimal places is 1.123, but it is very nearly 1.124.

Perhaps you want your REAL Rounded to the nearest 3rd decimal place... ??
 
good morning guys using a compact logic l32e i have a real number that i wish to truncate to x.xxx before i do any math to the value. is there an easy way to do this.

Jim Cav

Why truncate to an arbitrary number of decimal places anyway? Do you just want to lose accuracy/resolution?
 
For most thermocouples, only .1 degree resolution is necessary or accurate.
For the pressure applications I use a transducer and only .01 psi is necessary.
 
You don't within the PLC. This must be done in the HMI.

I would soften that to "should" instead of must. There are rare occasions, for instance needing to send a value ultimately formatted as string "###.###" to a funky antique display...The need to truncate instead of round? Perhaps to prevent some "rollover" value from ever being displayed and always being able to hit 0.000...

I agree though, for display purposes, do it in the HMI, and you can force a round-down by subtracting .0005, before normal rounding...right? Or is there an even/odd quirk to getcha?
 
Last edited:
At the risk of seeming pedantic, lets review some base 10 fundamentals that everyone knows.

When you have a base ten floating point number, the first number after the decimal is the integer value of that number divided by 10. The second digit after the decimal is the integer value of that number divided by 100. The next by 1000. And so on. The decimal fraction then becomes A/10 + B/100 + C/1000 + D/10000 + E/100000, etc.

The reason I'm pointing out the obvious is because when a float is stored in a computer it is represented the same way, except in binary - also obvious, but you might not have realized what it implies.

Here is a diagram showing how it is stored. The first bit of a 32 bit float is the sign bit. The next eight bits are the binary exponent. All the remaining bits are the mantissa, or fraction. To gain an extra bit of resolution an implied 1 is assumed at the beginning of the mantissa.

885px-Float_example.svg.png



Just like in base 10 where the value of a digit in the fraction is that integer divided by 10 raised to the exponent for its position, in base two is it the binary 1 or 0 divided by 2 raised to the position exponent.

So the first bit is either 0/2 or 1/2, the next bit is 0/4 or 1/4, then 0/8 or 1/8, 0/16 or 1/16, and so on.

So the only floating point fractions that you can represent as an exact value are when you can add up any combination of 1/2 + 1/4 + 1/8 + 1/16 + 1/32 + 1/64 + 1/128 + 1/256 + 1/512 +... +1/223 and come up with exactly that value.

By now you should see that there is an infinite number of base 10 fractions that you are not going to be able to represent to an exact value as a base 2 fraction. You can get close, but not exact. You can't get to exactly 1/10 (base 10) no matter how many bits you use. So it is futile to attempt to round a real number fraction to a specific number of digits in a computer. Wait until the number is displayed and round the display. Prior to display the number will be converted into a string of numeric characters, and the computer library routine that does this will round the string to display only a set number of digits. It does not change the float that is stored in memory however.

Here are just a few examples of base 10 numbers you cannot store in computer memory to an exact value because there is no combination of 1/2n bits that can be summed to equal the base 10 value: 0.10, 0.20, 0.30, 0.40, 0.60, 0.70, 0.80, 0.90. Notice that that includes every possible number rounded to 1 decimal place except .5.

All computers have this limitation and it has been the subject of millions of hours of software development with lots of clever schemes to address it. Expanding to 64 bits and 128 bits has improved accuracy, but the fundamental problem is still there.

On the rare occasion that you just absolutely have to have exactly three digits and no more in the fraction for a computation then change your units. Use milliliters instead of liters, grams instead of kilograms, millivolts instead of volts, milliseconds instead of seconds, and then use integer math.
 
Last edited:

Similar Topics

Hey fellas, I'm down to my last bug in an RSL5k ladder program converted from RSL5. In the 5 there's a DIV that divides an INT, by an INT and...
Replies
22
Views
10,299
Hi All, I have a small problem which is really bugging me. It's not causing any serious issues, only reading tags. Say I have a tag...
Replies
8
Views
7,278
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,478
Hey Guys, I was looking for a little help on a school project. I was asked to program three pumps with two inputs, more and less. The project...
Replies
23
Views
2,474
I need to check an axis actual travel position, and to compare it to the master travel position. To do this I have to multiply the axis travel...
Replies
6
Views
2,520
Back
Top Bottom