Your way to find LCM is incorrect. You should compute e like this: private static long gcd(long a, long b) { while (b > 0) { long temp = b; b = a % b; a = temp; } return a; } private static long lcm(long a, long b) {...

c++,fractions,simplify,greatest-common-divisor

The problem is that you do all your computations on frac before asking the user to input a fraction — and then you overwrite whatever the user inputs. You need to move this bit: cout << "Enter a fraction" << endl; cin >> frac; much, much higher up....

Use reference to reflect the changes in top and bottom and print those in your add_fraction function after calling reduce_fraction void reduce_fraction ( int & top, int & bottom) { ~~~ ~~~ //... } Then, reduce_fraction(top, bottom); cout << top << "/" << bottom << endl; ...

javascript,function,decimal,fractions

the value of num is not updated, it is not passed by reference. function parseFractions(x) { var y = x.split(' '); if (y.length > 1) { var z = y[1].split('/'); var a = (+y[0] + (z[0] / z[1])); x = a; } else { z = y[0].split('/'); if (z.length >...

You simply need to change the cell format; you want to use ???/???. This will make Excel represent any decimal number to the closest fraction approximation it can find using the specified numerator and denominator significant digits (number of ? in the format string) If the cell input is directly...

You can use the standard library like this: In [1]: from decimal import Decimal, getcontext In [2]: def calculate(num, denom, precision=100): ...: getcontext().prec = precision ...: return str(Decimal(num) / Decimal(denom)) ...: In [3]: calculate(1, 7) Out[3]: '0.1428571428571428571428571428571428571428571428571428571428571428571428571428571428571428571428571429' ...

c++,operator-overloading,fractions

In your operator+=, you declare a fraction: fraction add(f1.getNum1()*f2.getDen2()+f2.getNum2()*f1.getDen1(),f1.getDen1()*f2.getDen2()); This is of the form: fraction a(int1, int2); But you have not defined any constructor for fraction that takes two int arguments. The compiler tells you (correctly) that it doesn't know what you mean....

exception,fractions,divide-by-zero

The statement bottom % top yields a divide by zero error when top is zero. You can fix it by changing the first line of your simplify() method to this: if (top != 0 && bottom % top == 0) { ...

c++,math,fractions,bignum,arbitrary-precision

If you're using boost, you can use boost::rational in conjunction with boost::multiprecision::cpp_int (arbitrary-precision integer): #include <boost/rational.hpp> #include <boost/multiprecision/cpp_int.hpp> using boost::multiprecision::cpp_int; typedef boost::rational<cpp_int> fraction_t; For some reason, the constructor fraction_t("1", "2") doesn't work, although cpp_int("1") does, so if you need large int literals, you can use this helper function to not...

You need to create a class that is a wrapper for fractions. For member variables you would want integers Numerator and Denominator. You would need methods for adding, subtracting, multiplying, and dividing the fractions with each other.

A preferred practice is to first read an entire line into a buffer first, and then parse it. For example, char buf[80]; char dummy[80]; // ... if (!fgets(buf, sizeof buf, stdin) { // handle error } else if (sscanf(buf, "%d%d/%d%s", &whol, &num, &den, dummy) == 3) { // ... handle...

Ok I found the way ! #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { float voltage,current1,current2; char voltages[20], current[20], dtm[100]; strcpy( dtm, "VOLTAGE 0.231023459 CURRENT -0.234334567 0.345511234"); sscanf( dtm, "%s %f %s %f %f", voltages,&voltage,current,¤t1, ¤t2); printf("voltage = %f \n",voltage); printf("current1 = %f \n",current1); printf("current2 = %f \n",current2); return(0);...

python,variables,fractions,ratio,organizer

You can try something like this: from __future__ import division def spreadout(X, Y): ratio = len(X) / len(Y) result = [] while X or Y: if not Y or len(X)/len(Y) >= ratio: result.append(X.pop()) else: result.append(Y.pop()) return result The idea behind the algorithm is to determine the ratio of the X...

python,int,fractions,python-internals

The __trunc__ method is used. >>> class X(object): def __trunc__(self): return 2. >>> int(X()) 2 __float__ does not work >>> class X(object): def __float__(self): return 2. >>> int(X()) Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> int(X()) TypeError: int() argument must be a string, a bytes-like object...

java,trim,indexoutofboundsexception,fractions

Try String frac = scan.nextLine(); I think next() won't get anything after a space.

java,split,double,decimal,fractions

You shouldn't work with doubles as you are losing precision and this can lead to serious errors. But in the case of 1.0001 the problem is that: Double.toString(1.0001) == "1.0E-4" Then you try to parse "0E-4" and you get 0 instead of 1. You could do the following if you...

If the numerator and denominator are ints then the division is integer division, you'll never get the correct fractional part Casting it to double can correct most of the problem but you'll face the slow divisions and sometimes errors due to floating-point roundings. You should use multiplication instead. It'll be...

Use: for(int i = 3; i >= 0; i--) { float offsetFraction = ((float)(i+1))/(4); } This starts with the value 3 for i and decrements it at each loop iteration. Unless you are using i for something else, this is simpler to understand: for(int i = 4; i >= 1;...

If you don't want to allow zero for either the numerator or the denominator, then that's a class invariant, and should be enforced in the fraction class itself. class fraction { int numerator, denominator; public: fraction(int num, int denom) : numerator(num), denominator(denom) { if (0 == num || 0 ==...

You should find the greatest common divisor of the resulted numbers and divide the numerator and denominator by it. Here is one way to do it: public class Rational { private int num, denom; public Rational(double d) { String s = String.valueOf(d); int digitsDec = s.length() - 1 - s.indexOf('.');...

oracle,plsql,oracle9i,fractions

you are asking to execute a string or varchar2 as equation, the result is a value of this equation. right? here is a solution for this, I assume the space in "7 4/5" means "7+ 4/5" or you have to modify the function bellow to meet your requirements. create or...

c,linux,binary,infinite-loop,fractions

Your loop is incorrect: you should be shifting demonExp to the right too. It runs indefinitely for numerator=0 and an even denomExp. If numerator and denomExp are integer types, and the number is just a fraction numerator/denomExp, you can fix the code this way: while (numerator && is_even(numerator) && is_even(denomExp))...

You'll need multiple loops, one for each output line : // first line for (int i = 0; i < denom_sum.length; i++) { num_sum[i] = num_sum[i] + num[i]; System.out.printf("%d ", num_sum[i]); numeratorSum = numeratorSum + num_sum[i]; } System.out.printf(" %d \n", numeratorSum); // second line for (int i = 0; i...

If I correctly understood your question, I am afraid it does not have a complete solution. Since a float is stored with a finite number of bits, not all fractions can be represented, especially the ones that are not decimal numbers like 2/3. Even for decimal numbers, not all of...

You have two options: Use float.as_integer_ratio(): >>> (0.25).as_integer_ratio() (1, 4) Use the fractions.Fraction() type: >>> from fractions import Fraction >>> Fraction(0.25) Fraction(1, 4) The latter has a very helpful str() conversion: >>> str(Fraction(0.25)) '1/4' >>> print Fraction(0.25) 1/4 Because floating point values can be imprecise, you can end up with...