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.

Filter by
Sorted by
Tagged with
183votes
23answers
297kviews

range() for floats

Is there a range() equivalent for floats in Python? >>> range(0.5,5,1.5) [0, 1, 2, 3, 4] >>> range(0.5,5,0.5) Traceback (most recent call last): File "<pyshell#10>", line 1,...
user avatar
102votes
26answers
113kviews

Best way to represent a fraction in Java?

I'm trying to work with fractions in Java. I want to implement arithmetic functions. For this, I will first require a way to normalize the functions. I know I can't add 1/6 and 1/2 until I have a ...
98votes
6answers
117kviews

How to convert a decimal number into fraction?

I was wondering how to convert a decimal into a fraction in its lowest form in Python. For example: 0.25 -> 1/4 0.5 -> 1/2 1.25 -> 5/4 3 -> 3/1
user avatar
  • 6,820
68votes
11answers
78kviews

How to write fraction value using html?

I want to write fraction value such as the picture below: How do I write fraction value using html without using image? NOTE: I don't want this 1 1/2 pattern but strictly just as the pic above
user avatar
  • 1,705
50votes
8answers
19kviews

Is there a JavaScript function that reduces a fraction

say we have fraction 2/4, it can be reduced to 1/2. Is there a JavaScript function that can do the reducing?
user avatar
  • 13.8k
46votes
1answer
40kviews

Print number as reduced fraction in R

On my old beat-up TI-83 I can get a reduced fraction representation of a rational real number with the following syntax. .14>Frac 7/50 Is there a similar syntax, function, or CRAN ...
user avatar
45votes
2answers
33kviews

Does Python have a function to reduce fractions?

For example, when I calculate 98/42 I want to get 7/3, not 2.3333333, is there a function for that using Python or Numpy?
user avatar
  • 10.2k
36votes
10answers
20kviews

Is there a reliable way in JavaScript to obtain the number of decimal places of an arbitrary number?

It's important to note that I'm not looking for a rounding function. I am looking for a function that returns the number of decimal places in an arbitrary number's simplified decimal representation. ...
user avatar
  • 2,754
30votes
17answers
29kviews

Convert Fraction String to Decimal?

I'm trying to create a javascript function that can take a fraction input string such as '3/2' and convert it to decimal—either as a string '1.5' or number 1.5 function ratio(fraction) { var ...
user avatar
  • 46.2k
26votes
1answer
15kviews

Is there any way to access denominator of a fraction in Python 2

Is there any way to access the denominator in a fraction in Python 2? For example, I have a variable: x = fractions.Fraction(4,1) And I want to access its denominator. How do I do that?
user avatar
  • 1,606
24votes
11answers
26kviews

How to display "Classic" built-up fractions with an horizontal line in CSS / JavaScript?

I have a fraction and I want to display it neatly and nicely. For example 4/5 would be 4 — 5 I have looked at this and while this solution is decent the problem lies in having the 4 and the 5 in ...
user avatar
  • 7,900
24votes
5answers
31kviews

Split decimal variable into integral and fraction parts

I am trying to extract the integral and fractional parts from a decimal value (both parts should be integers): decimal decimalValue = 12.34m; int integral = (int) decimal.Truncate(decimalValue); int ...
user avatar
  • 1,853
23votes
15answers
31kviews

PHP convert decimal into fraction and back?

I want the user to be able to type in a fraction like: 1/2 2 1/4 3 And convert it into its corresponding decimal, to be saved in MySQL, that way I can order by it and do other comparisons to it. ...
user avatar
  • 53.1k
22votes
4answers
5kviews

regex to match irreducible fractions

How can I match irreducible fractions with regex? For example, 23/25, 3/4, 5/2, 100/101, etc. First of all, I have no idea about the gcd-algorithm realization in regex. Update for all of you who's ...
user avatar
19votes
11answers
37kviews

Is there any controls available for star rating?

Is there any controls available for star rating in ios ? So i can use that in UITableviewCell ?? I know there are some open open source controls like DYRating etc.. But iam not able to add it in ...
user avatar
  • 1,241
19votes
12answers
29kviews

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 ...
user avatar
18votes
2answers
7kviews

What's the difference between the classes Floating and Fractional in Haskell?

What is the difference is between the Floating and Fractional classes in Haskell?
user avatar
  • 285
16votes
6answers
4kviews

How to rendering fraction in Swing JComponents

Basically Swing JComponents are able to display numbers in fractions in this form 2 2/3. How can I paint fraction in the nicest form, for example 2⅔? . EDIT . as see I have only one way JTable ...
user avatar
  • 109k
14votes
1answer
3kviews

Best way to convert fractions.Fraction to decimal.Decimal?

In Python, the fractions.Fraction and decimal.Decimal standard library classes exist to help keep arithmetic with rational numbers precise. For the unfamiliar, an example of where it helps: >>&...
user avatar
  • 3,861
13votes
3answers
8kviews

Decimal to Fraction conversion in Swift

I am building a calculator and want it to automatically convert every decimal into a fraction. So if the user calculates an expression for which the answer is "0.333333...", it would return "1/3". For ...
user avatar
  • 18.3k
13votes
1answer
96kviews

Call to non-static member function without an object argument

Can anyone explain this error any why I am getting it? I believe I've got all of the arithmetic down in my separate class. This program stores and manipulates fractions, while testing overloading. ...
user avatar
13votes
2answers
7kviews

What algorithm does Python employ in fractions.gcd()?

I'm using the fractions module in Python v3.1 to compute the greatest common divisor. I would like to know what algorithm is used. I'm guessing the Euclidean method, but would like to be sure. The ...
user avatar
  • 22.5k
13votes
7answers
11kviews

Finding the closest integer fraction to a given random real between 0..1, given ranges of numerator and denominator

Given two ranges of positive integers x: [1 ... n] and y: [1 ... m] and random real R from 0 to 1, I need to find the pair of elements (i,j) from x and y such that x_i / y_j is closest to R. What is ...
user avatar
12votes
5answers
10kviews

What is the best way to separate double into two parts "integer & fraction" in java

I have tried to separate 5.6 (for example) by the following method: private static double[] method(double d) { int integerPart = 0; double fractionPart = 0.0; integerPart = (int) d; ...
user avatar
  • 111k
12votes
2answers
23kviews

How to use numpy arrays with fractions?

I'm trying to implement the simplex method in Python so I need to use the Gaussian elimination on arrays. Very often fractions come up and for more clarity and precision I would like to keep the ...
user avatar
  • 157
12votes
2answers
2kviews

How can I make a "working" repeating decimal representation of a rational number?

I've figured out how to display the repeating part of a repeating decimal using OverBar. repeatingDecimal doesn't actually work as a repeating decimal. I'd like to make a variation of it that looks ...
10votes
10answers
39kviews

How to convert decimal to fractions?

What I need to convert decimal to fractions. It is easy to convert to 10's feet. 1.5 => 15/10 This can do via this code: public class Rational { private int num, denom; public Rational(...
user avatar
  • 1,846
10votes
4answers
2kviews

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 ...
user avatar
10votes
6answers
7kviews

Converting a float into a string fraction representation

In Java, I am trying to find a way to convert a float number into a fraction string. For example: float num = 1.33333; String numStr = Convert(num); // Should return "1 1/3" float num2 = 1.333; ...
user avatar
  • 45.4k
10votes
2answers
1kviews

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 ...
user avatar
9votes
7answers
5kviews

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 ...
user avatar
9votes
3answers
591views

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(...
user avatar
  • 14.6k
8votes
13answers
43kviews

How to output fraction instead of decimal number?

In C++, When I calculate 2/3, it will output decimal values, how can I just get the original format (i.e.g 2/3) instead of 0.66666667 Thanks
user avatar
  • 3,989
8votes
5answers
18kviews

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 ...
user avatar
  • 13.6k
8votes
4answers
181views

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 ...
user avatar
  • 54.7k
8votes
2answers
8kviews

Python float to ratio

I try get ration of variable and get unexpected result. Can somebody explain this? >>> value = 3.2 >>> ratios = value.as_integer_ratio() >>> ratios (3602879701896397, ...
user avatar
  • 1,206
8votes
3answers
13kviews

How to keep fractions in your equation output

I've been using Python to calculate math equations. For example: from sympy import Symbol, Derivative, Integral x = Symbol('x') d = Symbol('d') Integral(8*x**(6/5)-7*x**(3/2),x).doit() Which results ...
user avatar
  • 1,509
8votes
3answers
18kviews

Fractions in Python

Is there anyway to compute a fraction, e.g. 2/3 or 1/2, in Python without importing the math module? The code snippet is simple: # What is the cube root of your number n = float(raw_input('Enter a ...
user avatar
  • 723
8votes
3answers
6kviews

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 . ...
user avatar
  • 25k
8votes
4answers
6kviews

How to convert a string representing a binary fraction to a number in Python

Let us suppose that we have a string representing a binary fraction such as: ".1" As a decimal number this is 0.5. Is there a standard way in Python to go from such strings to a number type (whether ...
user avatar
  • 1,446
8votes
2answers
812views

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, ...
user avatar
  • 2,949
8votes
2answers
2kviews

Convert a character vector of mixed numbers, fractions, and integers to numeric

I'm trying to write an R function to convert fractions and mixed numbers to decimals. e.g. mixedToFloat <- function(x){ x <- sub(' ', '+', x, fixed=TRUE) return(unlist(lapply(x, ...
user avatar
  • 28.4k
8votes
1answer
4kviews

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 ...
user avatar
  • 10.7k
7votes
5answers
3kviews

compare fraction without overflow

I am coding in C++. I am given 2 fractions, a/b and c/d where a,b,c,d are int. Does anyone know of a way to do a/b>c/d without overflow. For example, if I set a,b,c,d as the 4 largest primes less than ...
user avatar
  • 341
7votes
4answers
5kviews

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 ...
user avatar
7votes
3answers
7kviews

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'...
user avatar
7votes
4answers
3kviews

Convert percentage to nearest fraction

We have a very data intensive system. It stores raw data, then computes percentages based on the number of correct responses / total trials. Recently we have had customers who want to import old data ...
user avatar
  • 31.5k
7votes
4answers
2kviews

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 ...
user avatar
  • 32.4k
7votes
2answers
84views

Algorithm to round any proportion into its closest 1/r form

When calculating a proportion (0 < x < 1) I am looking to convert that result x into it's nearest 1/r form, so that for example for x = 0.30 is converted into 1/3 whereas for x = 0.29 is ...
user avatar
7votes
2answers
6kviews

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, ...
user avatar
  • 2,004

15 30 50 per page
1
2 3 4 5
15