-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4803d95
commit 44b1b5e
Showing
3 changed files
with
62 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/org/epicycloide_back/epicycloide_back/util/FractionConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.epicycloide_back.epicycloide_back.util; | ||
|
||
public class FractionConverter { | ||
|
||
static long gcd(long a, long b) { | ||
if (a == 0) | ||
return b; | ||
else if (b == 0) | ||
return a; | ||
if (a < b) | ||
return gcd(a, b % a); | ||
else | ||
return gcd(b, a % b); | ||
} | ||
|
||
public static Long[] decimalToFraction(double number) { | ||
|
||
// Fetch integral value of the decimal | ||
double intVal = Math.floor(number); | ||
|
||
// Fetch fractional part of the decimal | ||
double fVal = number - intVal; | ||
|
||
// Consider precision value to convert fractional part to integral equivalent | ||
final long pVal = 1000000000; | ||
|
||
// Calculate GCD of integral equivalent of fractional part and precision value | ||
long gcdVal = gcd(Math.round( | ||
fVal * pVal), pVal); | ||
|
||
// Calculate num and deno | ||
long num = Math.round(fVal * pVal) / gcdVal; | ||
long deno = pVal / gcdVal; | ||
|
||
return new Long[]{num, deno}; | ||
} | ||
} |