Decimal to Fraction Dilema

I like pursuing the odd/ even algorithm, but I can't find an instruction that returns whether an integer is either odd or even. I seem to keep coming back to the S:13/S:14 route which I don't like since I've never worked with them. An easy way maybe to convert the values to strings, if the string length is 6 places(right of decimal point), the denominator is automatically, "64th". If the string length is only 5, then the denominator is automatically "32nd". If the string length is only 4, then the denominator is automatically "16th".... and so on. That would take care of the denominator with only 6 rungs.
 
I've tried the DIV instruction on the Process Value and sent it to an integer and then a float. Neither way could I get a math run off into S:13 and S:14. What am I ignorant of?
 
When I had to do this before I tried to follow this scheme for converting decimals to fractions. This example is to convert to 1/8" increments. The scale in the picture is divided into 1/16" increments.
attachment.php


But it really depends on what you are going for. In my case, I was measuring distance and wanted to have something that was a good compromise going up and going down.

I think I just did a bunch of IF/THEN/ELSIF statement in Structured Text...

Fractions.png
 
I like pursuing the odd/ even algorithm, but I can't find an instruction that returns whether an integer is either odd or even.

To determine if an integer is odd or even just examine bit 0 of the word. If bit 0 is set to 1, then it is odd.
 
This is really a simple problem. It breaks down into two parts: 1) separate the integer and the decimal parts. 2) convert the decimal part to a fraction.

The first part has already been covered several times on the forum. Take the raw number as a float, subtract 0.5 and put the result in an integer. That's the integer part. Subtract the integer part from the float and put the result into a float, that's decimal part.

There are many ways to convert the decimal part into a fraction some easy some overly difficult. Either way you go, the first step is to convert the decimal into 64ths. Simply multiply the decimal part by 64 and put the result into an integer.

One easy way to get a fraction is to simply use this "64ths" integer as the index into an array of strings. Each element of the array holds the text for the fraction like this:
0 ""
1 "1/64"
2 "1/32"
3 "3/64"
4 "1/16"
etc.
The advantages are: it's simple, easy to follow, and executes very fast. The disadvantage is it consumes a lot of memory.

Another way is to recognize that the bit pattern of "64ths" contains everything you need to produce a reduced fraction. If bit 0 of "64ths" = 1, the denominator is 64 and the numerator = "64ths". If bit 0 of "64ths" = 0 and bit 1 = 1, the denominator is 32 and the numerator is "64ths" bit shifted right 1 place. If bit 0 and bit 1 of "64ths" = 0 and bit 2 =1, the denominator is 16 and the numerator is "64ths" bit shifted right 2 places. Continue this pattern for six rungs.
The advantages are: it's simple, fairly easy to follow, and executes pretty fast. The disadvantage is it's not as easy to follow as the lookup table. I would put a rung comment on each rung explaining what the intent is to offset that.

There is one boundary condition I didn't want to clutter the above description with. What if "64ths" = 64? You need to check for this first. If it happens, add 1 to the integer portion and set the fraction to a blank. The lookup table can handle this elegantly by having a blank string in the 0 position. For the second method, a 7th rung that concatenates the numerator + "/" + denominator would check for zero and make the string = " ". Since you don't want the "/" to show either on zero.
 

Similar Topics

Hi, how do i include the fractional part of the position I set for encoder? I have an Omron PLC and using cx programmer. I need to command my...
Replies
16
Views
3,954
In S7/TIA Portal, is there a function to round a real to a specified number of decimal places? e.g. If I have 22.519432 and I want to round it to...
Replies
16
Views
1,900
I am using a Micro 850E PLC (the new one with implicit messaging capability) to drive a Kinetix 5100 servo drive. The error code number on the...
Replies
7
Views
1,717
I'm using numeric input enable link tag to PLC DINT type. What I want is for the example in the panel view user will input 350.568 (and shown on...
Replies
1
Views
1,169
Hello, I have a SINT array I am receiving and one of the positions of this array contains the direction of the product. Inside the ladder...
Replies
4
Views
1,132
Back
Top Bottom