Questions tagged [fractions]

Fractions, or "fractional numbers" (lit. "broken numbers"), or "rational numbers", are numbers expressed as the ratio of two integers, such as 1/2 or 23/17.

In computing and programming, fractional (or "rational") numbers are an alternative to floating point numbers: Since numerator and denominator are stored as integers, the representation is exact, as opposed to most floating point implementations, which cannot represent most rational numbers exactly. Arbitrary-precision ("bignum") libraries usually contain support for rational numbers.

800 questions
10
votes
2 answers

How to find the maximum number of unique unit fractions that sum up to one

In my problem I have to write a program that calculates all the unique unit fractions (1/2 +1/3 + 1/6 = 1) that sum to one. I am currently at 570 fractions that sum to one in this format where the number is the denominator (this is my current…
Denlolsauce
  • 109
  • 5
10
votes
3 answers

Python float to ratio

I try to represent a floating point number as a ratio of two integers, but for some reason the integers that I get are quite different from what I would expect to see. Can somebody explain this? >>> value = 3.2 >>> ratios =…
itdxer
  • 1,236
  • 1
  • 12
  • 40
10
votes
2 answers

Convert Fractions to Words

I'd like to know if there is a neat way to convert 1/2, 1/4, 5/8 into words (half, quarter, five eighths) using PHP. I've got a product data feed, and need to create a clean URL for each product based on the name. Some have measurements like 1/4"…
Alexander Holsgrove
  • 1,795
  • 3
  • 25
  • 54
9
votes
3 answers

fractions.Fraction() returns different nom., denom. pair when parsing a float or its string representation

I am aware of the nature of floating point math but I still find the following surprising: from fractions import Fraction print(Fraction(0.2)) # -> 3602879701896397/18014398509481984 print(Fraction(str(0.2))) # ->…
Ma0
  • 15,057
  • 4
  • 35
  • 65
9
votes
4 answers

Convert fraction to string with repeating decimal places in brackets

I want to write a function in Python 3 that converts fractions given as numerator and denominator into their string representation as decimal number, but with repeating decimal places in brackets. An example: convert(1, 4) should output…
Byte Commander
  • 6,506
  • 6
  • 44
  • 71
9
votes
7 answers

Convert list of Fractions to floats in Python

I have a list of fractions, such as: data = ['24/221 ', '25/221 ', '24/221 ', '25/221 ', '25/221 ', '30/221 ', '31/221 ', '31/221 ', '31/221 ', '31/221 ', '30/221 ', '30/221 ', '33/221 '] How would I go about converting these to floats, e.g. data =…
user431392
  • 91
  • 1
  • 2
9
votes
1 answer

How is python's fractions.limit_denominator implemented?

limit_denominator(max_denominator=1000000) Finds and returns the closest Fraction to self that has denominator at most max_denominator. This method is useful for finding rational approximations to a given floating-point number: >>> >>> from…
thkang
  • 11,215
  • 14
  • 67
  • 83
9
votes
3 answers

How are fractions represented in computers?

Since computers think in terms of "1" and "0" how do they calculate and represent fractions such as 7.50 ? I know Java and JavaScript and if required for the answer you can use them as an example . Edit: I was watching this MIT video on hashing by…
Geek
  • 26,489
  • 43
  • 149
  • 227
8
votes
4 answers

Converting fractions to html entities

We've got some fraction information stored in the database, e.g. ¾ ½ Short of doing a search and replace, are there any inbuilt PHP functions that will automatically convert these to proper html entities?
Tom
  • 33,626
  • 31
  • 85
  • 109
8
votes
4 answers

Understanding Float>>asFraction and its variants

I'm currently puzzling over the response provided by the class method Float>>asFraction and its various forms. Here are a few examples: GNU Smalltalk 0.001 asFraction 1/1000 0.001 asExactFraction 1152921504606847/1152921504606846976 Pharo 0.001…
lurker
  • 56,987
  • 9
  • 69
  • 103
8
votes
2 answers

ULP unit of least precision

Can anyone explain ULP Unit of least precision? I have the following definition, but it is still not clear "The size of the error when representing fractions is proportional to the size of the number stored. The ULP or unit of least precision…
Aleks
  • 81
  • 1
  • 5
8
votes
1 answer

NumPy: convert decimals to fractions

I compute the reverse of matrix A, for instance, import numpy as np A = np.diag([1, 2, 3]) A_inv = np.linalg.pinv(A) print(A_inv) I got, [[ 1. 0. 0. ] [ 0. 0.5 0. ] [ 0. 0. …
SparkAndShine
  • 17,001
  • 22
  • 90
  • 134
8
votes
2 answers

Reading fractions in csv file with R

I have a text file of numerical data, with headers, where some numbers are entered as fractions, some are entered as integers, and some are entered as floats, e.g.: col1name, col2name, col3name, col4name 1, 2, 3, 4 0.5, 0.6, 0.7, 0.8 1/2, 2/3,…
Ben S.
  • 3,415
  • 7
  • 22
  • 43
8
votes
3 answers

Linear system solution with fractions in numpy

I have matrix A and a right-hand-side vector y expressed in terms of fractions.Fraction objects: import random, fractions, numpy as np A = np.zeros((3, 3), dtype=fractions.Fraction) y = np.zeros((3, 1), dtype=fractions.Fraction) for i in range(3): …
Spiros
  • 2,156
  • 2
  • 23
  • 42
8
votes
5 answers

How to simplify fractions in C#?

I'm looking for a library or existing code to simplify fractions. Does anyone have anything at hand or any links? P.S. I already understand the process but really don't want to rewrite the wheel Update Ok i've checked out the fraction library on…
Andrew Harry
  • 13,773
  • 18
  • 67
  • 102
1 2
3
53 54