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
78
votes
8 answers

The "guess the number" game for arbitrary rational numbers?

I once got the following as an interview question: I'm thinking of a positive integer n. Come up with an algorithm that can guess it in O(lg n) queries. Each query is a number of your choosing, and I will answer either "lower," "higher," or…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
33
votes
4 answers

Design principles behind std::ratio<>

I was looking at the class std::ratio<> from the C++11 standard that allows to make compile-time rational arithmetic. I found the template design and the operations implemented with classes overly complex and did not find any reason why they could…
Morwenn
  • 21,684
  • 12
  • 93
  • 152
25
votes
6 answers

simplifying fractions in Java

My task is to develop a rational class. If 500 and 1000 are my inputs, then (½) must be my output. I have written a program on my own to find it. Is there another best way to find the solution, or my program is already the best one? public class…
Pari Sairam Mohan
  • 421
  • 1
  • 4
  • 11
19
votes
12 answers

Convert a decimal number to a fraction / rational number

In JavaScript, is there any way to convert a decimal number (such as 0.0002) to a fraction represented as a string (such as "2/10000")? If a function called decimalToFraction had been written for this purpose, then decimalToFraction(0.0002) would…
Anderson Green
  • 30,230
  • 67
  • 195
  • 328
17
votes
7 answers

Converting float decimal to fraction

I am trying to convert calculations keyed in by users with decimal results into fractions. For e.g.; 66.6666666667 into 66 2/3. Any pointers? Thanx in advance
Joe Shamuraq
  • 1,245
  • 3
  • 18
  • 32
16
votes
3 answers

Efficiently detect that rational numbers are equal

I have a collection of many rational numbers, with the numerator and denominator of each stored as a large (hundreds or thousands of bits) unsigned integer. I'd like to be able to efficiently test whether any given rational number a/b in the set is…
Sneftel
  • 40,271
  • 12
  • 71
  • 104
15
votes
7 answers

Algorithm for detecting repeating decimals?

Is there an algorithm for figuring out the following things? If the result of a division is a repeating decimal (in binary). If it repeats, at what digit (represented as a power of 2) does the repetition start? What digits repeat? Some…
Imagist
  • 18,086
  • 12
  • 58
  • 77
12
votes
3 answers

How to parse a decimal fraction into Rational in Haskell?

I've been participating in a programming contest and one of the problems' input data included a fractional number in a decimal format: 0.75 is one example. Parsing that into Double is trivial (I can use read for that), but the loss of precision is…
Rotsor
  • 13,655
  • 6
  • 43
  • 57
12
votes
1 answer

Coq QArith division by zero is zero, why?

I noticed that in Coq's definition of rationals the inverse of zero is defined to zero. (Usually, division by zero is not well-defined/legal/allowed.) Require Import QArith. Lemma inv_zero_is_zero: (/ 0) == 0. Proof. unfold Qeq. reflexivity.…
larsr
  • 5,447
  • 19
  • 38
11
votes
4 answers

Find the simplest rational number between two given rational numbers

I found a problem related to rational numbers. Two rational numbers are given and the task is to find the simplest rational number between them. For this problem, the simplicity of a rational number could be defined as the rational number with the…
Tom Pažourek
  • 9,582
  • 8
  • 66
  • 107
10
votes
10 answers

How to convert rational and decimal number strings to floats in python?

How can I convert strings which can denote decimal or rational numbers to floats >>> ["0.1234", "1/2"] ['0.1234', '1/2'] I'd want [0.1234, 0.5]. eval is what I was thinking but no luck: >>> eval("1/2") 0
ketorin
  • 1,034
  • 2
  • 12
  • 21
8
votes
2 answers

Ratio<,> is constant, but what if I want to accept different ratios as an argument?

Perhaps an oxymoronic question: ratio<,> is, by definition, a compile-time constant. However, I would like to construct durations with different ratios that can be specified by the caller of my method. I'm guessing I should be using something other…
Tatiana Racheva
  • 1,289
  • 1
  • 13
  • 31
8
votes
2 answers

Is there an implementation of _rational_ interval arithmetic in Python?

Is there an implementation of rational interval arithmetic in Python? This uses floats, not rationals. If not, is there any implementation of rationals in Python that includes ±∞ ?
user380772
8
votes
2 answers

Multiplying integer by rational without intermediate overflow

I have a struct representing a nonnegative rational number p/q: struct rational { uint64_t p; uint64_t q; // invariant: always > 0 }; I would like to multiply my rational by a uint64 n and get an integer result, rounded down. That is, I…
ridiculous_fish
  • 17,273
  • 1
  • 54
  • 61
7
votes
3 answers

C Fraction Arithmetic

I'm having great difficulty with the following that I need to do for an assign: a. Declare a data structure that contains a rational number. b. Write f'xns that will +, -, *, / rational numbers. All f'xns have to pass 3 parameters, each pointing to…
1
2 3 4 5 6