fractions

Fraction length

三世轮回 提交于 2019-12-11 02:30:59
问题 How to get the fraction length? If at all possible, without using string operation or loop should all return length of 3: 5.234 5.23400 5.234000 Any programming language is accepted [EDIT] Not a homework, I want to display fractions at its minimum. Example, I defined numeric(18,8) in database. If the user entered only 5.234, the data saved in database is 5.23400000. I just want to display it back as 5.234 only 回答1: If Int(num) = num Then Return 0 num *= 10 If Int(num) = num Then Return 1 num

Excel fractions miscalculated

不羁岁月 提交于 2019-12-10 20:58:56
问题 I'm trying to calculate screen ratios in Excel. I have a column with x and y, an example of what I'd expect from the calculated ratio column is: x | y | ratio 1920 | 1080 | 16/9 but by using the formula =x/y and formatting the ratio column as a fraction I get: x | y | ratio 1920 | 1080 | 17/9 I assume this is something to do with rounding error, but is there any way around this, in order to show accurate screen resolution ratios? 回答1: @pnuts is right. You can change the output by using a

Float to Fraction conversion in Python

久未见 提交于 2019-12-10 16:35:15
问题 While doing exercise on the topic of float type to Fraction type conversion in Python 3.52, I found the difference between the two different ways of conversion. The first method is: >>> from fractions import Fraction >>> x = 1232.23 >>> f = Fraction(*x.as_integer_ratio()) >>> print(f) 2709702426188841/2199023255552 #Answer The second method is: >>> from fractions import Fraction >>> x = 1232.23 >>> f = Fraction(str(x)) >>> print(f) 123223/100 #Answer I want to know the reason behind these two

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

别来无恙 提交于 2019-12-10 04:06:52
问题 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 it is binary or decimal is not strictly important). For an integer, the solution is straightforward: int("101", 2) >>>5 int() takes an optional second argument to provide the base, but float() does not. I am looking for something functionally equivalent (I think) to this: def frac_bin_str_to_float

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

寵の児 提交于 2019-12-10 01:52:13
问题 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 docs (http://docs.python.org/py3k/library/fractions.html?highlight=fractions.gcd#fractions.gcd) don't help. Can anybody clue me in? 回答1: According to the 3.1.2 source code online, here's gcd as defined in Python-3.1.2/Lib/fractions.py : def gcd(a, b): """Calculate the Greatest Common Divisor of a

How can I turn a floating point number into the closest fraction represented by a byte numerator and denominator?

可紊 提交于 2019-12-09 01:16:59
问题 How can I write an algorithm that given a floating point number, and attempts to represent is as accurately as possible using a numerator and a denominator, both restricted to the range of a Java byte? The reason for this is that an I2C device wants a numerator and denominator, while it would make sense to give it a float. For example, 3.1415926535... would result in 245/78 , rather than 314/100 or 22/7 . In terms of efficiency, this would be called around three times at the start of the

How are fractions represented in computers?

泪湿孤枕 提交于 2019-12-08 16:29:48
问题 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 Prof. Cormen at 46:31 seconds he explains the multiplication hash function using a modular wheel which is a unit circle with several points in it and the points denote fractions. This prompted me to ask this basic question here in SO . 回答1: The

Using Fractions in Python

那年仲夏 提交于 2019-12-08 01:31:24
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 any suggestions on how to make this code tighter with more exceptions would be greatly appreciated. Here

Issues with setMaximumFractionDigits

懵懂的女人 提交于 2019-12-07 10:04:56
问题 setMaximumFractionDigits is not working with the following code. NSString *input=@"80.90"; NSNumberFormatter *numberFormatter=[[NSNumberFormatter alloc] init]; [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; [numberFormatter setMaximumFractionDigits:2]; NSNumber *number=[numberFormatter numberFromString:input]; number gets converted to 80.90000000000001. but I have put a limit to 2 decimal places. So, how do I get number as 80.90, instead of 80.90000000000001 回答1:

Comparing two fractions (< and friends)

你离开我真会死。 提交于 2019-12-07 07:14:54
问题 I have two fractions I like to compare. They are stored like this: struct fraction { int64_t numerator; int64_t denominator; }; Currently, I compare them like this: bool fraction_le(struct fraction a, struct fraction b) { return a.numerator * b.denominator < b.numerator * a.denominator; } That works fine, except that (64 bit value) * (64 bit value) = (128 bit value) , which means it will overflow for numerators and denominators that are too far away from zero. How can I make the comparison