Jump to content

I've never ventured in the depths of math related stuff on android - I programmed only simple apps just for fun or personal need. Now, for the same reason, I want to create an Android app for school related work, and I need to display the result of a trigonometrical calculation not as a floating point number (nor a double), but as a fraction (something like 3pi/4, with "pi" as a symbol, not word). Any ideas on how to do this?

Computer Case: NZXT S340 || CPU: AMD Ryzen 5 1600 || Cooler: CM Hyper212 Evo || MoBo: MSI B350 Mortar || RAM Vengeance LPX 2x8GB 3200MHz || PSU: Corsair CX600 || SSD: HyperX Fury 120GB & 240GB || HDD: WD Blue 1TB + 1TB 2.5'' backup drive || GPU: Sapphire Nitro+ RX 580 4GB

Laptop 1 HP x360 13-u113nl

Laptop Lenovo z50-75 with AMD FX-7500 || OS: Windows 10 / Ubuntu 17.04

DSLR Nikon D5300 w/ 18-105mm lens

Link to comment
https://linustechtips.com/topic/582165-android-java-and-trigonometry/
Share on other sites

Link to post
Share on other sites

Do you need a math keyboard for an Android device like this?

https://play.google.com/store/apps/details?id=net.schwiz.wolfram.full&hl=en

 

You might find that because these apps are open source that you could just borrow the code that is needed to display the "pi" symbol, so long as you attribute the code source correctly.

Those who deny freedom to others deserve it not for themselves (Abraham Lincoln,1808-1865; 16th US president).

Link to post
Share on other sites

46 minutes ago, SydneySideSteveSomewheres said:

Do you need a math keyboard for an Android device like this?

https://play.google.com/store/apps/details?id=net.schwiz.wolfram.full&hl=en

 

You might find that because these apps are open source that you could just borrow the code that is needed to display the "pi" symbol, so long as you attribute the code source correctly.

Nope, the app will be somethin more like a calculator, but specific for simple trigonometrical calculations (sine, cosine, tangent and so on). But I want it to display the result as a fraction, not float or double.

 

10 minutes ago, BrightCandle said:

Think about it, how would you do this with just integers because that is the answer. Once you understand how to calculate fractions by hand then you just apply that in your programming language. Its just maths.

So I'll work with fractions as two different integers, right? Then I'll show the result in the UI as a fraction graphically. Sounds good, if i got it right

Computer Case: NZXT S340 || CPU: AMD Ryzen 5 1600 || Cooler: CM Hyper212 Evo || MoBo: MSI B350 Mortar || RAM Vengeance LPX 2x8GB 3200MHz || PSU: Corsair CX600 || SSD: HyperX Fury 120GB & 240GB || HDD: WD Blue 1TB + 1TB 2.5'' backup drive || GPU: Sapphire Nitro+ RX 580 4GB

Laptop 1 HP x360 13-u113nl

Laptop Lenovo z50-75 with AMD FX-7500 || OS: Windows 10 / Ubuntu 17.04

DSLR Nikon D5300 w/ 18-105mm lens

Link to post
Share on other sites

I just wrote this, it works fine except with periodic numbers (0.33333 -> 33333/100000), if I find a solution I'll update the code.

public class ToFraction
{
    final static int ERROR = 6;    // 10^-6 approximation
    
    static int[] toFraction(double num)
    {
        int[] fraction = new int[2];// Fraction to return
        int[] temp = new int[2];    // Represents the first decimal after the dot as a fraction
        int sign = 1;
        int i = 0;        // Iterator
        
        if(num < 0)       // If num is less than 0
        {
            sign = -1;   // Its sign is negative
            num = -num;  // num now is positive
        }
        
        temp[] = (int)num;    // Gets the int part     2.5 -> 2
        num -= temp[0];        // Subtracts it from num 2.5 - 2 = 0.5
        fraction[0] = temp[0];  // As a fraction         2 -> 2/1
        fraction[1] = 1;
        
        while(++i < ERROR)
        {
            num *= 10;                       // Shifts all decimals to left 0.5 -> 5.0
            temp[0] = (int)num;               // Gets the int part 5.0 -> 5
            num -= temp[0];                   // Subtraction         5.0 - 5 = 0.0
            temp[1] = (int) Math.pow(10, i); // Now temp is     5/10
            LowestTerms(temp);               // 5/10 -> 1/2
            
            fraction = Sum(fraction, temp);  // fraction + temp 2/1 + 1/2 -> 5/2
            LowestTerms(fraction);
        }
        
        fraction[0] *= sign; // Multiplies by the sign
        return fraction;    // Returns the result
    }
    
    private static int[] Sum(int[] a1, int[] a2)
    {
        int[] b = new int[2];
        
        b[0] = a1[0]*a2[1] + a2[0]*a1[1];
        b[1] = a1[1]*a2[1];
        
        return b;
    }
    
    public static void LowestTerms(int[] f)
    {
        int min = Math.min(f[0], f[1]);
        for(int i = 2; i <= min; i++)
            if(f[0] % i ==  && f[1] % i == )
            {
                f[0] /= i;
                f[1] /= i;
                i = 1;
            }
    }
}

 

Link to post
Share on other sites

If you need to return a fractional result for any arbitrary angle, e.g. sin(49.652), then your best bet is to work with the infinite series representation of each trig function.  Extremely few values of x make sin(x) return a nice fractional number--most would either have massive values in the numerator and denominator to get a decent approximation, and many more are just plain irrational.  Just look up the series expansion for your various trig functions (they exist for the regular and the inverse ones).  For sin, the series is:

sin x = x - x^3/(3!) + x^5/(5!) - x^7/(7!) ...

And so on and so forth for an infinite number of terms.  You'll have to pick some number of terms that gives you good enough accuracy, or just keep computing terms until the end value changes by less than some pre-defined amount, but then you can do pretty simple least common denominator math on the terms and get a (likely utterly atrocious) fractional representation for any value of x.  For some of the reference angles (which you really should just memorize if you're doing trig), you can hard code them as special cases.

 

If you just need to return the values of the reference angles (pi/6, pi/4, etc) as fractions, and you won't be using other values as your inputs, then either just memorize them so you don't need a calculator at all, or hard code all of them in.  You'd probably end up with basically a lookup function if you're hard-coding all your possible inputs, and it might be more convenient to make an app that just lists all the reference angle values for each function.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×