Decimal to Fraction Dilema

RickParrot

Member
Join Date
Nov 2015
Location
Iowa
Posts
68
Hi, All
I have received the task of measuring a process down to a 1/64 of an inch. No problem with the laser's range and mA value. Maximum value is 7 foot even. So the span is 5,376(7x12x64) Problem is displaying the decimal value into three intergers/floats so that I can grab them and throw them onto a Panelview. Production people won't know, and won't learn that 32.265625 equals:
32 17/64".
Any easy way of doing this or is it inherently complicated. Fractions need to be reduced if possible.šŸ™ƒ
 
I understand how to reduce the fraction, what I am hoping for how to SIMPLY chop up the value (say 2,777) and get it into 3 floats which would end up as F8:1= 43 (inches), F8:2= 25 (fraction numerator), and F8:3= 64(fraction denominator)(64ths) Obviously, I'm not a professor at this.
 
Take a look at this thread: http://www.plctalk.net/qanda/showthread.php?t=35587 Using the DIV function there is this: "The unrounded quotient is placed in the most significant word of the math register; the remainder is placed in the least significant word."

S:13
Math Register (Least significant word of 32-bit value)
S:14
Math Register (Most significant word of 32-bit value)

Divide your total counts (in 64ths) by 64 and then check those two registers: S:13 and S:14. S:14 should be your distance in inches and S:13 your fractional distance, in 64ths.
 
I am assuming you are wanting to display for example 31.5 as 31 1/2", and will not accept 31 32/64. The look up table is the simplest to understand.

expanding then on keshik's

Code:
//N7:0 = your input, eg 5376
//N7:1 = whole inches
//N7:2 = Fraction Numerator
//N7:3 = Fraction Denominator
//N7:4 = Dummy register
//N20:0 to 63 = look up table of Numerators (0,1,1,3,1,5,3,7,1...)
//N21:0 to 63 = look up table of Denominators (64,64,32,64,16,64,32,64,8...)
DIV N7:0 64 N7:4  //Do a divide by 64, placing the rounded(nearest) result in N7:4, rounded(down) result in S:14, and remainder (x/64) in S:13
MOV S:14 N7:1
MOV N20:[S:13] N7:2 //select numerator from look up table
MOV N21:[S:13] N7:3 //select denominator from look up table

I will leave it to you to complete the numerator and denominator table, and please hurry up and join us in the metric system.
 
+1 on the lookup table idea. I've also had applications where I wanted to display reduced fractions. After hours of trying to come up with elegant mathematical solutions [that the next guy could understand!!], I did the lookup table and never looked back. Shouldn't be too complicated with indirect addressing.
 
Thanks guys! It's nice to know that even among veterans, this job is not a simple 2 rung task. You've given me plenty to work with and I especially like the S:13 and S:14 registers. Once I have some proven logic, I will post it and invite any comments.
 
Is there a way to separate the 2.7777 into 2 and 0.77777?
If so the integer part is easy.
multiply 0.77777x64=49.7777 round to 50 so now the answer is 2 50/64
Now reduce the fraction so the result is 2 25/32.

A long floating point number may be too confusing to the operator but fractions are almost as bad. I suggest you round any floating point number to 2 decimal places so 2.777777 become 2.78. This has got to be easier to deal with than a fraction. There are fewer numbers.
 
This has got to be easier to deal with than a fraction. There are fewer numbers.

Not necessarily. In my case the operators were measuring a product with a ruler graduated in 1/16ths of an inch. They had to compare the measurement with a value shown on an HMI. It's much easier for them to compare a fraction to a fraction, than a fraction to a decimal.
 
What about this.
place the integer value of 32.265625 into an integer register integer = 32
subtract 32 from 32.265625 and place result into floating value 0.265625
multiply 0.265625 by 64 = 17 - you may have to use a float value and round
for the result.

james
 
I think the difficulty comes in showing reduced fractions, i.e. 1/2 instead of 32/64.

Plus the added frustration of doing all the math in ladder logic.
 
You must be working with the wood industry. Machinists are comfortable with 32.217 or even 32.2172 whereas wood workers use that tape measure.

The math is not that complicated but it is tedious to do all of the IF-THEN rungs necessary to do the calculation.

I would tell you that this is the only way and that there is no quicker way. However a story comes to mind from when I was a freshman in college. My brother is a very smart guy and good at mathematics and computer science. So for a computer class I was given a problem to solve using the pascal language. I wrote the program and - after an hour of computing - my program printed out the answer. My program was like 100 lines long but got the job done. My brother realized a relationship that allowed him to write a 5 line program that ran in milliseconds.

This is one reason I am not a computer programmer.

However, as I think has been previously stated, the answer is to divide by 2 until you get a non-even number. Each time you divide by 2 you access a subroutine or a function block.

Good luck
 

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