How do I find infinitely long decimals in an array

0 favourites
  • 3 posts
From the Asset Store
Full game Construct 2 and Construct 3 to post on Google Play
  • 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?

  • I would suggest just going to the hundreds, or thousands decimal place.

    mid(), left()

    Then int(), back to numbers.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)