fractions

Optimized algorithm for converting a decimal to a “pretty” fraction

旧时模样 提交于 2020-01-02 04:39:09
问题 Rather than converting an arbitrary decimal to an exact fraction (something like 323527/4362363), I am trying to convert to just common easily-discernible (in terms of human-readability) quantities like 1/2, 1/4, 1/8 etc. Other than using a series of if-then, less than/equal to etc comparisons, are there more optimized techniques to do this? Edit: In my particular case, approximations are acceptable. The idea is that 0.251243 ~ 0.25 = 1/4 - in my usage case, that's "good enough", with the

Fractions instead of decimals

眉间皱痕 提交于 2019-12-31 06:07:26
问题 So, I am Writing this little program in c++, it's made to compute various values with lines (sorry i am french, i don't know how to say it in English, but they are the lines with equation types Y = kx + t). And I want my program to output fractions instead of decimals (2/3 instead of 0.666666666...). Can anyone tell me how ? I read online that there are some libraries for that purpose, can anyone help me on how to use them and/or how to implement them in my code ? Thanks :) #include "pch.h"

Converting a float into a string fraction representation

亡梦爱人 提交于 2019-12-30 17:39:10
问题 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; String numStr2 = Convert(num2); // Should also return "1 1/3" float num3 = 0.5; String numStr3 = Convert(num3); // Should return "1/2" float num4 = 2.25; String numStr4 = Convert(num4); // Should return "2 1/4" Any ideas how to do this in Java? 回答1: The simplest approach might be to use trial and error

Python float to ratio

风流意气都作罢 提交于 2019-12-28 16:03:49
问题 I try get ration of variable and get unexpected result. Can somebody explain this? >>> value = 3.2 >>> ratios = value.as_integer_ratio() >>> ratios (3602879701896397, 1125899906842624) >>> ratios[0] / ratios[1] 3.2 I using python 3.3 But I think that (16, 5) is much better solution And why it correct for 2.5 >>> value = 2.5 >>> value.as_integer_ratio() (5, 2) 回答1: Use the fractions module to simplify fractions: >>> from fractions import Fraction >>> Fraction(3.2) Fraction(3602879701896397,

Squeak Smalltalk, why sometimes the reduced method doesn't work?

蹲街弑〆低调 提交于 2019-12-23 22:01:12
问题 (2332 / 2332) reduced (2332 / 2) reduced (2332 / 322) reduced (1166/161) (2332 / 3) reduced (2332/3) (2332 / 2432423) reduced (2332/2432423) Look at the above codes. The first and second, when printed, do not work. The MessageNotUnderstood window pops up. And the 3rd, 4th, 5th code are okay. Results come out right. Why does the reduced method not work? Is it because the reduced method fails to handle final results which are integers like Uko guesses ? 回答1: Fractions are reduced automatically

Using Fractions in Python

送分小仙女□ 提交于 2019-12-23 04:38:33
问题 I'm using classes here to input a fraction (when given the numerator and denominator), as well as add and multiply two fractions together. For some reason, the imported fractions module only works correctly for part of the program; the gcd method works, but the Fraction method (where when given two numbers, puts into fraction format) does not work, instead throwing a NameError (specifically, "global name 'Fractions' is not defined"). What am I doing wrong? I am relatively new to Python, and

How can I write a complex fraction using HTML/CSS/jQuery?

时光怂恿深爱的人放手 提交于 2019-12-22 10:09:14
问题 I'd like to be able to write a fraction using HTML/CSS/jQuery (rather than using a TeX renderer or even MathML). At the moment there's a great workaround for writing simple fractions that works if you just have one term for both the numerator and the denominator, but once you start using more than one term it looks rather horrible. As an example, using: <sup><font size=-2>x<sup>2</sup> + 3x + 5</font></sup>/<sub><font size=-2>2x</font></sub> produces... x 2 + 3x + 5 / 2x What I'd like is a

Java - Converting hours(in double) to minutes(integer) and vice versa

人盡茶涼 提交于 2019-12-22 09:00:04
问题 I need the correct formula that will convert hours to minutes and vice versa. I have written a code, but it doesn't seem to work as expected. For eg: If I have hours=8.16, then minutes should be 490, but I'm getting the result as 489. import java.io.*; class DoubleToInt { public static void main(String[] args) throws IOException{ BufferedReader buff = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the double hours:"); String d = buff.readLine(); double hours =

Convert decimal amount to text string fraction in Javascript?

雨燕双飞 提交于 2019-12-22 08:57:22
问题 I have a value returned as a number, with could be decimal number e.g. "1.15". However, I need to format all numbers in a range to a given fraction. For example, all numbers greater than 0 but less than .2 I want to return "1/8". I already started to do this as a series of if/else statements, but I was wondering if there was a smarter and neater way. if (amt > 0 && amt <= .2){ q = '1/8'; } else if (amt > .2 && amt <= .3){ q = '1/4'; } else if (amt > .3 && amt <= .4){ q = '1/3'; } else if (amt

Does Python have a function to reduce fractions?

爱⌒轻易说出口 提交于 2019-12-22 01:33:21
问题 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 ? 回答1: The fractions module can do that >>> from fractions import Fraction >>> Fraction(98, 42) Fraction(7, 3) There's a recipe over here for a numpy gcd. Which you could then use to divide your fraction >>> def numpy_gcd(a, b): ... a, b = np.broadcast_arrays(a, b) ... a = a.copy() ... b = b.copy() ... pos = np.nonzero(b)[0] ... while len(pos) > 0: ... b2 = b[pos] ... a