# How do I find infinitely long decimals in an array

Get help using Construct 2

### » Sun Apr 30, 2017 1:54 pm

So I made an array that contains fractions (1/2, 1/3, 2/3, etc) and what I have set up is that the user types in two numbers, the program divides them and subtracts until it's left with only the decimal point, and saves that as a global variable, then the program tries to find a corresponding fractions but I'm encountering rounding issues

For example if the user types in 100/6 the answer is 0.666666668 but in the array 1/3 is 0.6666666666
So the program doesn't work because these are two different values because it was round. Is there any way around this?
B
24
S
9
Posts: 209
Reputation: 2,223

### » Sun Apr 30, 2017 2:49 pm

I would suggest just going to the hundreds, or thousands decimal place.
mid(), left()
Then int(), back to numbers.
B
173
S
50
G
189
Posts: 8,515
Reputation: 118,764

### » Sun Apr 30, 2017 4:18 pm

There is always going to be rounding, so finding an exact decimal number in the array may not work. Usually when comparing decimal or floating point numbers you'd do abs(num1-num2)<0.0001 or some sufficiently small number.

You may be able to get by if you reduce the number before dividing. So maybe (num1%num2)/num2
A full solution is to reduce the fraction first before dividing so that 1/3 and 10/3 with the numbers before the decimal will be identical.

Num1=num1%num2
g=gcm(num1, num2)
Result=(num1/g)/(num2/g)

Gcm is the greatest common multiple. You could look at Wikipedia to see how to do that.
B
94
S
33
G
125
Posts: 5,458
Reputation: 79,979