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
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
You can't. You would need to write a class dedicated to holding rational numbers (i.e. fractions). Or maybe just use the Boost Rational Number library.
int
values that produce 0.66666667
. Why not just write a function that picks one? The answer is not "you can't" –
Apocope 0.66666667
. It might be 1/3, or 1000000/3000001, or 1000001/3000000, etc. (assuming enought zeros to exhaust the FP precision). You can easily show that for any FP number, there is an infinite (though countably infinite) number of integer fractions. –
Grisly 66666667/100000000
, that's O(1). Then run some variation of GCD to reduce the fraction if you want. –
Apocope If I understand correctly, you have a floating point number (a float
or double
type variable), and you'd like to output this value as a fraction.
If that is the case, you need to further specify your question:
bestappr(x, A)
, with x your input, and A the largest denominator you want to try. bestappr will give you the fraction closest to x whose denominator is still smaller than A.write your own Rational class to calculate divisions
class Rational
{
public:
int numerator, denominator;
Rational(int num, int den=1){
numerator = num;
denominator=den;
}
Rational(Rational other){
numerator = other.numerator;
denominator = other.denominator;
}
double operator / (int divisor){
denominator *= divisor;
simplificate();
return getrealformat();
}
Rational& operator / (int divisor){
denominator *= divisor;
simplificate();
return this;
}
Rational& operator / (Rational &divisor){
numerator *= divisor.numerator;
denominator *= divisor.denominator;
simplificate();
return this;
}
double operator / (int divisor){
denominator *= divisor;
simplificate();
return getrealformat();
}
double getrealformat(){
return numerator/denominator;
}
simplificate(){
int commondivisor = 1;
for(int i=2;i<=min(abs(numerator), abs(denominator));i++)
if( numerator%i == 0 && denominator%i == 0 )
commondivisor = i;
numerator /= commondivisor;
denominator /= commondivisor;
}
};
use
Rational r1(45), r2(90), r3=r1/r2;
cout<<r3.numerator<<'/'<<r3.denominator;
cout<<r3.getrealformat();
float
or double
to Rational
? –
Piscatory how can I just get the original format (i.e.g 2/3) instead of 0.66666667
Only with great difficulty by wrapping something like the GMP library with custom output operators. Below is a bit more on GMP:
What is GMP?
GMP is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating point numbers. There is no practical limit to the precision except the ones implied by the available memory in the machine GMP runs on. GMP has a rich set of functions, and the functions have a regular interface.
The main target applications for GMP are cryptography applications and research, Internet security applications, algebra systems, computational algebra research, etc.
GMP is carefully designed to be as fast as possible, both for small operands and for huge operands. The speed is achieved by using fullwords as the basic arithmetic type, by using fast algorithms, with highly optimised assembly code for the most common inner loops for a lot of CPUs, and by a general emphasis on speed.
GMP is faster than any other bignum library. The advantage for GMP increases with the operand sizes for many operations, since GMP uses asymptotically faster algorithms.
The first GMP release was made in 1991. It is continually developed and maintained, with a new release about once a year.
You have to store them in some sort of Fraction class with two integer fields. Of course, you have to simplify the fraction before using it for output.
You can develop your own class or use some libraries, like this one for exact maths: CLN - Class Library for Numbers
float
or double
to a fraction. The conversion to a fraction can occur with a Fraction class. –
Piscatory float
and double
are fractions, in a way. As I understood it, the question was about how to manage the issue from the beginning, and the solution is to avoid the creation of float
or double
variables in the first place. –
Criminality This is impossible in general: floating point numbers are not precise and do not retain sufficient information to fully reconstruct a fraction.
You could, however, write a function that heuristically finds an "optimal" approximation, whereby fractions with small numerators and denominators are preferred, as are fractions that have almost the same value as the floating point number.
If you're in full control of the code, Oli's idea is better: don't throw away the information in the first place.
You can store all your fraction's numerators and denominators as intergers. Integers have exact representations in binary.
To simplify efforts, I suggest you stick with known denominators if possible.
I'm working with an application where the fractions are restricted to denominators of powers of 2 or using 3 (for thirds).
I convert to these fractions using an approximation (rounding to the nearest 1.0/24.0).
Without some restrictions, finding the denominator can be quite a chore and take up a lot of the execution time.
I am beginner and this way that I use may not be a proper way
#include <iostream>
using namespace std;
int main ()
{
double a;
double b;
double c;
cout << "first number: ";
cin >> a;
cout << "second number: ";
cin >> b;
c = a/b;
cout << "result is: " << c << endl;
if (b != 0) {
if (a > 0) {
if (c - (int)c > 0 && c - (int)c < 1)
cout << "fraction: " << a << "/" << b;
} else {
if (c - (int)c < 0 && c - (int)c < 1)
cout << "fraction: " << a << "/" << b;
}
}
return 0;
}
1.25
into 1
and .25
). But the question is about transforming the floating point number into a fraction-representation of a rational number, i.e. 1 1/4
or 5/4
. –
Genocide 1.25
your program outputs 1
and .25
, correct? How does it transform .25
into 1/4
? –
Genocide a
and b
explicitly. Some of the other answers said this too; if a rational number is given as input, you could simply store it so you have it when you need it. But the real difficulty is in calculating a
and b
from the floating point number. –
Genocide c
, and the program calculates a
and b
. But anyway... it's not that important. –
Genocide #include <iostream>
using namespace std;
int main() {
int a,b,q,r;
cin>>a>>b;//first number and second number
q = a/b;
r = a-q*b;
cout<<q<<" "<<r<<" "<<"/"<<" "<<b<<"\n";
return 0;
}
I just got quotient by a/b then got the remainder by a-q*b. open for suggetions if any.
Use greatest common divisor concept.
if we divide the numbers with gcd of their numbers we get least possible value of those.example:-
#define si long long
int main() {
si int total=4;
si int count=2;
si int g= __gcd(count,total);
count/=g;
total/=g;
cout<<count<<"/"<<total<<endl;
}
for more reference check out this:-https://www.codechef.com/viewsolution/17873537
This is a program to convert a decimal number into a fraction
#include<iostream>
using namespace std;
int main()
{
float num, origNum, rem = 1;
int den = 1, i, count=0, gcd=1;
cout << "Enter any float number to convert it into mixed fraction: ";
cin >> origNum;
num = origNum - static_cast<int>(origNum);
if (num > 0.1)
{
while ( (rem > 0.1) )
{
num = num * 10;
rem = num - static_cast<int>(num);
count++;
}
for (i = 1; i <= count; i++) // counter is for the calculation of denominator part of mixed fraction
{
den = den * 10;
}
for (i = 2; i <= num|| i<=rem; i++)
{
if( (static_cast<int>(num) % i == 0) && (den % i == 0) )
{
gcd = i;
}
}
cout << (static_cast<int>(origNum)) << " and " << (static_cast<int>(num))/gcd << "/" << den/gcd;
}
else
cout << (static_cast<int>(origNum));
return 0;
}
© 2022 - 2024 — McMap. All rights reserved.
355/113
– Corelation