Exemplo n.º 1
0
        static frac to_frac(double x, int precision)
        {
            int denom = 1;
            for (int i = 0; i < precision; i++) {
                denom *= 10;
            }

            double num = x * denom + 0.5; // hack: round if imprecise
            int gcdiv = gcd((int)num, denom);

            frac f = new frac();
            f.num = (int)(num / gcdiv);
            f.denom = denom / gcdiv;

            return f;
        }
Exemplo n.º 2
0
        static frac to_frac(double x, int precision)
        {
            int denom = 1;

            for (int i = 0; i < precision; i++)
            {
                denom *= 10;
            }

            double num   = x * denom + 0.5; // hack: round if imprecise
            int    gcdiv = gcd((int)num, denom);

            frac f = new frac();

            f.num   = (int)(num / gcdiv);
            f.denom = denom / gcdiv;

            return(f);
        }