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.

Some programming languages provide a built-in (primitive) rational data type to represent rational numbers and to do arithmetic on them (e.g. the ratio type of Common Lisp and analogous types provided by most languages for algebraic computation, such as Mathematica and Maple).

Many languages that do not have a built-in rational type still provide it as a library-defined type.

86 questions
1
vote
1 answer

Implementing rational numbers in c++ / numerator bigger than the denominator

I have following problem. I wrote an class for rational numbers in c++. The class and the operators work fine, unless the numerator is bigger than the denominator. I used for example -3/12 and 4/3 =13/12 but instead i get 3/4 The header…
PeterFox
  • 19
  • 3
1
vote
1 answer

Sudden rational result from devision in Ruby on Rails

When doing the devision in IRB: 6/3600 the result is always 0. But in a live Rails application running in WEBrick or Apache/Passenger the same division results in 1/600 value of type Rational. I am getting the same Rational result when doing…
Paul
  • 25,812
  • 38
  • 124
  • 247
1
vote
1 answer

Clone Method Java

I'm trying to create a clone method that will take an already created Rational and make a copy so that in my main I can do something like this: Rational r3 = new Rational(r1); So far I created a method in my Rational class that will take in a…
Bhetzie
  • 2,852
  • 10
  • 32
  • 43
1
vote
1 answer

Rational to double and GCF

This question is towards the last method. Whenever I return d in the method "public static Rational convert(double d)" it says that it can not convert from double to Rational. How would I make it so that I would be able to return a Rational number…
1
vote
1 answer

conversion error from boost cpp_rational to int

boost cpp_rational seems to convert wrong to int when the numerator and denominator have many digits. #include #include using namespace boost::multiprecision; using namespace std; int main() { …
WRF
  • 127
  • 10
1
vote
2 answers

Storing Fractions As Rational Python

As a project, I am creating a Rational Class from scratch that can take two fractions as input and store the simplified fraction. However, when I attempt to input two fractions, it appears to use integer division implicitly, so I can't…
1
vote
0 answers

Trim decimal digits

Possible Duplicate: Truncate a decimal value in c++ How can I truncate some of the decimal digits of a rational number so that it has only 6 digits? (For example 1.2345678909876 to 1.23456)
fpiro07
  • 847
  • 1
  • 13
  • 18
1
vote
2 answers

C# Printing Rational Number With High Resolution

I have the following conundrum. In C#, I want to store a rational and get a string representation of it's decimal notation. Normally I would just use float or double to store the number and get the string but I would like a "high resolution"…
recursion.ninja
  • 5,377
  • 7
  • 46
  • 78
1
vote
3 answers

Convert decimal to fraction (rational number) in Objective C?

As part of a calculator app, I am trying to implement uses with sigma notation. However, the result it prints out is always a decimal, and the rest isn't important. I simply want to change the decimal to a fraction. I already have the reduce…
Jtaylorapps
  • 5,680
  • 8
  • 40
  • 56
0
votes
2 answers

What is the most efficient way to determine the Farey sequence of degree n?

I am going to implement a Farey fraction approximation for converting limited-precision user input into possibly-repeating rationals. http://mathworld.wolfram.com/FareySequence.html I can easily locate the closest Farey fraction in a sequence, and I…
Matthew
  • 10,244
  • 5
  • 49
  • 104
0
votes
0 answers

Rational numbers appear in reduced forms

I want to save a rational number 3/12 in a variable using this code: rNo = Rational(3, 12) # => 1/4 but ruby reduces the number to 1/4. Can anybody please tell me how to keep the numbers as I write them?
FDI
  • 771
  • 1
  • 9
  • 25
0
votes
0 answers

C++ | Rational numbers operations

In the next code I can input a number n, next to this I can input n pair of numbers with a operator. Input 4 1 2 + 3 2 - 4 6 * 2 1 . Pairs means --> numerator and denominator --> n/d At the end we must input a ''.'' char to do the operation and…
0
votes
1 answer

Rational class and Move Semantics not working

I have tried to add more features to already given class(the answer of @Aak) in the problem: How to output fraction instead of decimal number? for printing fractions in numerator / denominator . First of all, the given code was not working without…
user9267359
0
votes
0 answers

How to achieve "1 - 1/3 - 1/3 - 1/3 = 0"?

I want to be able to subtract 1/3 from my variable in a mathematically correct way, meaning: If a 1D is subtracted by 1/3 three times, it should be 0D. However, I keep ending up with some tiny value (1.1102230246251565E-16) I've already tried…
RazorHail
  • 431
  • 1
  • 5
  • 17
0
votes
2 answers

When trying to print division, it converts fractions to decimals

I am coding a calculator and I was trying to print the division equation's answer, but instead it converted fractions to decimals. I am running Lua 5.2.4 print ("\n\tWhat math symbol will you use?") ms = io.read() -- Main function function…