How do I find infinitely long decimals in an array

Get help using Construct 2

Post » 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
22
S
8
Posts: 203
Reputation: 2,059

Post » 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.
Image ImageImage
B
169
S
50
G
169
Posts: 8,291
Reputation: 108,226

Post » 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
92
S
32
G
107
Posts: 5,274
Reputation: 69,959


Return to How do I....?

Who is online

Users browsing this forum: Lancifer and 57 guests