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
7
votes
3 answers

Pure Python rational numbers module for 2.5

Has anybody seen such a thing? Small self-sufficient modules are preferred.
Constantin
  • 27,478
  • 10
  • 60
  • 79
7
votes
1 answer

How to convert any number to a clojure.lang.Ratio type in Clojure?

In Scheme I can do: #;> (numerator 1/3) 1 #;> (denominator 1/3) 3 In Clojure I can do something similar: user=> (numerator 1/3) 1 user=> (denominator 1/3) 3 But in Scheme I can do: #;> (numerator 0.3) 3.0 and it is not possible in Clojure: user=>…
Felipe
  • 16,649
  • 11
  • 68
  • 92
6
votes
1 answer

Performant algorithm to rationalize floats

Given a floating point number, I'm looking to get a String representation of a rational number approximating the decimal (to within a given tolerance ε is fine). My current approach is as follows: String rationalize(double d) { String s =…
Andy Shulman
  • 1,895
  • 3
  • 23
  • 32
6
votes
1 answer

Why does Racket report that π is rational?

As any secondary math student can attest, pi is irrational. And yet: Welcome to Racket v5.3.6. > pi 3.141592653589793 > (rational? pi) #t Is this because the representation of pi, in the underlying machine's floating point format, is of limited…
Cognitive Hazard
  • 1,072
  • 10
  • 25
6
votes
2 answers

C++ input operator overload ">>"

I have a rational number class is made up of two integers: num, the nominator, and den, the denominator. The following operator is supposed to read the rational number from a stream. istream& operator >> (istream& Is, rational& r) { char c; //Test…
fpiro07
  • 847
  • 1
  • 13
  • 18
5
votes
2 answers

How do you represent fraction in F# without the loss of precision?

What is best way to represent fractions in F#? Haskell and Racket have convinient way to represent ratios. Is there a data type in F# to represent ratios?
unj2
  • 52,135
  • 87
  • 247
  • 375
5
votes
5 answers

How best to represent rational numbers in SQL Server?

I'm working with data that is natively supplied as rational numbers. I have a slick generic C# class which beautifully represents this data in C# and allows conversion to many other forms. Unfortunately, when I turn around and want to store this in…
mckamey
  • 17,359
  • 16
  • 83
  • 116
5
votes
2 answers

Why is the new method not needed for creating Rational in ruby

Possible Duplicate: Ruby syntax question: Rational(a, b) and Rational.new!(a, b) I'm in the process of reading the ruby pickaxe book, and I'm confused about the syntax of creating rational numbers. Rational(3,4) * Rational(1,2) produces =>…
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
4
votes
4 answers

Do any other languages have builtin rational types like scheme?

I haven't heard of any, most languages seem to just have division of ints round or be a floating point number. Was it found to be a problem in scheme and so not used in other languages?
Roman A. Taycher
  • 18,619
  • 19
  • 86
  • 141
3
votes
4 answers

Why do Haskell numerical literals need to start and end with digits?

In The Haskell 98 Report it's said that A floating literal must contain digits both before and after the decimal point; this ensures that a decimal point cannot be mistaken for another use of the dot character. What other use might this be? I…
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
3
votes
3 answers

finding a rational number with a property

I have to write a program to find a rational number that has a property. I wrote the code to check the property, but now I don't know how to check all rational numbers. I tried with float rat; for (int i=1 ; i ; ++i) { for (int j=1 ; j ; ++j) { …
user671486
  • 83
  • 1
  • 4
3
votes
1 answer

What is a realistic example showing the need for a Money class?

I've always heard that you should use a money class due to floating point inaccuracy. However, it is astonishingly hard to find any example where floating point inaccuracy actually leads to a wrong result. My programming language of choice is…
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
3
votes
1 answer

Implementation of rational numbers in Idris

Are there any existing implementations of rational numbers in Idris? E.g. Data.Ratio ports from Haskell.
stop-cran
  • 4,229
  • 2
  • 30
  • 47
3
votes
2 answers

Pattern-matching on Rationals in Haskell

The following function is pretty straightforward: test :: Int -> Int test x = case x of 0 -> 0 1 -> 1 _ -> 2 and indeed, test 0 == 0, test 1 == 1, and test 77 == 2. The following function is almost as straightforward: import…
3
votes
1 answer

Haskell Text.Json package can read but not write Rationals?

When I try to decode a JSON file with a floating point number, the Text.JSON package gives me the number as a JSRational. So, I can do a readJSON on a JSRational. However, I can't write rational numbers! Is this on purpose?
aceo
  • 31
  • 1