How to keep decimal of a number when sending to a function
-
Hi guys. I have a code that generates some numbers with high decimal. When I put these numbers in a vector and send it to a function, sometimes I feel that some decimals are lost and they are not transmitted exactly as they are. I send my numbers like this:
long double aa=-k1*k2*k3*kw; long double bb=-(2*k1*k2*k3*Cs+k1*k2*kw+3*k1*k2*k3*Ca_new); long double cc= -(k1*k2*Cs+2*k1*k2*Ca_new+k1*kw-k1*k2*k3); long double dd=(k1*k2-k1*Ca_new-kw); long double ee=(k1+Cs); vector<long double>_coeff; _coeff.clear(); _coeff.push_back(5);//degree _coeff.push_back((long double)aa); _coeff.push_back((long double)bb); _coeff.push_back((long double)cc); _coeff.push_back((long double)dd); _coeff.push_back((long double)ee); _coeff.push_back((long double)1); long double max=Roots(_coeff);
I have also tried different modes like the following code:
_coeff.push_back(-k1*k2*k3*kw);
-
Hi guys. I have a code that generates some numbers with high decimal. When I put these numbers in a vector and send it to a function, sometimes I feel that some decimals are lost and they are not transmitted exactly as they are. I send my numbers like this:
long double aa=-k1*k2*k3*kw; long double bb=-(2*k1*k2*k3*Cs+k1*k2*kw+3*k1*k2*k3*Ca_new); long double cc= -(k1*k2*Cs+2*k1*k2*Ca_new+k1*kw-k1*k2*k3); long double dd=(k1*k2-k1*Ca_new-kw); long double ee=(k1+Cs); vector<long double>_coeff; _coeff.clear(); _coeff.push_back(5);//degree _coeff.push_back((long double)aa); _coeff.push_back((long double)bb); _coeff.push_back((long double)cc); _coeff.push_back((long double)dd); _coeff.push_back((long double)ee); _coeff.push_back((long double)1); long double max=Roots(_coeff);
I have also tried different modes like the following code:
_coeff.push_back(-k1*k2*k3*kw);
@SeyMohsenFls said in How to keep decimal of a number when sending to a function:
When I put these numbers in a vector and send it to a function, sometimes I feel that some decimals are lost and they are not transmitted exactly as they are. I send my numbers like this:
That does not happen. You have a
vector<long double>
, if you putlong double
s into it that is what it will store.Your
long double
variables' assignments will only receive along double
if at least one of the variables on the right-hand side of the assignment is itself along double
. Is that always the case? Meanwhile, what eveidence do you have for "I feel" ?This question has nothing to do with Qt, just C++.
-
I used "feel" because the value I get from the function is incorrect. While the function has no problems. So there may be a problem with sending numbers.
-
Are you seeing a valid root but not the expected one?
The answer(s) (there may be more than one for a fifth-order polynomial) are very much dependent on the characteristics of your root finding algorithm. Some numeric algorithms get stuck in local minima, oscillate around a root without converging quickly, stop after a certain number of iterations, stop when a certain error threshold is reached, control precision loss in different ways etc. Matching the algorithm to the typical inputs is important in some applications.
Depending on the platform, a long double may be the same as a double in terms of decimal digit precision. To find out:
#include <iostream> #include <limits> int main(){ std::cout << std::numeric_limits<double>::digits10 << " " << std::numeric_limits<long double>::digits10 << std::endl; }
Gives 15 and 18 on my x86_64 machine with GCC.
The C-style static casts in the loading of the std::vector are unnecessary given all the input variables are declared long double.
-
I used "feel" because the value I get from the function is incorrect. While the function has no problems. So there may be a problem with sending numbers.
@SeyMohsenFls said in How to keep decimal of a number when sending to a function:
because the value I get from the function
What "function"? I don't particularly see any functions in your code above.
is incorrect
Incorrect in what specific way? How do you know it is "incorrect"?
So there may be a problem with sending numbers.
C++ does not work like that. It does not "mostly get things right, but has a problem/goes wrong when you 'send numbers'". Else programs would be pretty unreliable!
Your code just shows a few calculations. No inputs specified, no outputs expected, no information about what you actually see instead or how you see it. Just "a feeling". Whether you want help here or (better) in a C++ forum, you need to produce a minimal example which actually runs and shows values which differ from you expect.
-
Have a look at the numbers inside a debugger. I bet these are correct.
You don't provide enough information how you check the numbers. My guess is that you somehow print the on the console. Per default C++ displays 6 digits (or so). This is what @ChrisW67 points to with getting the number of digits10. You need to output your numbers with at least these many digits (and more digits don't help) to know the exact number. Remember that computers use base 2 instead of base 10 and not every number can be perfectly represented in base 2 (e.g. try 0.2).