/* * $Id: Rational.java,v 1.75 2007/02/09 20:07:44 rl Exp $ ********************************************************* * kaleido * * Kaleidoscopic construction of uniform polyhedra * Copyright © 1991-2007 Dr. Zvi Har’El * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * “This product includes software developed by * Dr. Zvi Har’El (http://www.math.technion.ac.il/~rl/).” * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * This software is provided ‘as-is’, without any express or implied * warranty. In no event will the author be held liable for any * damages arising from the use of this software. * * Author: * Dr. Zvi Har’El, * Technion, Israel Institue of Technology, * Haifa 32000, Israel. * E-Mail: rl@math.technion.ac.il ********************************************************** */ package kaleido; /** * Class Rational * @author Zvi Har’El * @version $Id: Rational.java,v 1.75 2007/02/09 20:07:44 rl Exp $ * @see Class source code */ public class Rational { int numerator; int denominator; static final Rational ZERO = new Rational(0, 1); /** * Private Constructor. */ private Rational(int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; } /** * Find the numerator and the denominator using the Euclidean * algorithm. The resulting rational is reduced. */ Rational(double x) { this(1, 0); Rational r = ZERO; double s = x; for (;;) { if (Math.abs(s) > (double) Integer.MAX_VALUE) return; int f = (int) Math.floor(s); Rational r0 = r; r = new Rational(numerator, denominator); long n = numerator * f + r0.numerator; if (Math.abs(n) > Integer.MAX_VALUE) return; numerator = (int) n; n = denominator * f + r0.denominator; if (Math.abs(n) > Integer.MAX_VALUE) return; denominator = (int) n; if (x == doubleValue()) return; s = 1 / (s - f); } } double doubleValue() { return (double) numerator / denominator; } Rational compl() { return new Rational(numerator, numerator - denominator); } boolean isCompl(Rational r) { return numerator == r.numerator && numerator == denominator + r.denominator; } boolean isInteger() { return denominator == 1; } boolean isFinite() { return denominator != 0; } boolean isStar() { return denominator != 1 && denominator != numerator - 1; } boolean isValid() { return numerator > denominator; } boolean equals(Rational r) { return numerator == r.numerator && denominator == r.denominator; } boolean equals(int i) { return numerator == i && denominator == 1; } public String toString() { if (!isFinite()) return "Infinity"; if (isInteger()) return "" + numerator; return numerator + "/" + denominator; } boolean equals(double x) { return equals(new Rational(x)); } static double compl(double x) { return x / (x - 1); } static int numerator(double x) { return new Rational(x).numerator; } static int denominator(double x) { return new Rational(x).denominator; } static boolean isInteger(double x) { return new Rational(x).isInteger(); } static boolean isStar(double x) { return new Rational(x).isStar(); } static String toString(double x) { return new Rational(x).toString(); } }