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.
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:
A FP number is a fraction, by definition: A FP number consists of two integers, a mantissa m and an expontent e (and a sign, but that's irrelevant here). So each FP number is really a pair (m,e), and the value f it represents is f=mb^e (where b is a fixed integral base, usually 2). So the natural representation as a fraction is simply m / b^(-e) with e<0 (if e>=0 , f is integral anyway).
However, you probably want to get the fraction with the smallest reasonable divisor. This is a different question. To get is, you could e.g. use the bestappr function from the Pari/GP library. In your case, you'd probably use 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();
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
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;
}
Dividing both numbers with their HCF might help.
#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;
}
Related
So I'm new to stackoverflow and coding I was learning about functions in c++ and how the stack frame works etc..
in that I made a function for factorials and used that to calculate binomial coefficients. it worked fine for small values like n=10 and r=5 etc... but for large a medium value like 23C12 it gave 4 as answer.
IDK what is wrong with the code or I forgot to add something.
My code:
#include <iostream>
using namespace std;
int fact(int n)
{
int a = 1;
for (int i = 1; i <= n; i++)
{
a *= i;
}
return a;
}
int main()
{
int n, r;
cin >> n >> r;
if (n >= r)
{
int coeff = fact(n) / (fact(n - r) * fact(r));
cout << coeff << endl;
}
else
{
cout << "please enter valid values. i.e n>=r." << endl;
}
return 0;
}
Thanks for your help!
You're not doing anything "wrong" per se. It's just that factorials quicky become huge numbers.
In your example you're using ints, which are typically 32-bit variables. If you take a look at a table of factorials, you'll note that log2(13!) = 32.535.... So the largest factorial that will fit in a 32-bit number is 12!. For a 64-bit variable, the largest factorial you can store is 20! (since log2(21!) = 65.469...).
When you get 4 as the result that's because of overflow.
If you need to be able to calculate such huge numbers, I suggest a bignum library such as GMP.
Factorials overflow easily. In practice you rarely need bare factorials, but they almost always appear in fractions. In your case:
int coeff = fact(n) / (fact(n - r) * fact(r));
Note the the first min(n,n-r,r) factors of the denominator and numerator are identical. I am not going to provide you the code, but I hope an example will help to understand what to do instead.
Consider n=5, r=3 then coeff is
5*4*3*2*1 / 2*1 * 3*2*1
And before actually carrying out any calculations you can reduce that to
5*4 / 2*1
If you are certain that the final result coeff does fit in an int, you can also calculate it using ints. You just need to take care not to overflow the intermediate terms.
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.
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:
A FP number is a fraction, by definition: A FP number consists of two integers, a mantissa m and an expontent e (and a sign, but that's irrelevant here). So each FP number is really a pair (m,e), and the value f it represents is f=mb^e (where b is a fixed integral base, usually 2). So the natural representation as a fraction is simply m / b^(-e) with e<0 (if e>=0 , f is integral anyway).
However, you probably want to get the fraction with the smallest reasonable divisor. This is a different question. To get is, you could e.g. use the bestappr function from the Pari/GP library. In your case, you'd probably use 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();
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
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;
}
Dividing both numbers with their HCF might help.
#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;
}
I have written the following routine, which is supposed to truncate a C++ double at the n'th decimal place.
double truncate(double number_val, int n)
{
double factor = 1;
double previous = std::trunc(number_val); // remove integer portion
number_val -= previous;
for (int i = 0; i < n; i++) {
number_val *= 10;
factor *= 10;
}
number_val = std::trunc(number_val);
number_val /= factor;
number_val += previous; // add back integer portion
return number_val;
}
Usually, this works great... but I have found that with some numbers, most notably those that do not seem to have an exact representation within double, have issues.
For example, if the input is 2.0029, and I want to truncate it at the fifth place, internally, the double appears to be stored as something somewhere between 2.0028999999999999996 and 2.0028999999999999999, and truncating this at the fifth decimal place gives 2.00289, which might be right in terms of how the number is being stored, but is going to look like the wrong answer to an end user.
If I were rounding instead of truncating at the fifth decimal, everything would be fine, of course, and if I give a double whose decimal representation has more than n digits past the decimal point it works fine as well, but how do I modify this truncation routine so that inaccuracies due to imprecision in the double type and its decimal representation will not affect the result that the end user sees?
I think I may need some sort of rounding/truncation hybrid to make this work, but I'm not sure how I would write it.
Edit: thanks for the responses so far but perhaps I should clarify that this value is not producing output necessarily but this truncation operation can be part of a chain of many different user specified actions on floating point numbers. Errors that accumulate within the double precision over multiple operations are fine, but no single operation, such as truncation or rounding, should produce a result that differs from its actual ideal value by more than half of an epsilon, where epsilon is the smallest magnitude represented by the double precision with the current exponent. I am currently trying to digest the link provided by iinspectable below on floating point arithmetic to see if it will help me figure out how to do this.
Edit: well the link gave me one idea, which is sort of hacky but it should probably work which is to put a line like number_val += std::numeric_limits<double>::epsilon() right at the top of the function before I start doing anything else with it. Dunno if there is a better way, though.
Edit: I had an idea while I was on the bus today, which I haven't had a chance to thoroughly test yet, but it works by rounding the original number to 16 significant decimal digits, and then truncating that:
double truncate(double number_val, int n)
{
bool negative = false;
if (number_val == 0) {
return 0;
} else if (number_val < 0) {
number_val = -number_val;
negative = true;
}
int pre_digits = std::log10(number_val) + 1;
if (pre_digits < 17) {
int post_digits = 17 - pre_digits;
double factor = std::pow(10, post_digits);
number_val = std::round(number_val * factor) / factor;
factor = std::pow(10, n);
number_val = std::trunc(number_val * factor) / factor;
} else {
number_val = std::round(number_val);
}
if (negative) {
number_val = -number_val;
}
return number_val;
}
Since a double precision floating point number only can have about 16 digits of precision anyways, this just might work for all practical purposes, at a cost of at most only one digit of precision that the double would otherwise perhaps support.
I would like to further note that this question differs from the suggested duplicate above in that a) this is using C++, and not Java... I don't have a DecimalFormatter convenience class, and b) I am wanting to truncate, not round, the number at the given digit (within the precision limits otherwise allowed by the double datatype), and c) as I have stated before, the result of this function is not supposed to be a printable string... it is supposed to be a native floating point number that the end user of this function might choose to further manipulate. Accumulated errors over multiple operations due to imprecision in the double type are acceptable, but any single operation should appear to perform correctly to the limits of the precision of the double datatype.
OK, if I understand this right, you've got a floating point number and you want to truncate it to n digits:
10.099999
^^ n = 2
becomes
10.09
^^
But your function is truncating the number to an approximately close value:
10.08999999
^^
Which is then displayed as 10.08?
How about you keep your truncate formula, which does truncate as well as it can, and use std::setprecision and std::fixed to round the truncated value to the required number of decimal places? (Assuming it is std::cout you're using for output?)
#include <iostream>
#include <iomanip>
using std::cout;
using std::setprecision;
using std::fixed;
using std::endl;
int main() {
double foo = 10.08995; // let's imagine this is the output of `truncate`
cout << foo << endl; // displays 10.0899
cout << setprecision(2) << fixed << foo << endl; // rounds to 10.09
}
I've set up a demo on wandbox for this.
I've looked into this. It's hard because you have inaccuracies due to the floating point representation, then further inaccuracies due to the decimal. 0.1 cannot be represented exactly in binary floating point. However you can use the built-in function sprintf with a %g argument that should round accurately for you.
char out[64];
double x = 0.11111111;
int n = 3;
double xrounded;
sprintf(out, "%.*g", n, x);
xrounded = strtod(out, 0);
Get double as a string
If you are looking just to print the output, then it is very easy and straightforward using stringstream:
#include <cmath>
#include <iostream>
#include <iomanip>
#include <limits>
#include <sstream>
using namespace std;
string truncateAsString(double n, int precision) {
stringstream ss;
double remainder = static_cast<double>((int)floor((n - floor(n)) * precision) % precision);
ss << setprecision(numeric_limits<double> ::max_digits10 + __builtin_ctz(precision))<< floor(n);
if (remainder)
ss << "." << remainder;
cout << ss.str() << endl;
return ss.str();
}
int main(void) {
double a = 9636346.59235;
int precision = 1000; // as many digits as you add zeroes. 3 zeroes means precision of 3.
string s = truncateAsString(a, precision);
return 0;
}
Getting the divided floating point with an exact value
Maybe you are looking for true value for your floating point, you can use boost multiprecision library
The Boost.Multiprecision library can be used for computations requiring precision exceeding that of standard built-in types such as float, double and long double. For extended-precision calculations, Boost.Multiprecision supplies a template data type called cpp_dec_float. The number of decimal digits of precision is fixed at compile-time via template parameter.
Demonstration
#include <boost/math/constants/constants.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <iostream>
#include <limits>
#include <cmath>
#include <iomanip>
using boost::multiprecision::cpp_dec_float_50;
cpp_dec_float_50 truncate(cpp_dec_float_50 n, int precision) {
cpp_dec_float_50 remainder = static_cast<cpp_dec_float_50>((int)floor((n - floor(n)) * precision) % precision) / static_cast<cpp_dec_float_50>(precision);
return floor(n) + remainder;
}
int main(void) {
int precision = 100000; // as many digits as you add zeroes. 5 zeroes means precision of 5.
cpp_dec_float_50 n = 9636346.59235789;
n = truncate(n, precision); // first part is remainder, floor(n) is int value truncated.
cout << setprecision(numeric_limits<cpp_dec_float_50> ::max_digits10 + __builtin_ctz(precision)) << n << endl; // __builtin_ctz(precision) will equal the number of trailing 0, exactly the precision we need!
return 0;
}
Output:
9636346.59235
NB: Requires sudo apt-get install libboost-all-dev
doing a school project. i do not understand why the sin comes out to -NaN when after sin(90) and cos(120).
Can anyone help me understand this?
Also, when I put this in an online C++ editor it totally works, but when compiled in linux it does not.
// Nick Garver
// taylorSeries
// taylorSeries.cpp
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
const double PI = atan(1.0)*4.0;
double angle_in_degrees = 0;
double radians = 0;
double degreesToRadians(double d);
double factorial(double factorial);
double mySine(double x);
double myCosine(double x);
int main()
{
cout << "\033[2J\033[1;1H";
cout.width(4); cout << left << "Deg";
cout.width(9); cout << left << "Radians";
cout.width(11); cout << left << "RealSine";
cout.width(11); cout << left << "MySin";
cout.width(12); cout << left << "RealCos";
cout.width(11); cout << left << "MyCos"<<endl;
while (angle_in_degrees <= 360) //radian equivalent of 45 degrees
{
double sine = sin(degreesToRadians(angle_in_degrees));
double cosine = cos(degreesToRadians(angle_in_degrees));
//output
cout.width(4); cout << left << angle_in_degrees;
cout.width(9); cout << left << degreesToRadians(angle_in_degrees);
cout.width(11); cout << left << sine;
cout.width(11); cout << left << mySine(degreesToRadians(angle_in_degrees));
cout.width(12); cout << left << cosine;
cout.width(11); cout << left << myCosine(degreesToRadians(angle_in_degrees))<<endl;
angle_in_degrees = angle_in_degrees + 15;
}
cout << endl;
return 0;
}
double degreesToRadians(double d)
{
double answer;
answer = (d*PI)/180;
return answer;
}
double mySine(double x)
{
double result = 0;
for(int i = 1; i <= 1000; i++) {
if (i % 2 == 1)
result += pow(x, i * 2 - 1) / factorial(i * 2 - 1);
else
result -= pow(x, i * 2 - 1) / factorial(i * 2 - 1);
}
return result;
}
double myCosine(double x)
{
double positive = 0.0;
double negative= 0.0;
double result=0.0;
for (int i=4; i<=1000; i+=4)
{
positive = positive + (pow(x,i) / factorial (i));
}
for (int i=2; i<=1000; i+=4)
{
negative = negative + (pow(x,i) / factorial (i));
}
result = (1 - (negative) + (positive));
return result;
}
double factorial(double factorial)
{
float x = 1;
for (float counter = 1; counter <= factorial; counter++)
{
x = x * counter;
}
return x;
}
(Marcus has good points; I am going to ramble in other directions...)
Look at the terms in a Taylor series. They become too small to make any difference after fewer than 10 terms. Asking for 1000 is asking for trouble.
Instead of going for 1000, go until the next term does not add anything, something like:
term = pow(x, i * 2 - 1) / factorial(i * 2 - 1);
if (result + term == result) { break; }
result += term;
The series would run much faster if you iteratively calculated the pow and factorial rather than starting over each time. (But, probably speed is not an issue at this point.)
Float has 24 bits of binary precision. Beginning perhaps with 13!, you will get roundoff errors in float. Double, on the other hand, has 53 bits of precision and will last until about 22! without roundoff errors. My point is that you should have done factorial() in double.
Another problem is that the computation of the Taylor series gets somewhat 'unstable' for bigger arguments. Intermediate terms become bigger than the end result, thereby leading to other roundoff errors. To avoid this, a common way to compute sine and cosine is to first fold to between -45 and +45 degrees. No unfolding, except maybe for the sign, is needed later.
As for why you had trouble on one system but not the other -- Different implementations handle NaN differently.
Once you have gotten the NaN out of the way, try computing the series in reverse order. This will lead to a different set of roundoff errors. Will it make your sin() closer to the real sin?
The 'real' sin is probably computed in hardware with 64-bit fixed-point arithmetic, and will be "correctly rounded" to 53 or 24 bits well over 99% of the time. (This, of course, depends on the chip manufacturer, hence my 'hand-waving' statement.)
To judge how 'close' your value is, you need to compute ULPs (units in the last place). This involves looking at the bits in the float/double. (Beyond the scope of this question.)
Sorry about the TMI.
Before I answer this, a few remarks:
It's always helpful for your own debugging to keep your code tidy. Remove unnecessary empty lines, make sure your bracketing style is uniform, and properly indent. I did this for you, but believe me, you'll avoid a lot of bugs if you keep up a consistent style!
you have functions that take double as input and return double, but internally just use float; that should be a red flag!
your whole degreesToRadians would be better to read and only one third as long if you just used return (d*PI)/180;
Answers now:
in your factorial function, you calculate a factorial for values up to 1999. Hint: try to figure out the value of 1999! and look up the maximum number that float on your machine can hold. Then look up double's maximum. How many orders of magnitude is 1999! larger?
1999! is ca. 10^5732. That is a large number, about 150 orders of magnitude larger than what a 32bit float can hold, or still 18 orders of magnitude larger than what a 64bit double can hold. To compare, to store 1999! in a double would be like trying to fit the distance from sun center to earth center in the typical 0.1µm diameter of bacteria.
I am at the moment trying to code a titration curve simulator. But I am running into some trouble with comparing two values.
I have created a small working example that perfectly replicates the bug that I encounter:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double a, b;
a = 5;
b = 0;
for(double i = 0; i<=(2*a); i+=0.1){
b = i;
cout << "a=" << a << "; b="<<b;
if(a==b)
cout << "Equal!" << endl;
else
cout << endl;
}
return 0;
}
The output at the relevant section is
a=5; b=5
However, if I change the iteration increment from i+=0.1 to i+=1 or i+=0.5 I get an output of
a=5; b=5Equal!
as you would expect.
I am compiling with g++ on linux using no further flags and I am frankly at a loss how to solve this problem. Any pointers (or even a full-blown solution to my problem) are very appreciated.
Unlike integers, multiplying floats/doubles and adding them up doesn't produce exactly the same results.
So the best practice is find if the abs of their difference is small enough.
If you have some idea on the size of the numbers, you can use a constant:
if (fabs(a - b) < EPS) // equal
If you don't (much slower!):
float a1 = fabs(a), b1 = fabs(b);
float mn = min(a1,b1), mx = max(a1,b1);
if (mn / mx > (1- EPS)) // equal
Note:
In your code, you can use std::abs instead. Same for std::min/max. The code is clearer/shorter when using the C functions.
I would recommend restructuring your loop to iterate using integers and then converting the integers into doubles, like this:
double step = 0.1;
for(int i = 0; i*step<=2*a; ++i){
b = i*step;
cout << "a=" << a << "; b="<<b;
if(a==b)
cout << "Equal!" << endl;
else
cout << endl;
}
This still isn't perfect. You possibly have some loss of precision in the multiplication; however, the floating point errors don't accumulate like they do when iterating using floating point values.
Floating point arithmetic is... interesting. Testing equality is annoying with floats/doubles in most languages because it is impossible to accurately represent many numbers in IEEE floating point math. Basically, where you might compute an expression to be 5.0, the compiler might compute it to be 4.9999999, because it's the closest representable number in the IEEE standard.
Because these numbers are slightly different, you end up with an inequality. Because it's unmaintainble to try and predict which number you will see at compile time, you can't/shouldn't attempt to hard code either one of them into your source to test equality with. As a hard rule, avoid directly checking equality of floating point numbers.
Instead, test that they are extremely close to being equal with something like the following:
template<typename T>
bool floatEqual(const T& a, const T& b) {
auto delta = a * 0.03;
auto minAccepted = a - delta;
auto maxAccepted = a + delta;
return b > minAccepted && b < maxAccepted;
}
This checks whether b is within a range of + or - 3% of the value of a.