fractions

Decimals to Fractions Conversion Algorithm, how does it work?

﹥>﹥吖頭↗ 提交于 2019-12-13 03:14:23
问题 I'm working on a harmonic ratio program and part of what I want a user to be able to do is plug in various ratios and have the decimal-valued frequency that's playing show you more ratio-locked frequencies that are higher or lower. Anyway, on this webpage there is a javascript algorithm to show fractional values (ratios) from given decimals. http://www.mindspring.com/~alanh/fracs.html How does it work? I am interested in implementing it myself, but I don't really understand how it functions.

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

偶尔善良 提交于 2019-12-12 07:46:32
问题 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; fractionPart = d - integerPart; return new double[]{integerPart, fractionPart}; } But what I got is: [0] = 5.0 [1] = 0.5999999999999996 Do you have any suggestion about doing this without converting the number to string? 回答1: Use BigDecimal to do that same calculation. (using doubles has precision problems because

Double to fraction in Java

孤者浪人 提交于 2019-12-12 07:18:39
问题 So what I'm trying to do is convert double to rational number. I check how many digits there is after decimal point and I want to save the number 123.456 as 123456 / 1000, for example. public Rational(double d){ String s = String.valueOf(d); int digitsDec = s.length() - 1 - s.indexOf('.'); for(int i = 0; i < digitsDec; i++){ d *= 10; } System.out.println((int)d); //checking purposes } However, for the number 123.456 I get a round off error and the result is 123455. I guess it'd be possible to

Weird Java fraction behavior

戏子无情 提交于 2019-12-12 06:47:31
问题 I have seen a very weird behaviour in Java's double variable, as I'm trying to simply add small fractions to a double and I see a completely bizarre results. double test = 0; test += 0.71; test += 0.2; Now I'd expect the result to be: test = 0.91 Right? Wrong! In reality, this is the number I get in my test double: test = 0.9099999999999999 Now while this is very close, it's a very bizarre fraction loss, and in the long run it causes serious bugs in my program. With a float I've gotten even a

Implementing istream in a C++ Fraction Calculator

痴心易碎 提交于 2019-12-12 05:34:57
问题 I am trying to learn the object oriented programming and make the simple fraction calculator than can add or subtract any number of functions and write the answer as a reduced fraction. Example: input= 3/2 + 4/ 8 , output = 2 I am trying overload operators in order to accomplish this. So in the program, I am trying to develop the input consists of an expression made of fractions separated by the operators '+'or '-'. The number of fractions in the expression is arbitrary. Each of the following

Need help understanding struct, and a few errors with program

回眸只為那壹抹淺笑 提交于 2019-12-11 10:58:57
问题 I don't get how to use structs properly to achieve my goal of calculating Fractions (it is required). Quite frankly I don't have much of an idea of what I'm doing, this is only my 3rd class in C++ and I feel lost...this was the task assigned to us Your enter() function accepts a fraction from the user. Your simplify() function simplifies the fraction that it receives, if possible. Your display() function displays the fraction that it receives. Your global functions use a Fraction type. A

Operator overloading : cannot add two pointers

北慕城南 提交于 2019-12-11 10:47:22
问题 I created a Fraction class that is has member functions to add, subtract, multiply, and divide between two Fraction Objects along with the required default and copy constructors. For this problem, I must used pointers (cannot use vectors!) because the Fraction objects can only be created if the user chooses to. In short, the pointer declaration and new initialization are in difference scopes. I am trying to also overload the operator=, operator+, operator-, operator*, and operator/ to accept

Python - Execute a String Code For Fractions

本秂侑毒 提交于 2019-12-11 10:21:04
问题 I've been working in some type of calculator for fractions using from fractions import * following the next logic: a = Fraction(1,4) b = Fraction(2,5) c = Fraction(3,4) print(a+b*c) OUTPUT 11/20 But I need to execute the statement from a string, just like 1/4 + 1/2 and for some reason always returns me 0 or 1 : from fractions import * class main(): for i in range(0, 10): print "\t\nWRITE YOUR OPERATION" code = 'print(' s = raw_input() elements = s.split() for element in elements: print

Finding the numerator of a float's fractional value, given a specific denominator [duplicate]

…衆ロ難τιáo~ 提交于 2019-12-11 08:26:07
问题 This question already has answers here : Closed 8 years ago . Possible Duplicates: How to convert floats to human-readable fractions? Convert decimal to fraction in Objective-C? I want to take a decimal, 5.50 , which is in a variable, and divide only the fractional part by 0.0625 , which is my accuracy point. This would give me 8 , as in 8/16 or 1/2 . Then I would like to display that answer as 5 8/16 or 1/2 in a text field. I know some answers will return a decimal still and not a whole

python class fraction numbers

*爱你&永不变心* 提交于 2019-12-11 05:36:10
问题 I made a code that makes the user input two fraction numbers in the form "d/n". How can I make it print the reduced form of the fraction ? ex: when I type 2/4 it prints 1/2? import sys class Rational: def __init__(self,n,d): self.numerator= n self.denominator= d def value(self): return float(self.numerator)/float(self.denominator) def __repr__(self): return "%d/%d ~ %g" % (self.numerator,self.denominator,self.value()) def read(self): while 1: try: s=raw_input("Enter fraction n/d: ") n,d= s