This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

How to make "Fraction Simplification" ? Like

9/18 return 1/2
14/21 return 2/3

I did not find such a function in the documentation

Godot version 3.2.4
in Engine by (41 points)

We don't know what you want. Do you want fractions to reduce to their smallest denominator? Do you want to maintain the fraction in the code? A division operation always returns a number in the form of an integer or a floating point number.

How i can find "Greatest common factor" of 2 numbers?

2 Answers

0 votes
Best answer

When you divide int by int, you will get int as result (fraction part will be discarded). To get float in result, you must make one of the numbers float. This can be done in two ways:
1. If you have number, you can add .0 to one of the values: 9.0 / 18 will give you 0.5
2. If your number is variable, then you have to use explicit type conversion: float(n) / 18 will give you 0.5 for n = 9.

To find greatest common factor you can use next function:

func gcd(a: int, b: int) -> int:
    return a if b == 0.0 else gcd(b, a % b)
by (1,656 points)
selected by
0 votes

You can use the multiplication method
https://youtu.be/lEpWjfPxhgY

In this video they also explain in medoto GCD

print(simplify_by_multiplication(14, 21))

func simplify_by_multiplication(numerator, denominator) -> Array:
    var simplify_values := [numerator, denominator]

    for i in range(1, numerator+1):
        for j in range( 1, denominator+1 ):
            print( 'i:', i, ' x j:', j, ' = ', i * j)

            if(( i * denominator ) == (j * numerator)):
                simplify_values = [i, j]
                return simplify_values

    return simplify_values
by (22 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.