Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [rational-numbers]

In mathematics, a rational number is any number that can be expressed as the quotient or fraction p/q of two integers, with the denominator q not equal to zero.

3
votes
3answers
106 views

Beginner radian/degree converter program

I'm a bit of a beginner when it comes to Java, and I created this small code to help me grasp a few small subjects. It allows its user to convert a radical to a degree measure and a degree measure to ...
8
votes
2answers
3k views

A simple Fraction class

My code seems to work fine. However, I feel like it is a bit too long (fraction.cpp). I really want to create simple, readable, and efficient code. Sample Run: Here is the fraction.h file: ...
4
votes
2answers
1k views

Calculate with fractions

This program is a solution to homework given by our professor. So I want my program to be the most beautiful program ever written. Therefore I want to know where I could improve the quality of the ...
27
votes
5answers
9k views

A Fraction class in C++

I am trying to learn clear and concise C++. Currently I'm learning classes in school, and to practice I have made a Fraction class. I'd appreciate any help regarding style, cleanliness or anything ...
4
votes
1answer
3k views

Program to add two fractions in C

The task was: You’re given A/B and C/D. You will have to calculate A/B + C/D and answer it as E/F. Input specification On the first line you’re given the test case number N. Then there will ...
2
votes
3answers
947 views

Rational class with Java

Here is a class to handle fractions with Java using BigInteger because it supports gcd calculus. I don't know if BigInteger is the optimal use with fractions, but this is an alternative with fractions....
-1
votes
2answers
361 views

C++ fraction class *Updated

This fraction class is a project I'm working on for fun. I want it to the best it can be. Please let me know if I'm following best procedures or if I'm missing something. **Update So, I took the ...
5
votes
1answer
737 views

Counting the number of unique fractions read from a text file

I will begin this question with the disclaimer that it is homework but my code is written and working, I just want to know if there any ways in which I can improve what I already have. I want my ...
7
votes
2answers
346 views

Fraction type in Rust

Inspired by another question here on Code Review, I decided to try implementing a Fraction type in Rust. Requirements: Able to be added, subtracted, multiplied, ...
8
votes
4answers
6k views

Implementation of a Fraction class in Java

I am writing a bunch of Math related classes:BigInteger,Fraction,matrix,vector,polynomial,set etc.All of these classes will use each other.The classes will either work in decimal format or in Rational ...
17
votes
4answers
4k views

A minimal Fraction class in C++

I am trying to learn idiomatic Modern C++. I was wondering how you would improve upon this Fraction class. I would really appreciate any help with code organization, cleanliness and anything else that ...
5
votes
1answer
281 views

Efficiently finding approximate fraction, with tolerance for fp rounding

I needed to convert floating point numbers to int/int fractions. Python technically has this functionality, but has issues with rounding errors: ...
7
votes
2answers
6k views

Implementation of a Rational Number class in C++

First of all, disclaimer; this is for a school assignment. I hope I'm not doing anything inappropriate by asking for input on it, but if it makes a difference, the assignment has already been ...
7
votes
2answers
139 views

4 distinct integers, whose reciprocals sum up to 1

I wrote the following short python program to solve a math puzzle, which asked for all the 4-tuples of distinct natural numbers whose reciprocals sum up to 1. ...
2
votes
2answers
78 views

Fraction class with documentation

I have never worked with Ruby before, and thought I'd start working on learning it. For my first little implementation, I have created a simple fraction class. It would be nice if someone would check ...
2
votes
1answer
332 views

Number representation for a calculator that works with very big numbers

I'm a beginner: I have learned concepts and features which I think I need in my project, but this is my first serious program and it is my internship project. I'm totally confused and need help to ...
2
votes
2answers
591 views

C++ operator overloading for rationalNumber [closed]

I have created a rationalnumber class and used operator overloading function of C++. My questions are: Have I implemented the class and ...
4
votes
2answers
164 views

Number class to add/compare different kinds of numbers

I am creating a Number class having Integer and Fraction derive class which add/compare different kinds of numbers. In addition, the caller need not needs to know ...
3
votes
2answers
168 views

Clojure performance for solving Project Euler 72 (counting proper reduced fractions)

This is a solution of Project Euler 72 in Java. How may proper reduced fractions \$\dfrac{n}{d}\$ are there, where \$n < d \le 10^6\$? ...
5
votes
3answers
317 views

Converting a decimal into a fraction

I have written a small program that converts a decimal number into a reduced fraction representation. I also designed this little program to be fairly robust, so that it can give the correct output ...
7
votes
1answer
95 views

Fraction or integer parser

Question How can the following implementation of fractionParse be made less ugly? (Preserving unreduced numerators and denominators is supposed to be a feature---...
2
votes
1answer
124 views

A utility class for fractions

I've been working though Project Euler problems using JavaScript, and along the way I've found it useful to create "classes" for different types of numbers (like an integer that can be arbitrarily ...
4
votes
2answers
376 views

Fraction class in Java - follow-up

(See the previous iteration.) I have improved my Fraction implementation taking the answers in the previous iteration into account. Differences Both numerator ...
10
votes
5answers
2k views

Fraction class in Java

(See the next iteration.) I have this class for representing exact fractions. See what I have: Fraction.java: ...
0
votes
1answer
3k views

Add two fractions or two integers using template class and overloading

Question: Need to create three classes. Number Fraction Integer Requirements The first class should support “display”, “==”, and “+”. "Display" : This operation displays the Number itself in its ...
3
votes
2answers
421 views

Decimal-to-fraction solver

I have created a program that turns a decimal into a fraction: ...
2
votes
1answer
118 views

Converting fraction notation in strings to a Rational data type

I've been messing around with a RationalInt type, using a simple int-based structure for the time being. I've created code to ...
8
votes
3answers
720 views

Risk battle simulator v1

I've written a program to find the odds of either side winning a battle in Risk, and how many men they would have left. I've finished, but I feel that it can be improved since it doesn't function if ...
6
votes
2answers
2k views

Scala fraction addition

This just adds fractions. For example, if we have a text file with: 1/3 1/2 It will print: 5/6 I would love to hear some ...
3
votes
2answers
112 views

Rational arithmetic from given input

I'm currently working on this for my own practice. I get 4 digits and an operation. The input is x1 y1 op x2 y2 and the fraction is ...
4
votes
2answers
2k views

Rational number implementation

I've been recently working on a custom rational number implementation. Due to not very interesting reasons, using this implementation is not an option. I'd appreciate comments of anyone who has the ...
3
votes
2answers
983 views

Decimal expansion of a rational number

I wrote a program that does division and either does or doesn't show repeating groups. After designing it the first time I had to redesign it because although it worked it was unnecessarily complex. ...
14
votes
7answers
2k views

My Rational struct, version 2

I implemented most of the suggestions from Version 1. Thanks to all who took time to review and offer really good comments. ...
8
votes
4answers
506 views

My Rational struct, version 1

I have written a Rational struct for working with rational numbers, i.e. Rational(int numerator, int denominator). A recent ...
-1
votes
4answers
1k views

Finding the lowest terms for fractions in C#

I have been working on this Rational Operations calculator where you can input two rational numbers and can add, subtract, multiply and divide and the final output is the reduced answer. For example: ...
8
votes
2answers
1k views

Unit Tests for a Fraction Class

As my first Java homework, I wrote a class to handle fractions. This Fraction class implements basic operations for the fractions (addition, subtraction, ...
2
votes
2answers
2k views

Fraction Class in Python

I decided to train myself in OOP with a simple perfect precision Fraction class. ...
-1
votes
1answer
574 views

“Data Abstraction” and “Abstract Data Type” for a rational number [closed]

Below is implementation 1 of data abstraction for rational number using functional paradigm written in Python: ...
12
votes
7answers
23k views

Mathematical functions on fractions

I have been challenged by @nhgrif to write a Fraction class to better my understanding of OOP. I have found an interesting challenge here (I apologize that the source is not mentioned, I was not able ...
2
votes
2answers
717 views

“Rational” Wrapper class with support for Rational Number Approximation

Enough with imprecise floating point datatypes called float anddouble. I present to you Rational. Of course, Rational number ...
6
votes
2answers
4k views

Converting fractions to mixed numbers

I've been making a program for an assignment that converts fractions to mixed numbers, and I'm done, but I was wondering if I could simplify it in any way, since it seems quite complicated right now. ...
1
vote
1answer
228 views

Rational class supporting precise numbers

This is my JavaScript Rational class, similar to other languages' BigFraction class, but with one major difference: This class ...
7
votes
4answers
21k views

Simplify a fraction

I have been learning Python for a while (my first programming language) in my spare time. Since it's been 20 years since math class, and I didn't get very far with that, I just brushed up on pre-...
3
votes
2answers
203 views

Project Euler Problem 57 in Haskell

I'm very new to Haskell, and I would like some feedback on my coding style. Is there anything here that could be made more elegant or concise? This is my solution to Project Euler Problem 57. It ...
0
votes
2answers
4k views

Fractions in Java [closed]

Part I-IV recommend code changes as mentioned in link 1, for the initial code given at link 2. Can you please review the code changes for Part I-IV? Part I: Constructors (1 point) Modify the ...
10
votes
1answer
160 views

Yet Another Fraction

A recent question inspired me to implement a Fraction class. I decided to write this one in vba, because I like writing tools for the poor souls that still have to ...
10
votes
5answers
1k views

Using TryXXX pattern to avoid exceptions

I have been using the TryXXX and GetXXX pattern lately to give clients a choice whether to trap an exception or to read a boolean. An example of this concept would be ...
-5
votes
2answers
1k views

“Magic Fraction” finder

A Magic Fraction for N is one that has the following properties: It is a proper fraction (the value is < 1) It cannot be reduced further (the GCD of the numerator and the denominator is 1)...
4
votes
1answer
67 views
11
votes
2answers
2k views

A fraction of the code

Following up on this post, and including some major changes suggested, here's the revised code. Changes include: No longer keeping an IFormatProvider at instance ...