Mar 24, 2020 at 6:56am UTC
So I rewrited my code and suddenly same problem occured.
source code :
https://drive.google.com/open?id=1GdOu5BbH8bbJFbkn_1NYUVZyt_Ty8WA-
About problem : I extracted data from file it was ok, but when multiplied all the coefficients the result was wrong, even on the low numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
// Abbriviated Multiplication Formula calculator using Pascal's triangle concept
#include <iostream>
#include <cmath>
#include <fstream>
#include <sstream>
using namespace std;
int main (void ) {
const int MAX_ARRAY_SIZE = 16;
long int pascalData[MAX_ARRAY_SIZE]; // or coefficients
unsigned long long int firstVar[MAX_ARRAY_SIZE]={1}, secondVar[MAX_ARRAY_SIZE]={1}, result[MAX_ARRAY_SIZE]={1};
int kA, kB, powerNum;
cout << "Welcome to the AMF Pascal coefficients calculator! \n" ;
cout << "Enter coefficients of the formula \" (Ax +- By)^N \" A, B, N: " ;
cin >> kA >> kB >> powerNum;
cout << endl;
powerNum ++; // just for convinience in lines 42, 47
// first read coefficients from table/triangle and write it to array...
ifstream readTable ("pascal.txt" );
string bufferString;
for (int i=0; i<powerNum; i++) { // getting to the right line
getline (readTable, bufferString);
}
stringstream bufferSS;
bufferSS << bufferString;
for (int i=0; i<powerNum; i++) { // this loop will extract integers from bufferString
bufferSS >> pascalData[i];
}
for (int i=0; i<powerNum; i++) {
firstVar[i] = pow(kA, powerNum-i);
secondVar[i] = pow (kB, i);
}
for (int i=0; i< powerNum; i++) { // multiplying data to get the resulting coefficients
result[i] = firstVar[i] * secondVar[i] * pascalData[i];
}
for (int i=0; i<powerNum; i++) { // output
cout << result[i] << endl;
}
}
Last edited on Mar 24, 2020 at 7:03am UTC
Mar 24, 2020 at 7:34am UTC
I see that you're using integers everywhere, but also using pow().
1. What sort of exponents and bases are you handling?
2. pow() is a floating point function. Are you expecting exact results even in the unit digit, or are you content with approximate results?
You may need to use a bignum library to get the results you're expecting.
Mar 24, 2020 at 11:36am UTC
Hi helios,
I am not going to use floating point data so int's are ok but as you mentioned I need bignum library. I will search for one but it would be great if you give an example code.