rational-number

Simplifying Fractions With java

纵饮孤独 提交于 2019-12-13 06:29:15
问题 Hey guys I am working on a SW here I am kinda in need of help, you see we need to make a method where we are gonna simplify fractions. any idea how? here's my code as of now (don't mind the dvalue Method it is already finsih all I need is the simplify method) public class Fraction { public int num; public int den; public double dValue; public void display() { System.out.println("Numerator: "+num); System.out.println("Denominator: "+den); } public double dValue() { dValue = (double)num/den;

Can you implement and override the list.sort() method to sort a list of rational numbers?

耗尽温柔 提交于 2019-12-11 10:47:52
问题 So I've been given the following problem: Write a program that creates a List of Rationals and sorts them into increasing order. Use appropriate methods from the Collections Framework classes to sort the elements into increasing order. I've created a 'Rational' class to represent rational numbers and I've also made the list of random Rational numbers. But I'm having trouble figuring out a way to implement a method of sorting the list. Here's samples of the code before I go on further: public

boost rational_cast with rounding?

半腔热情 提交于 2019-12-10 14:17:37
问题 How can I do a rational_cast<int64_t> with rounding? Currently I'm doing a hack like this: boost::rational<int64_t> pts = ..., time_base = ...; int64_t rounded = std::llround(boost::rational_cast<long double>(pts / time_base)); But I'd like to be able to do it "properly" without involving floating point. 回答1: Rounding is inherently lossy. The quickest hack that comes to mind is simply using the built-in behaviour (which is floor -ing or trunc -ing the result) and offset by a half: Live On

How to convert a Rational into a “pretty” String?

依然范特西╮ 提交于 2019-12-04 04:06:40
I want to display some Rational values in their decimal expansion. That is, instead of displaying 3 % 4 , I would rather display 0.75 . I'd like this function to be of type Int -> Rational -> String . The first Int is to specify the maximum number of decimal places, since Rational expansions may be non-terminating. Hoogle and the haddocks for Data.Ratio didn't help me. Where can I find this function? xnyhps Here is an arbitrary precision solution that doesn't use floats: import Data.Ratio display :: Int -> Rational -> String display len rat = (if num < 0 then "-" else "") ++ (shows d ("." ++